Node.js和twilio集成(Node.js and twilio integration)

编程入门 行业动态 更新时间:2024-10-28 11:21:41
Node.js和twilio集成(Node.js and twilio integration)

我正在尝试将twilio与Node.js + express集成。

我还没有网站。 我应该为HOSTNAME提供什么值,以及SID和AUTH_TOKEN,这些值来自twilio网站。

我已经编写了一些代码,无论下面给出了我在twiclient.js中的views文件夹中给出的建议,我在app.js中添加了一个路由来重定向请求如果调用了/ twi,但是我没有得到任何结果。 控制台中出现了一些错误,请你帮我弄清楚我做错了什么? 我已经放置了正确的SID,令牌和主机名,如下所示。

app.js有以下条目,还需要做什么才能让twilio调用部分工作吗?

另外,我应该在哪里定义用于在views文件夹中呼叫电话的GUI?

var TwilioClient = require('twilio').Client,       Twiml = require('twilio').Twiml,       sys = require('sys'); var client = new TwilioClient('MY_ACCOUNT_SID', 'MY_AUTH_TOKEN', 'MY_HOSTNAME'); var phone = client.getPhoneNumber('+2323232323'); phone.setup(function() { phone.makeCall('+15555555555', null, function(call) {}); phone.setup(function() {     phone.makeCall('+15555555555', null, function(call) {         call.on('answered', function(callParams, response) {             response.append(new Twiml.Say('Hey buddy. Let\'s meet for drinks later tonight.'));             response.send();         });     }); });

I am trying to integrate twilio with Node.js+express.

I don't have a site yet. what value should I give for HOSTNAME, along with SID and AUTH_TOKEN, these values I got from twilio site.

I have written some code, whatever suggestion given below I have placed in to views folder in twiclient.js , I have added a route in app.js to redirect the request if /twi is called , but I am not getting any result. some errors are appearing in the console, would you please help me figure out what I'm doing wrong? I have placed the correct SID, token and hostname, as specified below.

app.js has the following entry, does anything else need to be done for the twilio calling part to work?

Also, where should I define the GUI for calling a phone in the views folder?

var TwilioClient = require('twilio').Client,       Twiml = require('twilio').Twiml,       sys = require('sys'); var client = new TwilioClient('MY_ACCOUNT_SID', 'MY_AUTH_TOKEN', 'MY_HOSTNAME'); var phone = client.getPhoneNumber('+2323232323'); phone.setup(function() { phone.makeCall('+15555555555', null, function(call) {}); phone.setup(function() {     phone.makeCall('+15555555555', null, function(call) {         call.on('answered', function(callParams, response) {             response.append(new Twiml.Say('Hey buddy. Let\'s meet for drinks later tonight.'));             response.send();         });     }); });

最满意答案

主机名是'api.twilio.com'。 您的SID和AUTH_TOKEN来自您的twilio帐户。 登录时,转到仪表板。 你会在那里找到你的SID和AUTH_TOKEN。

这是我用来向twilio发出呼叫请求的代码。 它应该可以帮助您入门。

var https = require('https'); var qs = require('querystring'); var api = 'your api key'; var auth = 'your auth token'; var postdata = qs.stringify({ 'From' : '+5554321212', 'To' : '+5552226262', 'Url' : 'http://yourwebsite.com/call' }); var options = { host: 'api.twilio.com', path: '/2010-04-01/Accounts/<your api key>/Calls.xml', port: 443, method: 'POST', headers: { 'Content-Type' : 'application/x-www-form-urlencoded', 'Content-Length' : postdata.length }, auth: api + ':' + auth }; var request = https.request(options, function(res){ res.setEncoding('utf8'); res.on('data', function(chunk){ console.log('Response: ' + chunk); }) }) request.write(postdata); request.end();

The hostname is 'api.twilio.com'. Your SID and AUTH_TOKEN come from your twilio account. When you log in, go to the dashboard. You'll find your SID and AUTH_TOKEN listed there.

Here's the code I use to make a request to twilio to place a call. It should help you get started.

var https = require('https'); var qs = require('querystring'); var api = 'your api key'; var auth = 'your auth token'; var postdata = qs.stringify({ 'From' : '+5554321212', 'To' : '+5552226262', 'Url' : 'http://yourwebsite.com/call' }); var options = { host: 'api.twilio.com', path: '/2010-04-01/Accounts/<your api key>/Calls.xml', port: 443, method: 'POST', headers: { 'Content-Type' : 'application/x-www-form-urlencoded', 'Content-Length' : postdata.length }, auth: api + ':' + auth }; var request = https.request(options, function(res){ res.setEncoding('utf8'); res.on('data', function(chunk){ console.log('Response: ' + chunk); }) }) request.write(postdata); request.end();

更多推荐

本文发布于:2023-07-20 02:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1192043.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:js   Node   integration   twilio

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!