Twilio programmable SMS not sending in deployed Lambda function - node.js

I am working on a Serverless AWS service that uses Twilio's programmable SMS to deliver text messages.
My setup is consistently delivering messages successfully when I run the stack locally (e.g. sls offline start), but in the deployed environment I seem to be unable to even call the method on the Twilio client.
Here's how the message delivery is set up:
const twilio = require('twilio');
const twilioClient = twilio(
process.env.TWILIO_SID,
process.env.TWILIO_TOKEN,
{
lazyLoading: true,
}
);
export function sendMessage(user, message) {
twilioClient.messages.create({
from: process.env.TWILIO_NUMBER,
to: user.phone,
body: message,
}, function(err, message) {
console.log('error', err);
console.log('message', message);
});
}
// And then usage in a Serverless Function Handler
function example(event, context, callback) {
context.callbackWaitsForEmptyEventLoop = false;
// user is also determined here
sendMessage(user, 'This is a message');
return {
body: JSON.stringify({}),
statusCode: 200
};
}
Locally, running this works and I am able to see the output of the message log, with nothing on the error log. However, when deployed, running this yields nothing -- the method appears to not even get called (and I can verify in the Twilio logs that no API call was made), therefor no error or message logs are made in the callback.
In debugging I've tried the following:
I've logged all the environment variables (Twilio SSID, auth token, phone number), as well as the function arguments, and they all appear to be in place. I also checked out the Lambda function itself to make sure the environment variables exist.
I've examined my CloudWatch logs; no errors or exceptions are logged. Other than the Twilio method not getting called the Lambda function is executed without issue.
I've tried logging things like twilio and twilioClient.messages.create to make sure the client and function definition didn't get wiped out somehow.
I thought maybe it had to do with context.callbackWaitsForEmptyEventLoop so I changing it from false to true.
I have come up empty, I can't figure out why this would be working locally, but not when deployed.
Edit: per the Twilio client example, if you omit the callback function the method will return a Promise. I went ahead and tried to await the response of the method:
export function sendMessage(user, message) {
return twilioClient.messages.create({
from: process.env.TWILIO_NUMBER!,
to: user.phone,
body: message,
});
}
// Usage...
async function example(event, context, callback) {
context.callbackWaitsForEmptyEventLoop = false;
try {
const message = await sendMessage(user, 'This is a message');
console.log('message', message)
} catch (error) {
console.log('error', error);
}
return {
body: JSON.stringify({}),
statusCode: 200
};
}
In this example the Lambda function is successful, but neither the message nor the error are logged.

I tried this and it works. I tried to make my code as similar to use, with a few changes.
const twilio = require('twilio');
const twilioClient = twilio(
process.env.TWILIO_SID,
process.env.TWILIO_TOKEN
);
let user = '+14075551212';
function sendMessage(user, message) {
return twilioClient.messages.create({
from: process.env.TWILIO_NUMBER,
to: user,
body: message,
});
}
exports.handler = async function(event, context, callback) {
try {
const message = await sendMessage(user, 'This is a message');
console.log('message', message);
callback(null, {result: 'success'});
} catch (error) {
console.log('error', error);
callback("error");
}
};

Related

Callback ignored issue in node.js running on AWS Lambda

