1

我有想要发送到TwiML 重定向小部件的 Json 值。Json 来自 HTTP 请求。我看到除了 Method Get 和 Post,还有一个 Method for Liquid Variable Input,这可以是我的解决方案吗?如果是,它是如何工作的?我没有运气设置这个选项。

我来自(GetAccountsByPhoneNumber)的 Json:

{"Accounts": [
      {"AccountNumber": "9999999998", "HouseNumber": "3207", "StreetName": "Stokesberry ln"},
      {"AccountNumber": "9999999997", "HouseNumber": "1204", "StreetName": "S Hardneir Rd"},
      {"AccountNumber": "9999999996", "HouseNumber": "533", "StreetName": "Park Street"},
      {"AccountNumber": "9999999995", "HouseNumber": "926", "StreetName": "S CO RD 67"}
]}

Twiml 函数提示选择他们调用的帐户。

    exports.handler = function(context, event, callback) {
        const twiml = new Twilio.twiml.VoiceResponse();
//This is Where I need to get Access to my JSON Object, 
//it works with Function, but does not return to studio flow.
        var responseMany = JSON.parse(event.IncomingJson);
        var gather = twiml.gather({
            input: 'dtmf speech',
            timeout: 5,
            hints: '1,2,3,4,5,9',
            numDigits: 1
        });
        gather.say("If you are calling about ");
        gather.say({voice: 'Polly.Joanna'}).ssmlSayAs({'interpret-as': 'address'}, responseMany.Accounts[0].HouseNumber + " " + responseMany.Accounts[0].StreetName);
        gather.say("Press or say one.");

        gather.say("If you are calling about ");
        gather.say({voice: 'Polly.Joanna'}).ssmlSayAs({'interpret-as': 'address'}, responseMany.Accounts[1].HouseNumber + " " + responseMany.Accounts[1].StreetName);
        gather.say("Press or say two.");

        if(responseMany.Accounts.length >= 3){
            gather.say("If you are calling about ");
            gather.say({voice: 'Polly.Joanna'}).ssmlSayAs({'interpret-as': 'address'}, responseMany.Accounts[2].HouseNumber + " " + responseMany.Accounts[2].StreetName);
            gather.say("Press or say three.");
        }
          if(responseMany.Accounts.length >= 4){
            gather.say("If you are calling about ");
            gather.say({voice: 'Polly.Joanna'}).ssmlSayAs({'interpret-as': 'address'}, responseMany.Accounts[3].HouseNumber + " " + responseMany.Accounts[3].StreetName);
            gather.say("Press or say four.");
        }  
      twiml.redirect("https://webhooks.twilio.com/v1/Accounts/.../Flows/...?FlowEvent=return")
        callback(null, twiml);
    };

作为一个函数,它在twiml.redirect上崩溃并且永远不会重新进入流程。

4

1 回答 1

0

你在这里有几个问题。

首先,方法的 Liquid 变量输入选项只是一种在您选择GETPOST在运行时传递的方式。要在 TwiML Redirect 小部件的输入和输出中发送数据,您必须专门使用附加到 URL 字符串的 GET 参数。

这对于复杂的 JSON 文档并不理想,特别是因为 URI 的大小限制可能比 HTTP 正文短,但假设您的数据传递需求不是那么极端,您可以通过在其中发送 HTTP 请求响应来完成此操作序列化(未解析)的形式直接作为 get 参数输入到函数中。Twilio 应该处理 URL 编码。您的 TwiML 重定向小部件配置应如下所示:

Method: GET
URL: https://example.twil.io/func?IncomingJson={{widgets.HTML_REQUEST_WIDGET_NAME.body}}

您的另一个问题是您没有处理<Gather>动词的响应。Gather 将向Digits它发送到action属性 URL 的任何请求附加一个属性,默认情况下,该 URL 与生成 TwiML 文档的 URL 相同。通过执行以下操作,您可能可以在一次调用中完成所有这些(并避免twiml.redirect完全需要):

const gather = twiml.gather({
    input: 'dtmf speech',
    timeout: 5,
    hints: '1,2,3,4,5,9',
    numDigits: 1,
    method: 'GET',
    action: context.FLOW_WEBHOOK_URL + '?FlowEvent=return'
    // Use an environment variable for the Webhook URL
});

当 Twilio 触发操作 URL 时,它将&Digits=1(例如)附加到 URL,然后可以在您的流中作为流动变量访问该 URL {{widgets.TWIML_REDIRECT_WIDGET_NAME.Digits}}:.

于 2021-01-14T07:19:15.903 回答