Register multiple webhooks for single Twilio number - twilio

Is it possible to programmatically register multiple webhook URLs for a single Twilio number? (When we receive an inbound SMS we would like multiple webhooks to be called with that data)
The docs for "update an incomingPhoneNumber resource" suggest that you can optionally set the smsUrl, but it's not clear how you would set multiple webhook URLs for a single number.

Not through the Twilio console options. You can write a Twilio Function that can fork out multiple outbound webhooks such as below, and then you just point the Twilio console to this Twilio Function.
const axios = require('axios');
const qs = require('querystring');
exports.handler = function(context, event, callback) {
let twiml = new Twilio.twiml.MessagingResponse();
let {
ApiVersion,
SmsSid,
SmsStatus,
SmsMessageSid,
NumSegments,
ToState,
From,
MessageSid,
AccountSid,
ToCity,
FromCountry,
ToZip,
FromCity,
To,
FromZip,
ToCountry,
Body,
NumMedia,
FromState
} = event;
let requestBody = {
ApiVersion,
SmsSid,
SmsStatus,
SmsMessageSid,
NumSegments,
ToState,
From,
MessageSid,
AccountSid,
ToCity,
FromCountry,
ToZip,
FromCity,
To,
FromZip,
ToCountry,
Body,
NumMedia,
FromState
};
let url1 = "https://example.com/1";
let url2 = "https://example.com/2";
const config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
Promise.all([
axios.post(url1, qs.stringify(requestBody), config),
axios.post(url2, qs.stringify(requestBody), config)
]).then(result => {
callback(null, twiml);
}).catch(err => {
console.log(err);
callback(err);
});
};

Related

Twilio Device Setup - Registers, Connects to TWiML App, but won't accept calls

Am trying to setup a soft phone in browser, via TWiML app.
I created the following:
A page on my own server which serves TWiML to dial a number, say '+11111111111'.
A TWiML app which points to that page
A Twilio number which points to that app.
A page on my server which creates a Twilio Device associated with the app and buttons to run device.connect() or device.register()
The device is setup correctly, as I can register and connect with no errors.
The app works - the phone rings at +111 when I press "Call Using Twilio" button on the TWiML app page, when I call the number pointing to the TWiML app, and when I press the button on my own page. Yay!
What I cannot figure out:
How to answer the phone using the Twilio Device.
The device registers with no issue, so why doesn't it get incoming calls?
How to call other numbers aside from the one in my app.
I did in device.connect({params: {To: '+2222222222'}}), why doesn't it call that number?
Here are all the relevant pages:
calls.pug:
doctype html
html(lang="en")
head
meta(charset="utf-8")
title Browser Calls
body
input#call(type="submit" value="call")
input#register(type="submit" value="register")
pre
script(src="/js/twilio-sdk/twilio.js")
script(type="module" src="/js/calls.js")
[client] calls.js
const $ = css => document.querySelector(css);
const echo = msg => $('pre').innerText += msg + "\n";
var device;
async function setupDevice(){
let data = await fetch('/token/new');
data = await data.json();
device = new Twilio.Device(data.token);
device.on("error",
err => echo("Twilio.Device Error: " + err.message));
echo ('Device setup complete.')
}
$('#register').addEventListener('click', async e => {
if (!device) await setupDevice();
device.on("registered", conn =>
echo('Ready to accept incoming calls.')
);
device.on("incoming", call =>
echo('Incoming call to device')
);
device.register()
});
$('#call').addEventListener('click', async e => {
if (!device) await setupDevice();
let params = { To: '+22222222222' }
device.connect({ params })
});
[node] token.js
const express = require('express');
const router = express.Router();
const AccessToken = require('twilio').jwt.AccessToken;
const VoiceGrant = AccessToken.VoiceGrant;
const config = require('../config');
router.get('/new', function (req, res) {
const accessToken = new AccessToken(
config.accountSid, config.apiKey, config.apiSecret, {identity: 'SG'}
);
const grant = new VoiceGrant({
outgoingApplicationSid: config.appSid,
incomingAllow: true,
});
accessToken.addGrant(grant);
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({ token: accessToken.toJwt() }));
});
module.exports = router;
Twilio developer evangelist here.
How to answer the phone using the Twilio Device.
The device registers with no issue, so why doesn't it get incoming calls?
To direct a call to the Device, you need to return TwiML that <Dial>s the <Client> with the ID of your access token ("SG" in this case).
So, if you want calls to your Twilio number to be directed to this client you should update the Twilio number's incoming webhook to point to a URL that returns this:
<Response>
<Dial>
<Client>
<Identity>SG</Identity>
</Client>
</Dial>
</Response>
How to call other numbers aside from the one in my app.
I did in device.connect({params: {To: '+2222222222'}}), why doesn't it call that number?
Currently, it sounds like you point the Device to a TwiML app that responds with static TwiML that dials your example number '+11111111111'. To use the parameters, you will need to make that TwiML dynamic, by returning it from your Node server instead. The parameters are sent to the TwiML endpoint, so you can then use them in the response.
Here is an example Express endpoint that would work for you:
const express = require('express');
const router = express.Router();
const { VoiceResponse } = require("twilio").twiml;
const config = require('../config');
router.use(express.urlencoded({ extended: false }));
router.post("/call", function(res, res) {
const To = req.body;
const twiml = new VoiceResponse();
const dial = twiml.dial({ callerId: config.twilioNumber });
dial.number(To);
res.set("Content-Type", "text/xml");
res.send(twiml.toString());
});
module.exports = router;

