我正在尝试通过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" });
});
});
答案 0 :(得分:1)
您很有可能正在使用旧版本的Twilio的Node.js库,但不支持Studio流程。当前版本为3.39.1
如果您在package.json文件中阅读了“ dependencies”,则可以找到正在使用的版本。
此外,如果您在项目根文件夹中打开终端并运行npm outdated
,则该表中可能会看到twilio颜色为红色。
也许还有其他方法可以做到,但是要获得Twilio软件包的最新版本,我需要在项目的根文件夹中打开一个终端,然后
npm uninstall twilio --save
npm install twilio --save
之后,再次使用npm list --depth=0
检查一下,希望您会获得-- twilio@3.39.1
,它支持Studio流程。
答案 1 :(得分:0)
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));