Twilio功能:收到来电时发送WhatsApp消息

时间:2019-05-12 15:38:57

标签: node.js twilio whatsapp twilio-functions

我想在收到电话时使用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号替换fromwhatsapp:+01234567890参数时,我没有收到任何消息。

1 个答案:

答案 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);
    }

};