Last active
October 4, 2016 04:38
-
-
Save amazingandyyy/6e67cb8a07bea72a47da0fd2c1319a0a to your computer and use it in GitHub Desktop.
Twilio SMS API (NodeJS snippet)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To send sms message with Twilio SMS API in Node.js. | |
/*----------------------Sample codes----------------------*/ | |
// .env | |
// TWILIO_ACCOUNT_SID='ACde4b5sample3cc1fb166ef26bd83e8' | |
// TWILIO_AUTH_TOKEN='4f631903940sample06869e988' | |
// TWILIO_NUMBER='+162822XXXXXX' | |
// init twilio | |
var twilio = require('twilio')(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN); | |
router.post('/sentMessage', function(req, res) { | |
// prepare content | |
var receiver_num = req.body.phoneNumber | |
var receiver_name = req.body.name | |
// trigger twilio to send message | |
twilio.sendMessage({ | |
to: receiver_num, | |
from: process.env.TWILIO_NUMBER, | |
body: `Hi ${receiver_name}` | |
}, function(err, good){ | |
if (err) { | |
console.log('err when send twilio SMS: ', err); | |
return res.status(500).send('fail to send') | |
} | |
// successful | |
console.log(`send message to ${receiver_num}`) | |
res.status(200).send('message sent') | |
}) | |
}); | |
/*--------------------------------------------------------*/ | |
/*--------------------------over--------------------------*/ | |
/*--------------------------------------------------------*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
get SID and Token here: https://www.twilio.com/console