UNPKG

exotel-client

Version:
95 lines (81 loc) 2.16 kB
var http = require('https'); var querystring =require("querystring"); var sid; var token; var basePath = "/v1/Accounts/~exotel_sid~/"; module.exports = { init: function(exotel_sid, exotel_token) { sid = exotel_sid; token = exotel_token; }, call_number: function(from, exophone, to, callback) { var path = "Calls/connect"; var params = { 'From' : from, 'To' : to, 'CallerId' : exophone, 'CallType' : 'trans' } makeRequest('POST', path, params, callback); }, call_flow: function(from, exophone, flowId, callback) { var path = "Calls/connect"; var url = "http://my.exotel.in/exoml/start/~appid~"; url = url.replace('~appid~', flowId); var params = { 'From' : from, 'Url' : url, 'CallerId' : exophone, 'CallType' : 'trans' } makeRequest('POST', path, params, callback); }, send_sms: function(from, to, body, callback) { var path = "Sms/send"; var params = { 'From' : from, 'To' : to, 'Body' : body } makeRequest('POST', path, params, callback); }, call_status: function(callSid, callback) { var path = "Calls/"+callSid; var params = {}; makeRequest('GET', path, params, callback); }, sms_status: function(SmsId, callback) { var path = "SMS/Messages/"+SmsId; var params = {}; makeRequest('GET', path, params, callback); } }; function makeRequest(type, path, params, callback) { auth = "Basic " + new Buffer(sid + ":" + token).toString("base64"); fullPath = basePath+path+'.json'; fullPath = fullPath.replace('~exotel_sid~', sid); var postData = querystring.stringify(params); var options = { hostname: 'twilix.exotel.in', path: fullPath, method: type, headers: { 'Authorization':auth, 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': postData.length } }; var req = http.request(options, function(res) { res.setEncoding('utf8'); res.on('data', function (chunk) { if(typeof callback === 'function') { callback(null, chunk); } }); }); req.on('error', function(e) { callback(e.message, null); }); req.write(postData); req.end(); }