muthu vel
muthu vel

Reputation: 119

Node.js and twilio integration

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();
        });
    });
});

Upvotes: 1

Views: 2182

Answers (1)

Don
Don

Reputation: 677

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();

Upvotes: 4

Related Questions