Twilio callback Error using serverless Whatsapp using NodeJS

Viewed 56

I am following a tutorial by Brent Schooley of Twilio who showed how to create a serverless Whatsapp service using NodeJS that queries a value from Google Sheets (his source code). My dependencies seem correct, and the deployment happens correctly my sandbox is configured correctly with the generated webhook but I'm getting a callback error and it says I need to put it in the right place (error 82002). I'm using this as a project and new to NodeJS after following their single message tutorials. Here is my code:

const { GoogleSpreadsheet} = require('google-spreadsheet');

let fs = require('fs');
const { Twilio } = require('twilio');
const { error } = require('console');
const Response = require('twilio/lib/http/response');
let credsFile = Runtime.getAssets()['/creds.json'].path;
let creds = JSON.parse(fs.readFileSync(credsFile, 'utf-8'));

async function getName(name) {
    const doc = new GoogleSpreadsheet('where I enter the Google Sheet ID');
    await doc.useServiceAccountAuth(creds);
    await doc.loadInfo();

    const krishnaNamesSheet = doc.sheetsByIndex[0];

    const krishaNames = await krishnaNamesSheet.getRows();

    const krishna = krishaNames.find(row => row.Name.toLowerCase() === name.toLowerCase());

    return krishna;

}

exports.handler = async function(context, event, callback) {
    const twiml = new Twilio.twiml.MessagingResponse();
    const name = event.Body.trim();
    console.log(name);

    const krishna = await getName(name);

    if(krishna) {
        twiml.message(`The meaning of the Krishna name ${event.Body} is: ${krishna.Meaning}`);
    } else {
        twiml.message(`I'm sorry, I don't know about that name yet!`);
    
    }

    callback(null, twiml);
};

What's wrong with this? I looked at other examples like this:

if(krishna) {
        return callback(null, twiml.message(`The meaning of the Krishna name ${event.Body} is: ${krishna.Meaning}`));
    } else {
        return callback(new Error(return twiml.message(`I'm sorry, I don't know about that name yet!`));
    
    }
};

But didn't work. What is the right way to prevent this error. Here is error response:

Error - 82002 Error on Twilio Function response Your Function invocation resulted in StatusCode 5xx. Possible Causes Your Function timed out before responding Your Function returned an error response Possible Solutions Your Function must contain a callback. Make sure you place the Function callback callback(err, response) is placed correctly in your Function code. If you are using a JavaScript promise, make sure the callback is called in both success and catch blocks. Your Function responded with an error.

Raw error response body:

UnhandledPromiseRejectionWarning: Unhandled promise rejection: TypeError: Cannot read property 'MessagingResponse' of undefined at Object.exports.handler (/var/task/handlers/ZNd211e21df8b5046306e730661731dc38.js:26:36) at Object.exports.handler (/var/task/node_modules/runtime-handler/index.js:339:10) at Runtime.exports.handler (/var/task/runtime-handler.js:17:17) at Runtime.handleOnceNonStreaming (/var/runtime/Runtime.js:73:25)

1 Answers

I think the issue is the way you require Twilio. You have:

const { Twilio } = require('twilio');

But it should just be:

const Twilio = require('twilio');
Related