1

我想在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 消息,但是当我用数字替换tofrom参数时whatsapp:+01234567890,我没有收到任何消息。

4

1 回答 1

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

};

于 2019-05-12T20:04:58.730 回答