简体   繁体   English

如何使用node.js响应传入的Twilio呼叫和SMS消息?

[英]How can I respond to incoming Twilio calls and SMS messages using node.js?

In my application I'm using the twilio node.js module to receive an sms, send an sms, receive a call and make an outgoing call. 在我的应用程序中,我使用twilio node.js模块接收短信,发送短信,接听电话和拨打电话。 I figured out how to send an sms and make a call. 我想出了如何发送短信和拨打电话。 But I don't know how to respond to incoming calls and SMS messages. 但我不知道如何回复来电和短信。 How can I use node to respond to these? 我如何使用节点来响应这些?

When Twilio receives a call to your phone number, it will send an HTTP request to a URL you configure in the admin console: 当Twilio收到您电话号码的呼叫时,它会向您在管理控制台中配置的URL发送HTTP请求:

twilio帐户仪表板

What Twilio expects in return from this HTTP request is a set of XML instructions called TwiML that will tell Twilio what to do in response to the call. Twilio期望从这个HTTP请求返回的是一组称为TwiML的XML指令 ,它将告诉Twilio响应该调用该做什么。 For example, let's say that you wanted to respond to a phone call by saying "thank you" and then playing a music file. 例如,假设您想通过说“谢谢”然后播放音乐文件来回复电话。 If you wanted to do this in node, you might consider using this node library and the express framework to send a TwiML response: 如果您想在节点中执行此操作,可以考虑使用此节点库和express框架来发送TwiML响应:

var twilio = require('twilio'),
    express = require('express');

// Create express app with middleware to parse POST body
var app = express();
app.use(express.urlencoded());

// Create a route to respond to a call
app.post('/respondToVoiceCall', function(req, res) {
    //Validate that this request really came from Twilio...
    if (twilio.validateExpressRequest(req, 'YOUR_AUTH_TOKEN')) {
        var twiml = new twilio.TwimlResponse();

        twiml.say('Hi!  Thanks for checking out my app!')
            .play('http://myserver.com/mysong.mp3');

        res.type('text/xml');
        res.send(twiml.toString());
    }
    else {
        res.send('you are not twilio.  Buzz off.');
    }
});

app.listen(process.env.PORT || 3000);

Good luck - hope this is what you were looking for. 祝你好运 - 希望这就是你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM