Skip to content

Instantly share code, notes, and snippets.

@arindam89
Last active August 24, 2022 07:00
Show Gist options
  • Save arindam89/f5380e12560b337c4d89 to your computer and use it in GitHub Desktop.
Save arindam89/f5380e12560b337c4d89 to your computer and use it in GitHub Desktop.
// Imagine your code structure is something like this.
// start
...
function sendSms() {
// Implementation
}
..
//end
// This is how it can wrapped in a Angular Service
var smsService = angular.module('app.services')
smsService.factory('smsService',
[function(){
// start
...
function sendSms() {
// Implementation
}
..
//end
return {
sendSms: sendSms
}
}]);
// Configuration for Twilio SMS API.
module.exports = {
// Production
TWILIO_ACCOUNT_SID: 'ACba5e5ecc95blahblah79bbc17d93f62e08',
TWILIO_AUTH_TOKEN: '0331cbb3628fbe5blahblahf983e069009',
// TWILIO_NUMBER: '+14086375837'
TWILIO_NUMBER: '+12513330209'
// Testing
// TWILIO_ACCOUNT_SID: 'AC3724c2b73b5blahblahb890d2d358eb188b',
// TWILIO_AUTH_TOKEN: '6d904fbc7c33blahblahae60f8fcec78e9',
// TWILIO_NUMBER: '+15005550006'
}
var SMS_CONFIG = require('./twilio.api_key.js');
var cfg = {};
// A random string that will help generate secure one-time passwords and
// HTTP sessions
cfg.secret = process.env.APP_SECRET || 'keyboard cat';
// Your Twilio account SID and auth token, both found at:
// https://www.twilio.com/user/account
//
// A good practice is to store these string values as system environment
// variables, and load them from there as we are doing below. Alternately,
// you could hard code these values here as strings.
cfg.accountSid = process.env.TWILIO_ACCOUNT_SID || SMS_CONFIG.TWILIO_ACCOUNT_SID;
cfg.authToken = process.env.TWILIO_AUTH_TOKEN || SMS_CONFIG.TWILIO_AUTH_TOKEN;
cfg.sendingNumber = process.env.TWILIO_NUMBER || SMS_CONFIG.TWILIO_NUMBER;
var requiredConfig = [cfg.accountSid, cfg.authToken, cfg.sendingNumber];
var isConfigured = requiredConfig.every(function(configValue) {
return configValue || false;
});
if (!isConfigured) {
var errorMessage =
'TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN, and TWILIO_NUMBER must be set.';
throw new Error(errorMessage);
}
// Export configuration object
module.exports = cfg;
var express = require('express');
var twilioService = require('../services/twilio_service.js');
var router = express.Router();
router.post('/sendmsg', function(req, res, next) {
var msg = req.body;
var resp = {};
if (!msg || !msg.to || !msg.text) {
resp.status = "error";
resp.message = "invalid data";
res.json(resp);
return 1;
//next();
}
twilioService.sendSms(msg.to, msg.text, function(err, data){
if(err) {
resp.status = "error";
resp.message = err;
} else {
resp.status = "success";
resp.message = data.sid;
}
res.json(resp);
//next();
});
});
module.exports = router;
var config = require('../../config/twilio.config.js');
var client = require('twilio')(config.accountSid, config.authToken);
module.exports.sendSms = function(to, message, callback) {
console.log("==== Sending SMS ====");
console.log("To: " + to );
console.log("Msg: " + message );
console.log("==== End Sending SMS ====");
client.messages.create({
body: message,
to: to,
from: config.sendingNumber
// mediaUrl: imageUrl
}, function(err, data) {
if (err) {
console.error('Could not notify user');
console.error(err);
// resp.status = "error";
// resp.message = err;
} else {
console.log('User notified');
// resp.status = "success";
// resp.message = data.sid;
}
callback(err, data);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment