我想在收到电话时使用Twilio functions
来执行操作。
简单任务: 当我收到有关twilio号码的电话时,我想转接该电话,并且还向whatsapp号发送一条消息,以通知有关来话的电话。
Twilio网站上有一个类似的示例: https://support.twilio.com/hc/en-us/articles/360017437774-Combining-Voice-SMS-and-Fax-TwiML-in-the-Same-Response
但是我无法将其与WhatsApp配合使用。
它仅适用于SMS消息,但是当我用to
号替换from
和whatsapp:+01234567890
参数时,我没有收到任何消息。
答案 0 :(得分:1)
我发布了一种方法,并用我的Twilio WhatsApp沙盒进行了测试,
/**
* 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);
}
};