In order to use this slash webtask, you must configure the following secrets in the webtask editor:
TWILIO_NUMBER
- the "from" twilio number
TWILIO_SID
, TWILIO_AUTH_TOKEN
- your twilio credentials
var numbers = { | |
skynet: ['+14251234567','+15148836820'], | |
gustav: ['+13036498063'], | |
crew2: ['+16102423400', '+16083352365','+5491154760241','+5481161020204','+5491156130402'] | |
}; | |
var twilio = require('twilio'); | |
var async = require('async'); | |
module.exports = function (ctx, cb) { | |
console.log('NEW REQUEST', ctx.body); | |
// Parse command: {slack_username} {message_to_send} | |
var tokens = ctx.body.text.match(/^\s*([^\s+]+)\s+(.+)$/); | |
if (!tokens) return cb(null, { | |
response_type: 'in_channel', | |
text: 'Usage: /wt wakeup {name} {message}\nAllowed {name}s are:\n' + Object.keys(numbers).sort().join('\n') | |
}); | |
var user = tokens[1]; | |
var message = tokens[2]; | |
if (!numbers[user]) return cb(null, { | |
response_type: 'in_channel', | |
text: `Unknown name: ${user}.\nAllowed {name}s are:\n` + Object.keys(numbers).sort().join('\n') | |
}); | |
cb(null, { text: `Waking up ${user}...`, response_type: 'in_channel'}); | |
// Initialize Twilio client, use keys provided by webtask runtime in context | |
var client = new twilio.RestClient(ctx.secrets.TWILIO_SID, ctx.secrets.TWILIO_AUTH_TOKEN); | |
// Send SMS | |
var response = []; | |
async.each(numbers[user], (number, cb) => { | |
client.messages.create({ | |
body: `From ${ctx.body.user_name}: ${message}`, | |
to: number, | |
from: ctx.secrets.TWILIO_NUMBER | |
}, (err, message) => { | |
response.push(err ? `${number}: ${err.message}` : `${number}: ${message.status || message.message}`); | |
cb(); | |
}); | |
}, (e) => { | |
// Confirm back on Slack | |
require('superagent').post(ctx.body.response_url) | |
.send({ text: response.join('\n'), response_type: 'in_channel' }) | |
.end(); | |
}); | |
}; |