I have this code in node.js (nodejs10.x) running in AWS Lambda.
module.exports.register = (event, context, callback) => {
// Body es un json por lo que hay que deserializarlo
let body = JSON.parse(event.body);
let rules = {
'name': 'required|min:3',
'family_name': 'required|min:3',
'email': 'required|email',
'curp': 'required|size:18',
'modules': 'required',
'password': 'required'
};
let validation = new validator(body, rules);
// If errors this validation exits using the callback
if(validation.fails()){
console.log(validation.errors.all())
const response = {
statusCode: 422,
body: JSON.stringify(validation.errors.all())
};
callback(null, response);
}
// just for testing
const isModulesValid = false;
if(!isModulesValid){
console.log('Modules validation failed. ')
const response = {
statusCode: 422,
body: JSON.stringify({'modules': 'Invalid modules string. '})
};
callback(null, response);
// However this is not working
}
// and even if there is an error this code is executed
console.log('XXXX');
I am testing it locally with a code like this.
// Callback
let callback = function(err, result) {
if (err)
console.log(err);
if (result)
console.log(result);
// Terminate execution once done
process.exit(0);
}
// lambda.generateToken(event, context, callback);
lambda.register(event, context, callback);
Locally if isModulesValid = false the code exits and the console.log('XXXX') is not executed. However when running it in AWS Lambda, even if the validation fails the code continues to run and the console.log() is executed.
I cannot figure out whats going on. Help please?
Locally you are using the callback which has process.exit(0); which is making the process finish and hence the next line is not executing. Callback doesn't mean that the code afterwards wouldn't be executed. the code flow continues after that as well. It all depends on what you have in your callback.
This piece of code solved the problem:
if(!isModulesValid){
console.log('Modules validation failed. ')
const response = {
statusCode: 422,
body: JSON.stringify({'modules': 'Invalid modules string. '})
};
// Return callback explcitly
return callback(null, response);
}
Apparently it has to do with how AWS Lambda handles the task queue. I found a good explanation here: https://blog.danillouz.dev/aws-lambda-and-the-nodejs-event-loop/

Twilio messages not firing on cron lamba

I've been working on this issue for a few hours with no success. I am triggering the below function to run every 15 minutes. I can see from the CloudWatch that the function is firing. From my logs, I know that the function successfully getting appointments that require notification be sent. I am using 2 try/catch blocks to try and get some error message coming through, but no errors are being logged.
When I run this function using sls invoke local, it works fine and sends the expected text message to the correct numbers. However, when deployed and run on a cron-basis, its not working but also not erroring out - so I am at a loss.
I think it's an async/await issue. Basically, the console logs show up in the Cloudwatch logs, but nothing inside the twilioClient.messages.create function comes out in the logs.
Any help would be appreciated - I am sure it's something simple, but been staring at this for a few hours now without any success!
function sendNotifications(appointments) {
console.log('require notifications', appointments.length);
appointments.forEach(function(appointment) {
// Create options to send the message
// Appointments show in the logs
console.log('appt', appointment);
console.log('from', process.env.TWILIO_PHONE_NUMBER);
try {
const options = {
to: `${appointment.meta.contact.number}`,
from: process.env.TWILIO_PHONE_NUMBER,
/* eslint-disable max-len */
body: `Hi ${appointment.meta.contact.name}. Just a reminder that you have an property viewing for ${appointment.meta.property.name} at ${appointment.date}, ${appointment.time}. Please reply with CONFIRM to confirm that you'll be attending this viewing or CANCEL BOOKING to cancel this viewing.`
/* eslint-enable max-len */
};
// Send the message! - this log displays
console.log('about to send message');
twilioClient.messages.create(options, function(err, response, callback) {
// Nothing in this block gets printed to the logs
if (err) {
// Just log it for now
console.log('ERROR', err);
} else {
// Log the last few digits of a phone number
let masked = appointment.meta.contact.number.substr(
0,
appointment.meta.contact.number.length - 5
);
masked += '*****';
console.log(`Message sent to ${masked}`);
try {
updateBooking({ booking: appointment, message: response });
} catch (e) {
console.log(e.message);
}
}
// Don't wait on success/failure, just indicate all messages have been
// queued for delivery
if (callback) {
callback.call();
}
});
} catch (e) {
console.log('ERR', e.message, appointment);
}
});
}
From the doc https://www.twilio.com/docs/sms/quickstart/node
twilioClient.messages.create does not have a callback params, but returns a Promise
Try to execute the Lambda with:
twilioClient.messages
.create({
body: 'just for test',
from: 'a verified number',
to: 'a verified number'
})
.then(message => console.log(message.sid));
If this works, then that was the issue.
Otherwise could be process.env.TWILIO_PHONE_NUMBER is not set correctly or ${appointment.meta.contact.number} may be missing or invalid. Maybe print those also.
Twilio could also not have been correctly set up. Make sure accountSid and authToken are set.

AWS SES create template with lambda function always return null

so on my first time learning AWS stuff (it is a beast), I'm trying to create e-mail templates, I have this lambda function:
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({ region: "us-east-1" });
exports.handler = async (event, context, callback) => {
// Create createTemplate params
var params = {
Template: {
TemplateName: "notification" /* required */,
HtmlPart: "HTML_CONTENT",
SubjectPart: "SUBJECT_LINE",
TextPart: "sending emails with aws lambda"
}
};
// Create the promise and SES service object
const templatePromise = new AWS.SES({ apiVersion: "2010-12-01" })
.createTemplate(params)
.promise();
// Handle promise's fulfilled/rejected states
templatePromise
.then((data) => {
console.log(data);
callback(null, JSON.stringify(data) );
// also tried callback(null, data);
}, (err) => {
console.error(err, err.stack);
callback(JSON.stringify(err) );
});
as far as I am understanding, this function should return me a template? an object, anything? when I use the lambda test functionality I always got null in the request response
does anyone know what I am doing wrong here?
edit: and It is not creating the e-mail template, I check the SES Panel - email templates and it is empty
edit2: if I try to return a string eg: callback(null, "some success message"); it does return the string, so my guess is something wrong with the SES, but this function is exactly what we have in the AWS docs, so I assume it should just work..
Try not to resolve the Promise and change your code to just returning it as-is:
return await templatePromise;
which should present you some more detail of what is really going wrong in your code - it might be some hidden access issue - so you might need to adjust the role your lambda function is using. createTemplate on the other side should not return much in case of successful execution but just create the template.
Also try to follow the following try/catch pattern when using async (as described here in more detail: https://aws.amazon.com/de/blogs/compute/node-js-8-10-runtime-now-available-in-aws-lambda/)
exports.handler = async (event) => {
try {
data = await lambda.getAccountSettings().promise();
}
catch (err) {
console.log(err);
return err;
}
return data;
};

Async code inside node uncaughtException handler

I have a node app (version 8.11.1 using Typescript 2.8.1) that catches uncaught exceptions using the uncaughtException hook like this:
process.on('uncaughtException', (err) => {
await sendEmail(recipient, subject, body);
});
I'm calling an asynchronous method inside the handler to send out an email and the code isn't working. It appears that the node process dies before the async call can finish and I never receive the email.
It looks like you may not be able to successfully use async methods inside this handler. The documentation doesn't say that outright but it implies it stating
The correct use of 'uncaughtException' is to perform synchronous
cleanup of allocated resources
I'm not trying resume operation or do anything funky. All I want to do is send out and email stating that the system crashed. I am not able to find any libraries that will synchronously send emails so I'm at a bit of a loss on how to handle this.
I've seen one suggestion to synchronously write the data to a file (or database) and have some outside processing polling for the existence of that file and sending the email if it exists. I suppose that would work but it's pretty hacky. Any better solutions?
Update:
Okay well after running some more tests it looks like you can actually run async code from inside the uncaughtException handler just fine. The following works:
const mailer = require('nodemailer');
process.on('uncaughtException', async err => {
const transport = mailer.createTransport({
service: 'gmail',
auth: {
user: 'someone#email.com',
pass: 'abc'
}
});
const mailOptions = {
from: 'someone#email.com',
to: 'someone.else#email.com',
subject: 'hi',
text: 'there'
};
transport.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
}
else {
console.log(info);
}
});
});
throw new Error('boom');
The above code works fine as a standalone app, but if I copy it into my codebase, it doesn't work. The code executes but I don't get an email (presumably the app dies before it can finish sending). So there must be something else going on in my environment that is preventing it from working. I'll keep digging.
I don't know what library you use to send an email and what version of node js you use but if you are using node js version greater than 7 you can use async/await and send an email as below
var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain});
process.on('uncaughtException', async (err) => {
var data = {
from: 'User <me#samples.mailgun.org>',
to: 'serobnic#mail.ru',
subject: 'ERROR MESSAGE',
text: `Caught exception: ${err}\n`,
};
var body = await mailgun.messages().send(data);
console.log(body);
});
// using callback - supported for all versions
process.on('uncaughtException', (err) => {
var data = {
from: 'User <me#samples.mailgun.org>',
to: 'serobnic#mail.ru',
subject: 'ERROR MESSAGE',
text: 'Caught exception:' + err,
};
mailgun.messages().send(data, function (err, body) {
console.log(body);
});
});

DialogFlow V2 Webhook - Expects Speech responses Immediately and not after async requests

I have a DialogFlow V2 node.js webhook.
I have an intent that is called with a webhook action:
const { WebhookClient } = require('dialogflow-fulfillment');
const app = new WebhookClient({request: req, response: res});
function exampleIntent(app) {
app.add("Speak This Out on Google Home!"); // this speaks out fine. no error.
}
Now, if I have an async request which finishes successfully, and I do app.add in the success block like this:
function exampleIntent(app) {
myClient.someAsyncCall(function(result, err) {
app.add("This will not be spoken out"); // no dice :(
}
// app.add("but here it works... so it expects it immediately");
}
... then Dialog Flow does not wait for the speech to be returned. I get the error in the Response object:
"message": "Failed to parse Dialogflow response into AppResponse, exception thrown with message: Empty speech response",
How can I make DialogFlow V2 wait for the Webhook's Async operations to complete instead expecting a speech response immediately?
NOTE: This problem only started happening in V2. In V1, app.ask worked fine at the tail-end of async calls.
exampleIntent is being called by the main mapper of the application like this:
let actionMap = new Map();
actionMap.set("my V2 intent name", exampleIntent);
app.handleRequest(actionMap);
And my async request inside myClient.someAsyncCall is using Promises:
exports.someAsyncCall = function someAsyncCall(callback) {
var apigClient = getAWSClient(); // uses aws-api-gateway-client
apigClient.invokeApi(params, pathTemplate, method, additionalParams, body)
.then(function(result){
var result = result.data;
var message = result['message'];
console.log('SUCCESS: ' + message);
callback(message, null); // this succeeds and calls back fine.
}).catch( function(error){
console.log('ERROR: ' + error);
callback(error, null);
});
};
The reason it worked in V1 is that ask() would actually send the request.
With V2, you can call add() multiple times to send everything to the user in the same reply. So it needs to know when it should send the message. It does this as part of dealing with the response from your handler.
If your handler is synchronous, it sends the reply immediately.
If your handler is asynchronous, however, it assumes that you are returning a Promise and waits till that Promise resolves before sending the reply. So to deal with your async call, you need to return a Promise.
Since your call is using Promises already, then you're in very good shape! The important part is that you also return a Promise and work with it. So something like this might be your async call (which returns a Promise):
exports.someAsyncCall = function someAsyncCall() {
var apigClient = getAWSClient(); // uses aws-api-gateway-client
return apigClient.invokeApi(params, pathTemplate, method, additionalParams, body)
.then(function(result){
var result = result.data;
var message = result['message'];
console.log('SUCCESS: ' + message);
return Promise.resolve( message );
}).catch( function(error){
console.log('ERROR: ' + error);
return Promise.reject( error );
});
};
and then your Intent handler would be something like
function exampleIntent(app) {
return myClient.someAsyncCall()
.then( function( message ){
app.add("You should hear this message": message);
return Promise.resolve();
})
.catch( function( err ){
app.add("Uh oh, something happened.");
return Promise.resolve(); // Don't reject again, or it might not send the reply
})
}

Resources