Cloud Function for Firebase: Email Duplicates - node.js

Im trying to implement some code that sends an email to anyone who would like to sign up to my newsletter. The code is actually working, but it sends multiple duplicates. Im using Firebase' samplecode, like this.
I think the problem is that it listensens for every change on the {uid} and I'm setting 4 values. If I manually change anything at the database from the dashboard, it triggers the event and sends a new mail. My code:
'use strict';
const functions = require('firebase-functions');
const nodemailer = require('nodemailer');
// Configure the email transport using the default SMTP transport and a GMail account.
// For other types of transports such as Sendgrid see https://nodemailer.com/transports/
// TODO: Configure the `gmail.email` and `gmail.password` Google Cloud environment variables.
const gmailEmail = encodeURIComponent(functions.config().gmail.email);
const gmailPassword = encodeURIComponent(functions.config().gmail.password);
const mailTransport = nodemailer.createTransport(
`smtps://${gmailEmail}:${gmailPassword}#smtp.gmail.com`);
// Sends an email confirmation when a user changes his mailing list subscription.
exports.sendEmailConfirmation = functions.database.ref('/mailingList/{uid}').onWrite(event => {
const snapshot = event.data;
const val = snapshot.val();
if (!snapshot.changed('subscribed')) {
return;
}
const mailOptions = {
from: '"Spammy Corp." <noreply#firebase.com>',
to: val.email
};
// The user just subscribed to our newsletter.
if (val.subscribed == true) {
mailOptions.subject = 'Thanks and Welcome!';
mailOptions.text = 'Thanks you for subscribing to our newsletter. You will receive our next weekly newsletter.';
return mailTransport.sendMail(mailOptions).then(() => {
console.log('New subscription confirmation email sent to:', val.email);
});
}
});

A database trigger will run for each change made to the path it's monitoring, and you need to plan for that. In your function, you need a way to figure out if the email has already been sent. The typical solution is to write a boolean or some other flag value back into the node that triggered the change, then check for that value every time and return early if it's set.

Related

Sending Notification With Firebase Cloud Functions to Specific users

I am using firebase cloud function in my firebase group chat app, Setup is already done but problem is when some one send message in any group then all user get notification for that message including non members of group.
I want to send notification to group specific users only, below is my code for firebase cloud function -
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const _ = require('lodash');
admin.initializeApp(functions.config().firebase);
exports.sendNewMessageNotification = functions.database.ref('/{pushId}').onWrite(event => {
const getValuePromise = admin.database()
.ref('messages')
.orderByKey()
.limitToLast(1)
.once('value');
return getValuePromise.then(snapshot => {
const { text, author } = _.values(snapshot.val())[0];
const payload = {
notification: {
title: text,
body: author,
icon: ''
}
};
return admin.messaging()
.sendToTopic('my-groupchat', payload);
});
});
This will be really help full, if anyway some one can suggest on this.
As per our conversation on the comments I believe that the issue is that you are using a topic that contains all the users, which is the my-groupchat topic. The solution would be to create a new topic with the users that are part of this subtopic.
As for how to create such topics, there are a couple of examples in this documentation, in there you can see that you could do it server side, or client side. In your case, you could follow the examples for the server side, since you would need to add them in bulk, but as new users are added it could be interesting to implement the client side approach.

Error: Invalid login: Application-specific password required

i want send Welcome notification when user sign in using Cloud-Function with firebase auth
so i m using nodejs CLI and run the code
my index.js file
'use strict';
const functions = require('firebase-functions');
const nodemailer = require('nodemailer');
// Configure the email transport using the default SMTP transport and a GMail account.
// For Gmail, enable these:
// 1. https://www.google.com/settings/security/lesssecureapps
// 2. https://accounts.google.com/DisplayUnlockCaptcha
// For other types of transports such as Sendgrid see https://nodemailer.com/transports/
// TODO: Configure the `gmail.email` and `gmail.password` Google Cloud environment variables.
const gmailEmail = functions.config().gmail.email;
const gmailPassword = functions.config().gmail.password;
const mailTransport = nodemailer.createTransport({
service: 'gmail',
auth: {
user: gmailEmail,
pass: gmailPassword,
},
});
// Your company name to include in the emails
// TODO: Change this to your app or company name to customize the email sent.
const APP_NAME = 'Cloud Storage for Firebase quickstart';
// [START sendWelcomeEmail]
/**
* Sends a welcome email to new user.
*/
// [START onCreateTrigger]
exports.sendWelcomeEmail = functions.auth.user().onCreate((user) => {
// [END onCreateTrigger]
// [START eventAttributes]
const email = user.email; // The email of the user.
const displayName = user.displayName; // The display name of the user.
// [END eventAttributes]
return sendWelcomeEmail(email, displayName);
});
// [END sendWelcomeEmail]
// [START sendByeEmail]
/**
* Send an account deleted email confirmation to users who delete their accounts.
*/
// [START onDeleteTrigger]
exports.sendByeEmail = functions.auth.user().onDelete((user) => {
// [END onDeleteTrigger]
const email = user.email;
const displayName = user.displayName;
return sendGoodbyeEmail(email, displayName);
});
// [END sendByeEmail]
// Sends a welcome email to the given user.
async function sendWelcomeEmail(email, displayName) {
const mailOptions = {
from: `${APP_NAME} <noreply#firebase.com>`,
to: email,
};
// The user subscribed to the newsletter.
mailOptions.subject = `Welcome to ${APP_NAME}!`;
mailOptions.text = `Hey ${displayName || ''}! Welcome to ${APP_NAME}. I hope you will enjoy our service.`;
await mailTransport.sendMail(mailOptions);
console.log('New welcome email sent to:', email);
return null;
}
// Sends a goodbye email to the given user.
async function sendGoodbyeEmail(email, displayName) {
const mailOptions = {
from: `${APP_NAME} <noreply#firebase.com>`,
to: email,
};
// The user unsubscribed to the newsletter.
mailOptions.subject = `Bye!`;
mailOptions.text = `Hey ${displayName || ''}!, We confirm that we have deleted your ${APP_NAME} account.`;
await mailTransport.sendMail(mailOptions);
console.log('Account deletion confirmation email sent to:', email);
return null;
}
i refer this code https://github.com/firebase/functions-samples/blob/master/quickstarts/email-users/functions/index.js
but after i ran the code i got error
Error: Invalid login: 534-5.7.9 Application-specific password required. Learn more at
534 5.7.9 https://support.google.com/mail/?p=InvalidSecondFactor i82sm13686303ilf.32 - gsmtp
at SMTPConnection._formatError (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:784:19)
at SMTPConnection._actionAUTHComplete (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:1523:34)
at SMTPConnection._responseActions.push.str (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:550:26)
at SMTPConnection._processResponse (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:942:20)
at SMTPConnection._onData (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:749:14)
at TLSSocket.SMTPConnection._onSocketData.chunk (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:195:44)
at emitOne (events.js:116:13)
at TLSSocket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
i also Allow less secure apps From your Google Account
and also done 2 step-verification
but still got an error
I read all "Similar Questions" here in stackoverflow and I don't know if I need anything else or if I'm doing anything bad
If you have enabled 2-factor authentication on your Google account you can't use your regular password to access Gmail programmatically. You need to generate an app-specific password and use that in place of your actual password.
Steps:
Log in to your Google account
Go to My Account > Sign-in & Security > App Passwords
(Sign in again to confirm it's you)
Scroll down to Select App (in the Password & sign-in method box) and choose Other (custom name)
Give this app password a name, e.g. "nodemailer"
Choose Generate
Copy the long generated password and paste it into your Node.js script instead of your actual Gmail password.
You need to use an application password for this purpose. This issue will arise when 2 Step verification is turned-on for your Gmail account. You can bypass it by using app password. here is how to generate an app password.
Select your profile icon in the upper-right corner of Gmail, then select Manage Google Account.
Select Security in the left sidebar.
Select App passwords under the Signing into Google section. You're then asked to confirm your Gmail login credentials.
Under Select app, choose Mail or Other (Custom name), then select a device.
Select Generate.
Your password appears in a new window. Follow the on-screen instructions to complete the process, then select Done.
google doc : https://support.google.com/mail/answer/185833?hl=en#zippy=%2Cwhy-you-may-need-an-app-password
Generate a password from https://security.google.com/settings/security/apppasswords and use that password instead.
in Gmail, Enable 2-Step-Verification.
Then create App password & use it on SMTP
UPD: You can't do this on localhost. I try. Because it's not a secure connection.
you may still get error if you are doing this on localhost,Because it's not a secure connection(http not https).here is how to solve
for that remove secure: true in the configuration when you create the transporter or comment out it or set it equal to false.

Firebase auth email - prevent fake/invalid accounts

I am using Firebase auth email accounts to sign up users to a site.
What I have noticed lately is the below cases.
Users sign up using a valid email address and then never verify the
email address
Users attempt to sign up using a fake email address
For the first case we can search all accounts that have not been verified within a time span and delete them.
admin.auth().getUser(uid).then(user => {
const creationTime = user.metadata.creationTime
const isVerified = user.emailVerified
const lastSignInTime = user.lastSignInTime
if(!isVerified){
// Process and delete unverified accounts after x days
...
}
})
How can we handle accounts where the email address is fake or misspelled? I am not seeing any property on the firebase.User object that indicates an invalid email address. We do however receive a mail delivery failure message for each user that has signed up using a invalid email address - this is not enough to automatically purge fake / invalid accounts.
What are best practices on preventing fake signups?
Kind regards /K
You can't stop someone from using any string that looks like an email address, and the system doesn't have a way of telling you that the verification email was successfully sent.
The usual way to deal with this is to create some database record for each user account that tracks their validation status. You can query the database to find out which users have not validated after some amount of time. Your app should be sending your backend ID tokens from the user that can be used to check if they are validated, and if so, update the database to show that it happened.
So this is the code I came up with to purge unverified accounts.
May not be the most elegant solution, but works.
exports.scheduledUserCleanup = functions
.region('europe-west1')
.pubsub
.schedule('0 3 * * *')
.timeZone('Europe/Stockholm')
.onRun(async (event) => {
const today = moment()
const inactivityThresholdDays = 7 //Get purge threshold days
let myPromises = [] //Collect all promises to carry out
//Search for users that have NOT validated email
database.ref('user-signups').once('value', (usersSnapshots) => {
usersSnapshots.forEach((snapshot) => {
const uid = snapshot.key
// Get user from firebase auth
admin.auth().getUser(uid)
.then((firebaseAuthUser) => {
const creationTimeStr = firebaseAuthUser.metadata.creationTime
const isVerified = firebaseAuthUser.emailVerified
const lastSignInTimeStr = firebaseAuthUser.metadata.lastSignInTime
const neverSignedIn = (creationTimeStr === lastSignInTimeStr) ? true : false
if(!isVerified && neverSignedIn){
// Process and delete unverified accounts after 7 days
const creationTime = moment(creationTimeStr)
const daysSinceCreation = today.diff(creationTime, 'days')
if(daysSinceCreation > inactivityThresholdDays){
console.log('Remove user from db and Firebase auth', uid)
myPromises.push( admin.auth().deleteUser(firebaseAuthUser.uid) )
myPromises.push( database.ref(`user-signups/${uid}`).remove() )
}else{
console.log(`Keep for ${inactivityThresholdDays} days before deleting`, uid)
}
}
return true
})
.catch((error) => {
// Remove if not found in Firebase Auth
const notFoundInFirebaseAuth = (error.code) ? error.code === 'auth/user-not-found' : false
if(notFoundInFirebaseAuth){
console.log('Remove user from db', uid)
myPromises.push( database.ref(`user-signups/${uid}`).remove() )
}
return false
})
})
})
// Execute promises
return Promise.all(myPromises)
.then(() => Promise.resolve(true))
.catch((err) => {
console.error('Error', err)
return Promise.reject(err)
})
})

DialogFlow with Telegram: How to receive an image and save it along with the conversation

I'm developing a chat bot for Telegram using DialogFlow, but I can't go through two topics, and I can't find the documentation for them.
The flow of the conversation, is the user answer some closed questions and send an image.
How do I get this image?
And to save her along with the other answers?
The answers need to be saved as a form/survey and not as a conversation history.
I have a similar setup in my chatbot. I store the answers in a Firebase database.
In order to interact with the Firestore Database you should implement a Fulfillment
You can see a guide on how to implement Firebase for DialogFlow here
Here you can see a sample of my code. In general lines after setting up the connection to the Firebase database you just want to map your intents to your functions using intentMap.set.
As you said you are using closed answers you can set intets to handle the responses and each "final" intent will trigger a different function that will write a different message to the db.
To write the response to the Firesbase database you just only need to implement admin.database().ref().push().set({}) with the information and the desired structure.
In my example I also store the conversation Id from the chat payload and the date.
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
//const DialogflowApp = require('actions-on-google').DialogflowApp;
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
admin.initializeApp({
credential : admin.credential.applicationDefault(),
databaseURL: 'ws://YOURDATABASE.firebaseio.com/'
});
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
var userId;
let conv = agent.conv();
const ROOTREF = admin.database().ref();
const actions = new Map();
let intentMap = new Map();
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('NoTunel', handleWriteToDbNoTunnel(agent));
agent.handleRequest(intentMap);
function assignConv(agent){
userId = agent.parameters.UserId;
return admin.database().ref('Users/'+ userId).set({
Apellido:"XXXX",
Nombre:"XXXX",
chatId:333,
});}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
var token = "YOUR TOKEN HERE";
var url = "https://api.telegram.org/bot"+ token;
function handleWriteToDbNoTunnel(agent){
const Dia = new Date();
if(matricula !== "")
return admin.database().ref('Limpieza/').push().set({
chatId: request.body.queryResult.outputContexts[3].parameters.telegram_chat_id+'"',
Field1: answer1,
Field2: answer2,
day: day.getTime()
});
}
});
Also if you want to store images with the user responses you can implement the getfile method from the telegram api and store the image code or the image itself
I am adding this answer to slightly improve on Chris32's answer.
There is a better way to get the value of the Telegram Chat ID as I am using it in a personal project.
I will go end to end to explain my approach.
I have mapped some files to some specific intents. In my intent-mapper.js file, I have mapped Default Welcome Intent to welcome.js file as prescribed in the documentation for the Dialogflow Fufillment library for NodeJS (Please note that the library is deprecated and not being updated, personally I am using a fork of the repo that I have worked on personally).
const intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
.
.
Then, in welcome.js,
const globalParameters = {
'name': 'global-parameters',
'lifespan': 9999,
'parameters': {}
};
globalParameters.parameters.telegramChatId = agent.originalRequest?.payload?.data?.chat?.id || -1;
.
.
agent.setContext(globalParameters);
The telegramChatId variable in the global parameters context will save the value for the chat ID which can be passed to a helper function to send a message. In order to to retrieve the value from the global parameters, the code snippet is this.
const globalParameters = agent.getContext('global-parameters');
const telegramChatId = globalParameters.parameters.telegramChatId;
Then the Telegram message helper function is largely the same as in Chris32's answer. The message can be any string and chatId can be passed as an argument to the following helper function.
const TelegramBot = require('node-telegram-bot-api');
const { telegramBotToken } = process.env.TELEGRAM_BOT_TOKEN;
const bot = new TelegramBot(telegramBotToken, { polling: false });
const sendTelegramTextMessage = (message, chatId) => {
try {
bot.sendMessage(chatId, message, {parse_mode: 'html'});
} catch (err) {
console.log('Something went wrong when trying to send a Telegram notification', err);//remove console.log()
}
};
The reason I have put this all in a context since in my use case I am sending push notifications via Telegram once the user asks for it (this happens later in the conversation flow), so I have implemented it this way. The main point to note is that the agent object already has the detectIntentRequest variable saved inside it which in turn has the value we need as a part of its payload. Here's a snippet of the same.
Please note I have removed many lines from my code for brevity, but in a nutshell, the chat ID can be accessed from
agent.originalRequest?.payload?.data?.chat?.id
And the value for the telegram bot token is a secret value which can be saved in an environment variable or Secrets Manager. Please note my answer explains a better way to retrieve the chat ID without needing to refer directly to the request object since Dialogflow Fulfillment library already caches the value in the body for us. The other stuff for receiving and sending images is explained in the main answer.

