Skip to content

Instantly share code, notes, and snippets.

@coolaj86
Last active December 12, 2017 16:05
Show Gist options
  • Save coolaj86/5847176 to your computer and use it in GitHub Desktop.
Save coolaj86/5847176 to your computer and use it in GitHub Desktop.
Forward SMS from one number to another using Twilio

Twillio Forward SMS

Twilio provides a TwiMLet to forward voice calls, but it doesn't provide one for SMS.

Although it's not possible to directly forward SMS (by displaying the number of the original sender in the forwarded message), it's certainly possible to append the number to the forwarded message.

Usage

copy config.default.js to config.js and then set your twilio SMS receive callback to http://yourserver.com/twilio/forwardsms

module.exports =
{
fooaccount:
{ number: '555-444-9999'
, id: 'AC5cf0ada6f83e532b75b721b2a'
, auth: '2a04fe71858693359231949fcfd57228'
, forwardTo: '444-555-3333'
}
};
(function () {
"use strict";
var config = require('./config').fooaccount
, connect = require('connect')
, Twilio = require('twilio')
, twilio = new Twilio.RestClient(config.id, config.auth)
, app = connect()
, port = '8080'
, server
;
function sms(forwardTo, forwardFrom, body) {
// Use this convenient shorthand to send an SMS:
twilio.sendSms(
{ to: forwardTo
, from: config.number
, body: body += ' from: ' + forwardFrom
}
, function(error, message) {
if (!error) {
console.log('Success! The SID for this SMS message is:');
console.log(message.sid);
console.log('Message sent on:');
console.log(message.dateCreated);
}
else {
console.log('Oops! There was an error.');
}
}
);
}
app.use(connect.json());
app.use(connect.urlencoded());
app.use(connect.bodyParser());
app.use('/twilio/forwardsms', function (req, res, next) {
if (!/^POST$/i.test(req.method)) {
next();
}
res.setHeader('Content-Type', 'application/xml');
res.end('<Response></Response>');
sms(config.forwardTo, req.body.From, req.body.Body);
});
server = app.listen(port, function () {
console.log('listening', server.address());
});
}());
{
"name": "twilio-forward-sms-example",
"version": "0.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": "",
"author": "",
"license": "BSD"
}
req.body:
{ AccountSid: 'ACafd72658d9a6fcf06378317c29d3cbe5',
Body: '(1/2)Hello Self! This is a pretty long text message. I\'d go far enough to say that it will actually take 2 messages to come through... yeah, but I gotta make it',
ToZip: '84070',
FromState: 'IN',
ToCity: 'SALT LAKE CITY',
SmsSid: 'SM50cfd7580516c40a9dc051e312872e26',
ToState: 'UT',
To: '+19999999999',
ToCountry: 'US',
FromCountry: 'US',
SmsMessageSid: 'SM50cfd7580516c40a9dc051e312872e26',
ApiVersion: '2010-04-01',
FromCity: 'INDIANAPOLIS',
SmsStatus: 'received',
From: '+15555555555',
FromZip: '46218' }
req.body:
{ AccountSid: 'ACafd72658d9a6fcf06378317c29d3cbe5',
Body: '(2/2)longer. It\'s gotta be totes los longes, y\'know?',
ToZip: '84070',
FromState: 'IN',
ToCity: 'SALT LAKE CITY',
SmsSid: 'SM01e688123b0bd75ea21e385610d3dcf7',
ToState: 'UT',
To: '+19999999999',
ToCountry: 'US',
FromCountry: 'US',
SmsMessageSid: 'SM01e688123b0bd75ea21e385610d3dcf7',
ApiVersion: '2010-04-01',
FromCity: 'INDIANAPOLIS',
SmsStatus: 'received',
From: '+15555555555',
FromZip: '46218' }
@tessoudali
Copy link

i want code foward incoming sms to skype users please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment