我想使用 Twilio functions
在接到电话时运行我的操作。
简单的任务: 当我接到 twilio 号码上的电话时,我想转接电话并向 Whatsapp 号码发送消息以通知来电。
Twilio 网站上有一个类似的例子: Improve this question
但我无法让它与 WhatsApp 一起使用。
它仅适用于短信,但是当我用 to
数字替换 from
和 whatsapp:+01234567890
参数时,我没有收到任何消息。
最佳答案
我发布了一种方法,用我的 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);
}
};
关于node.js - Twilio 函数 : Sending WhatsApp message when incoming call is received,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56100810/