我想在接收电话时使用Twilio functions
来运行我的操作。
简单任务:当我在Twilio号码上接到电话时,我想转发电话,还将消息发送给WhatsApp号码以告知即将到来的电话。
Twilio网站上有一个类似的示例:https://support.twilio.com/hc/en-us/articles/360017437774-Combining-voice-voice-sms-sms-sms-and-fax-twiml-in-twiml-in-in-the-same-same-response
,但我无法与WhatsApp一起使用。它仅适用于SMS消息,但是当我用whatsapp:+01234567890
号码替换to
和from
参数时,我没有收到任何消息。
我正在发布一种方法,用我的twilio whatsapp sandbox进行了测试,它有效。
/**
* This Function will forward a call to another phone number.
* It will send a WhatsApp message before doing that.
*/
exports.handler = function (context, event, callback) {
let fromNumber = event.From; // number which called our Twilio number
let recipientNumber = '+10000000001'; // number where the call will be forwarded
let client = context.getTwilioClient();
client.messages
.create({
from: 'whatsapp:+10000000002', // Twilio's WhatsApp sandbox number
body: `Call from ${fromNumber}, forwarded to ${recipientNumber}.`,
to: 'whatsapp:+10000000003' // WhatsApp number registered with sandbox
})
.then(function (message) {
console.log(message.sid);
forwardCall();
});
function forwardCall() {
// generate the TwiML to tell Twilio how to forward this call
let twiml = new Twilio.twiml.VoiceResponse();
let dialParams = {};
twiml.dial(dialParams, recipientNumber);
// return the TwiML
callback(null, twiml);
}
};