I have created a function in Twilio to handle incoming messages and send them to DialogFlow. However I do not see any way to get the phone number of the Twilio (receiver phone number). Is there any way to do it?
It is not an option to hardcode it in the environment variables or in the code, cuz the function may handle requests to different numbers.
exports.handler = function(context, event, callback) {
from_number = event.phone
//how to get the receiver phone number (the number which belongs to Twilio
}
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);
});
};