Getting the number of the caller in twilio

I want to know the current callers(client) Phone Number and then be able to verify it from my HubSpot CRM to see which language my client speaks. Then Enqueue the call to the agent that speaks the language or give the Caller an option to choose the preferred language.
Anyhoo
Right now my goal is to just get the number of the caller(client).
exports.handler = function(context, event, callback) {
console.log(event);
//const request = require('request');
//const client = require('twilio');
const res = new Twilio.Response();
res.appendHeader('Access-Control-Allow-Origin', '*');
res.appendHeader('Access-Control-Allow-Methods', 'OPTIONS POST');
res.appendHeader('Content-Type', 'application/json');
res.appendHeader('Access-Control-Allow-Headers', 'Content-Type');
const fetch = require("node-fetch");
var phoneNumber = event.From; //|| [ "+63XXXXXXXXXX" ];
const apiUrl = "https://myapiurl.com/contact-details.php?number="+phoneNumber;
fetch(apiUrl,{
method: 'GET',
headers: { 'Content-Type': 'application/x-www-form-urlencoded'}
})
.then( response =>{
return response.json();
})
.then(json=>{
console.log(json);
res.setBody(json);
callback(null,res);
})
.catch(error=>{
console.log(error);
callback();
});
};
Also let me know if there is a better way to do it.
Twilio developer evangelist here.
Your code shows you getting event.From. This is the number of the person calling, so you have already done it!

How to make an outbound call with Twilio and detect if it has been answered

