我发布了一种方法,用我的 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);
}
};