【问题标题】:Twilio: How to initiate a flow with an API callTwilio:如何使用 API 调用启动流程
【发布时间】:2019-12-27 02:33:15
【问题描述】:

我正在尝试通过 Node 中的 API 调用在 Twilio 上启动一个流程,但我似乎无法让它工作。该流程旨在在发送 API 请求时进行出站调用。尝试了几个我在网上看到的代码示例无济于事。我收到类似Cannot read property X of undefined 的错误(见下文),但问题是我能够通过 API 调用向我的手机发起电话呼叫(不是流程,只是一个呼叫),所以我知道Twilio 客户端已连接。

作品:

app.post('/call', (req, res) => {
  client.calls
  .create({
     url: 'https://handler.twilio.com/twiml/PNxxxxxxxxxxxxxxxxxxxx',
     to: '+1708xxxxxxx',
     from: '+1312xxxxxxx'
   })
  .then((call, err) => {
      if (err) { console.log(err) }
      res.json({ success: "success" });
    });
  });

不工作:触发器Cannot read property 'v1' of undefined

app.post('/flow', (req, res) => {

    client.studio.v1.flows('FWxxxxxxxxxxxxxxxxxxxx')
    .fetch()
    .then(flow => console.log("flow : ", flow));

不工作:触发器Cannot read property 'flows' of undefined

app.post('/flow', (req, res) => {
  client.studio.flows('FWxxxxxxxxxxxxxxxxxxxx')
  .executions
  .create({
    to: '+1847xxxxxxx',
    from: '+1312xxxxxxx'
  })
  .then(function(execution) { console.log("sid : ", execution.sid); });
});

不工作:没有错误,什么也没发生

app.post('/flow', (req, res) => {

  client.calls
  .create({
    url: 'https://studio.twilio.com/v1/Flows/FWxxxxxxxxxxxxxxxxxxxx/Executions',
    to: '+1847xxxxxxx',
    from: '+1312xxxxxxx'
  })
  .then((call, err) => {
    if (err) { console.log("err : ", err) }
    if (call) { console.log("call : ", call)}
    res.json({ success: "success" });
  });
});

【问题讨论】:

    标签: node.js twilio twilio-studio


    【解决方案1】:

    您很可能使用的是旧版本的 Twilio 的 Node.js 库,不支持 Studio 流。当前版本为3.39.1

    如果您阅读 package.json 文件中的“依赖项”,您可以找到您正在使用的版本。

    此外,如果您在项目根文件夹中打开一个终端并运行 npm outdated,您可能会在该表中看到 twilio 为红色。


    如何解决:

    可能还有其他方法可以做到这一点,但要为 Twilio 的包获取最新版本,我会在项目的根文件夹中打开一个终端并

    • 运行npm uninstall twilio --save
    • 然后运行npm install twilio --save

    之后再次检查 npm list --depth=0,希望您会得到支持 Studio 流程的 -- twilio@3.39.1

    【讨论】:

    • 同样奇怪的是,在我的package.json 中我有"twilio": "^4.6.0",但不得不退回到3.39.1,但它至少可以工作
    • 在没有看到您的流程的情况下,您可能在通话后在流程中发出 http 请求。尝试使用仅拨打语音电话的最小流程,然后逐步添加更多内容以找出问题所在。
    【解决方案2】:

    https://www.twilio.com/docs/libraries/node

    我会将 twilio 安装到您的项目中。

    从控制台 - 这会将包添加到您的 package.json 并下载模块 npm install --save twilio

    
    var accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; // Your Account SID from www.twilio.com/console
    var authToken = 'your_auth_token';   // Your Auth Token from www.twilio.com/console
    
    var twilio = require('twilio');
    var client = new twilio(accountSid, authToken);
    
    client.messages.create({
        body: 'Hello from Node',
        to: '+12345678901',  // Text this number
        from: '+12345678901' // From a valid Twilio number
    })
    .then((message) => console.log(message.sid));```
    
    
    when using the twilio node package they also have info on making calls with it
    https://www.twilio.com/docs/voice/quickstart/node
    
    
    // Download the helper library from https://www.twilio.com/docs/node/install
    // Your Account Sid and Auth Token from twilio.com/console
    // DANGER! This is insecure. See http://twil.io/secure
    const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
    const authToken = 'your_auth_token';
    const client = require('twilio')(accountSid, authToken);
    
    client.calls
          .create({
             url: 'http://demo.twilio.com/docs/voice.xml',
             to: '+123456789',
             from: '+987654321'
           })
          .then(call => console.log(call.sid));
    
    
    

    【讨论】:

    • 已经安装在我的项目中,这就是/call url 的工作方式。但/flow 不是。
    • studio.v1 是从哪里来的?我在他们的文档中找不到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多