首页 > 解决方案 > 自动驾驶任务中使用的 Twilio 功能应发送 SMS 但返回“发生应用程序错误”

问题描述

(输入后的第二个更新版本 - 有进展 - 谢谢)我想在通话期间使用 Twilio 功能发送短信(以避免制作完整的外部应用程序)。目标是在自动呼叫结束时发送确认短信(使用 Twilio 自动驾驶仪)。如果用户说出正确的句子(“我想通过短信确认”),自动驾驶仪会启动以下任务。

{
    "actions": [
        {
            "redirect": "https://my_domain_here.twil.io/my_url_here"
        }
    ]
}

然后我的函数有以下代码:

    exports.handler = function(context, event, callback) {
    var client = context.getTwilioClient();
/**to have some view on incoming request    */
    console.log("context : "+JSON.stringify(context));
    console.log("event : "+JSON.stringify(event));
//send the answer SMS
    console.log("sending SMS");
client.messages
  .create({
    body: 'test sms',
    from: '+32x_my_nbr_xx',
    to: '+32x_my_other_nbr_xx'//is hardcoded - just to test
  })
.then(message => console.log(message.sid))
.done();
//now create the Twiml to send back to autopilot the text to be spoken
    console.log("SMS sent, returning to autopilot");
    var action = {"actions": [{"say": "I understand that you want a confirmation. I will send you a summary by SMS. Goodbye" }]};
    callback(null, action);
}

但是当我打电话说“我想通过短信确认”时,我会听到“我明白你想要确认”。我将通过短信向您发送摘要。再见”。但是没有发送短信。当我查看自动驾驶仪的日志时,触发了正确的意图。该功能的日志不包含任何内容(只是常规日志,而不是 Msgid)有人知道吗?

我知道它会起作用,但真的没有办法避免编写和维护一个完整的后端只是为了发送这个 SMS 吗?提前谢谢。

标签: twiliotwilio-functions

解决方案


Twilio 开发人员布道者在这里。

我怀疑 Autopilot 发出的请求是语音请求,这意味着您将无法返回 MessageResponse,因为它仅适用于来自传入 SMS 消息的请求。

要在该函数中发送文本,您需要使用 Node 助手库来调用 REST API:

client.messages
  .create({
    body: 'This is the ship that made the Kessel Run in fourteen parsecs?',
    from: '+15017122661',
    to: '+15558675310'
  })
.then(message => console.log(message.sid))
.done();

希望有帮助。


推荐阅读