【问题标题】:Twilio functions: Sending WhatsApp message when incoming call is received [closed]Twilio 功能:收到来电时发送 WhatsApp 消息 [关闭]
【发布时间】:2023-03-29 15:41:01
【问题描述】:

我想在接到电话时使用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 数字时,我没有收到任何消息。

【问题讨论】:

  • tofrom 的号码有什么用? from 应该是 Twilio 的 Whatsapp 号码。
  • 对于to,我使用的是我在沙盒中注册的手机号码。对于from,我使用的是 Twilio Sandbox Whatsapp 号码。我想我会在结合这两个功能之前解决问题并尝试让 Whatsapp 的东西正常工作。

标签: node.js twilio whatsapp twilio-functions


【解决方案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-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2022-09-30
    • 2019-09-26
    相关资源
    最近更新 更多