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

狂风中的少年 提交于 2019-12-19 05:51:01

问题


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. 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?


回答1:


When Twilio receives a call to your phone number, it will send an HTTP request to a URL you configure in the admin console:

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. 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:

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.



来源:https://stackoverflow.com/questions/14556426/how-can-i-respond-to-incoming-twilio-calls-and-sms-messages-using-node-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!