Skip to content

Instantly share code, notes, and snippets.

@yefim
Last active October 4, 2016 07:19
Show Gist options
  • Save yefim/9823281 to your computer and use it in GitHub Desktop.
Save yefim/9823281 to your computer and use it in GitHub Desktop.
A simple opt in group messaging solution with Twilio, Redis, and Expressjs
NUMBERS_SET = 'numbers'
TWILIO_NUMBER = '+12345678900'
TWILIO_SID = 'account_sid'
TWILIO_TOKEN = 'auth_token'
express = require('express')
http = require('http')
path = require('path')
redis = require('redis')
app = express()
twilioClient = require('twilio')(TWILIO_SID, TWILIO_TOKEN)
redisClient = redis.createClient()
app.set('port', process.env.PORT or 3000)
app.use(express.urlencoded())
app.post '/', (req, res) ->
from = req.param('From')
message = req.param('Body')
redisClient.sismember NUMBERS_SET, from, (err, exists) ->
if exists
redisClient.get from, (err, name) ->
message = "#{name}: #{message}"
redisClient.smembers NUMBERS_SET, (err, numbers) ->
for number in numbers
continue if number is from
twilioClient.sendMessage
to: number
from: TWILIO_NUMBER
body: message
res.send()
else
redisClient.sadd(NUMBERS_SET, from)
redisClient.set(from, message)
twilioClient.sendMessage
to: from
from: TWILIO_NUMBER
body: 'Your name and number were successfully added.'
res.send()
# listen
http.createServer(app).listen app.get('port'), ->
console.log("Express server listening on port #{app.get('port')}");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment