The receiver phone number and other useful props are included in the event
object that is part of the callback function:
{
"ToCountry": "US",
"ToState": "CA",
"SmsMessageSid": "SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"NumMedia": "0",
"ToCity": "BOULEVARD",
"FromZip": "",
"SmsSid": "SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"FromState": "WA",
"SmsStatus": "received",
"FromCity": "",
"Body": "Ahoy!",
"FromCountry": "US",
"To": "+15555555555",
"ToZip": "91934",
"NumSegments": "1",
"MessageSid": "SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"AccountSid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"From": "+14444444444",
"ApiVersion": "2010-04-01",
"request": {
"headers": { ... },
"cookies": { ... }
},
}
exports.handler = (context, event, callback) => {
// Access the pre-initialized Twilio REST client
const twilioClient = context.getTwilioClient();
// Determine message details from the incoming event, with fallback values
const from = event.From || '+15095550100';
const to = event.To || '+15105550101';
const body = event.Body || 'Ahoy, World!';
twilioClient.messages
.create({ to, from, body })
.then((result) => {
console.log('Created message using callback');
console.log(result.sid);
return callback();
})
.catch((error) => {
console.error(error);
return callback(error);
});
};