I have a Twilio function, as below; it forwards the SMS to an email address. In the phone number's configuration, I am calling this function when 'A message comes in'. There is no other component involved - no callback, no Messaging Service, no Studio, nada.
const got = require('got');
exports.handler = function(context, event, callback) {
console.log(event.to);
const requestBody = {
subject: `sms to [${event.to}, from ${event.From}`,
//build rest of JSON request
}
got.post('https://api.mailclient.com/v3/mail/send', {
headers: {
'Authorization': `Bearer ${context.DEV__API_TOKEN}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(requestBody)
})
.then(response => {
let twiml = new Twilio.twiml.MessagingResponse();
callback(null, twiml);
})
.catch(err => {
console.error(err);
callback(err, 'error');
});
};
I am testing the setup by sending an SMS from a mobile phone. Of course the email goes through, but the ${event.to} always turns up as 'undefined'. I am not sure what am I missing. Any pointers, please?
Twilio developer evangelist here.
In the incoming request the parameters are all capitalised values. You are already correctly using event.From
, so you just need to change event.to
to event.To
and it will no longer be undefined
.