Dialogflow assistant app exiting after successful fulfillment

I have a dialogflow assistant app with 3 intents. The first intent asks the user for location and name details from google. I am using a webhook for the fulfillment of this intent. I am able to extract the user information name and location, but after it is showing output from webhook, it is exiting from flow. But it is supposed to pass the location parameters to next intent and stay on the flow. Can anybody help me how to stop assistant from exiting?
Here is the webhook code
'use strict';
const functions = require('firebase-functions');
const DialogflowApp = require('actions-on-google').DialogflowApp;
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const requestPermission = (app) => {
app.askForPermissions('To report ', [app.SupportedPermissions.NAME, app.SupportedPermissions.DEVICE_PRECISE_LOCATION]);
};
const userInfo = (app) => {
if (app.isPermissionGranted()) {
const address = app.getDeviceLocation().coordinates;
const name = app.getUserName().givenName;
if (name) {
app.tell(`You are name ${name}`);
}
else {
// Note: Currently, precise locaton only returns lat/lng coordinates on phones and lat/lng coordinates
// and a geocoded address on voice-activated speakers.
// Coarse location only works on voice-activated speakers.
app.tell('Sorry, I could not figure out where you are.Plaese try again');
}
} else {
app.tell('Sorry, I could not figure out where you are.Please try again');
}
};
const app = new DialogflowApp({request, response});
const actions = new Map();
actions.set('request_permission', requestPermission);
actions.set('user_info', userInfo);
app.handleRequest(actions);
});
The problem is that you are calling app.tell() in your code which is a signal to the Assistant to send the message and then end the conversation.
If you want to send the message and then leave the microphone open for the user to reply, you should use app.ask() instead. It takes the same parameters - the only difference is that it expects the user to reply.
So that portion of your code might look something like
if (name) {
app.ask(`You are name ${name}. What would you like to do now?`);
}
(You should make sure that the prompt for the user is one that they will expect to reply. The review process will reject your Action if you reply and it isn't obvious that the user is supposed to reply to you.)

Resources