I am trying to develop a simple application with Twilio and I have some issues. I have the code made with Python to make the call.
def makeCall(self, phoneNumber, from_num, message):
call = self.client.calls.create(
method='POST',
machine_detection='Enable',
to= phoneNumber,
from_= from_num,
url= self.url + urlencode({'Message' : message}),
status_callback= self.url_callback,
status_callback_method = 'POST'
)
return call.sid
And I also have a Node.js application with the Twiml response.
const express = require('express');
const VoiceResponse = require('twilio').twiml.VoiceResponse;
const urlencoded = require('body-parser').urlencoded;
const app = express();
app.post('/start', (request, response) => {
const twiml = new VoiceResponse();
const gather = twiml.gather({
numDigits: 1,
action: '/confirmation'
});
gather.say({
language:'en-EN'
}, 'Hello, are you ok?' );
gather.pause({
length: 1
});
gather.say({
language:'en-EN'
}, 'If no press one, if yes press two');
response.type('text/xml');
response.send(twiml.toString());
});
app.post('/confirmation', (request, response) => {
const twiml = new VoiceResponse();
if (request.body.Digits) {
switch (request.body.Digits) {
case '2':
twiml.say({
language:'en-EN'
}, 'I am sorry to hear that');
case '1':
twiml.say({
language:'en-EN'
}, 'Perfect!');
default:
twiml.say({
language:'en-EN'
}, 'Sorry, I don't understand you.');
twiml.pause({
length: 1
});
twiml.say({
language: 'en-EN'
}, 'Repeat please');
}
}
response.type('text/xml');
response.send(twiml.toString());
});
app.post('/callback', (request, response) => {
console.log(request.body.CallStatus);
console.log('--------------------');
console.log(request.body.AnsweredBy);
console.log('--------------------');
response.type('text/xml')
response.send(request.AnsweredBy)
});
app.listen(3000);
The problem is that when I execute the python function. If the user reject the call or doesn't answer, it sends a voicemessage to the answering machine and I would like to avoid it. I would also like to detect in the python code, if the call is rejected or not answered.
Thanks in advance
Twilio developer evangelist here.
You can't detect whether the call was answered in your python code that creates the call. That will queue up the call to be dispatched by Twilio, so all further events will happen asynchronously to that API call.
For your Node.js application that is receiving the webhook you can check what the current status of the call is by inspecting the CallStatus parameter that is sent as part of the body of the request. The CallStatus can be one of: "queued", "ringing", "in-progress", "completed", "busy", "failed" or "no-answer" and you can see more about the CallStatus parameter in the documentation.
To read the CallStatus parameter, you'll need to ensure you are using the body-parser middleware properly, urlencoded is a function and you need to set the express app to use it.
const urlencoded = require('body-parser').urlencoded;
const app = express();
app.use(urlencoded({ extended: false });
You can then get the call status in your response function.
app.post('/start', (request, response) => {
console.log(request.body.CallStatus);
// and so on
Then you can handle it from there.

Call forwarding + SMS + Whisper?

I'm trying to implement a Twilio function to (1) forward calls to my personal phone, (2) send a "heads up" SMS just before, and (3) say a whisper prior to connecting. I've been able to set up Twilio to do any 2 of the previous 3 things but never the 3 at the same time!
exports.handler = function(context, event, callback) {
// Get an initialized Twilio API client
const client = context.getTwilioClient();
twilioClient.messages.create({
to: 'PERSONAL_PHONE',
from: 'TWILIO_PHONE',
body: 'Incoming!!!'
}).then(function() {
const twiml = new Twilio.twiml.VoiceResponse();
twiml.dial.number({ url: WHISPER_URL }, 'PERSONAL_PHONE');
callback(null, twiml);
});
};
When implementing this, it sends the SMS but the call never connects (and the calling party hears an error message).
Would really appreciate a lesson here :)
Thank you!
Btw, I found a solution:
exports.handler = function(context, event, callback) {
// Get an initialized Twilio API client
const client = context.getTwilioClient();
twilioClient.messages.create({
to: 'NUMBER',
from: 'TWILIO_PHONE',
body: 'Incoming!!!'
}).then(function() {
const twiml = new Twilio.twiml.VoiceResponse();
const dialobj = twiml.dial();
dialobj.number({url:'WHISPER_URL'},'NUMBER');
callback(null, twiml);
});
};

Twilio statusCallback doesn't fire

I'm trying to set up messages delivery status check with twilio. For some reasons twilio statusCallback doesn't fire. Could you please help me to find an error?
Here is a file where I do initialization and send messages:
const Twilio = require('twilio');
const {
TWILIO_ACCOUNT_SID,
TWILIO_AUTH_TOKEN,
TWILIO_PHONE_NUMBER
} = require('config');
const client = new Twilio(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN);
module.exports = {
send: (body, phoneNumber) => client.messages.create({
body,
to: phoneNumber,
from: TWILIO_PHONE_NUMBER,
statusCallback: 'http://postb.in/b/XXXXXXXX'
})
.then((message) => {
console.log(message.sid);
return message.sid;
})
};
Here is a test where I call message sending after initialization:
const { expect } = require('chai');
const smsUtility = require('utils/sms');
describe('Sms utility', () => {
it('should send a message and return message sid', (done) => {
const body = 'Body';
const number = '+XXXXXXXXXXX';
smsUtility.send(body, number)
.then((messageSid) => {
expect(typeof messageSid).to.equal('string');
return done();
})
.catch((err) => {
console.log(err);
return done();
});
});
});
I use test credentials, but when I replace it with real twilio credentials, I successfully receive a message, so this part works fine. Also, when I try to ping postb.in url manually (with curl), it also works OK. Only statusCallback doesn't work.
Thanks.
Twilio developer evangelist here.
It looks to me like you have everything set up nicely aside from your Postbin URL.
I noticed you show your URL as http://postb.in/b/XXXXXXXX. But the /b/ version of the URL is the dashboard for your Postbin. Requests to the dashboard won't show up on the dashboard.
Instead, you should use the URL that looks like: http://postb.in/XXXXXXXX. Try that and let me know if it's working.

Resources