Skip to content

Instantly share code, notes, and snippets.

View lbarwiko's full-sized avatar

lbarwiko

View GitHub Profile
class CreateVolunteer(TaskQueue):
def __init__(self, r):
super().__init__(r, 'createVolunteer')
def respond(self, m):
message = {
'message_id': 'GreetVolunteer',
'number': m['number']
}
self.sendSMS(message)
class Feed():
def __init__(self, r, qName):
self.redis = r
self.name = qName
def sendSMS(self, message):
self.redis.rpush('sendSMSQueue', json.dumps(message))
def respond(self, m):
pass
class TaskQueue(Feed):
def __init__(self, r, qName):
super().__init__(r, qName)
self.type = "task"
def get_message(self):
val = self.redis.lpop(self.name)
if not val:
return
return json.loads(val.decode("utf-8"))
def handleReply(self, message):
lastMessage = self.readLQCache(message['number'])
if lastMessage:
replySchema = self.getExpectedReply(lastMessage)
if replySchema:
matchedReply = False
for option in replySchema['options']:
if message['body'] in option['responses']:
matchedReply = True
self.clearLQCache(message['number'])
def sendMessage(self, message):
self.sendSMS(message)
if 'message_id' in message:
if self.doesMessageExpectReply(message['message_id']):
self.updateLQCache(message['number'], message['message_id'])
def updateLQCache(self, number, message_id):
return self.redisClient.hset('LQ', number, message_id)
def readLQCache(self, number):
message = self.redisClient.hget('LQ', number)
if message:
return message.decode("utf-8")
export default (redis)=>{
function recieve(req, res, next){
if(req.body && req.body.Body && req.body.From){
redis.rpush('recievedQueue', JSON.stringify({
'number': req.body.From,
'body': req.body.Body
}));
return;
}
res.status(404).json({err: 'Bad text.', code: 404});
const SmsApi = Router();
SmsApi.post('/', Sms.recieve);
router.use('/sms/', SmsApi);
def sendSMS(self, message):
try:
if 'message_id' in message:
self.twilio.messages.create(
body=messages.MASTER_LIST[message['message_id']]['body'],
from_= config.TWILIO['from_'],
to = message['number']
)
elif 'message' in message:
self.twilio.messages.create(
MASTER_LIST = {
"default":{
"body": "We weren't expecting a reply from you! Sign up on whatnow.co to join the community!"
},
"GreetVolunteer": {
"body": "We appreciate you! Do you understand your resposibilities and feel comfortable with this role? (You can review them at whatnow.co/greet-volunteer) Reply Y/N",
"expectsReply": {
"options":[
{
"responses": ['y', 'Y', 'YES', 'yes', 'Yes'],