twilio-response-builder
v1.2.0
Published
Build your Twilio SMS Response before connecting to the Twilio Webhook
Downloads
14
Readme
Twilio-Response-Builder is a package for building SMS response before connecting with the Twilio webhook. It provides the SMS response in XML format for sending to the Twilio server, and a callback for mounting on Express/Loopback custom routers.
For Loopback Router:
var builder = require('twilio-response-builder');
var client = new builder(
'/test',
'GET',
'hello world'
);
module.exports = function(app){
if (client.method==='GET'){
app.get(client.route, client.requestCallback);
}
else if (client.method==='POST'){
app.post(client.route,client.requestCallback);
}
}
For Express.js Router:
var twilio = require('twilio-response-builder');
var client = new twilio(
'/test',
'GET',
'hello world'
);
app.route(client.route)
.get(client.requestCallback);
If you only need to extract the XML response
(Notice: since we only need the client.smsXMLResponse property, the first 2 parameters of the constructor don't really matter. However, the first parameter (route : String) must be started with a '/' and the second parameter (method : String) must be either 'GET' or 'POST', as of version 1.1.2.):
app.get('/test', function(res,req){
var smsResponse = 'hello world';
var twilio = require('twilio-response-builder');
var client = new twilio('///', 'GET',smsResponse);
// send message HERE
res.setHeader('content-type','application/xml');
res.send(client.smsXMLResponse);
});
Connect the webhook with your application like this in Messaging -> A Message Comes In:
Change /[your custom route] to client.route and the HTTP request drop down to client.method. In the Express example above, we should change the method to HTTP GET:
Click Save. And now you can test the server: