Skip to content

Instantly share code, notes, and snippets.

@mehdimehdi
Created November 6, 2016 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehdimehdi/2f48a470e7953c2b1b58001ca5950e5d to your computer and use it in GitHub Desktop.
Save mehdimehdi/2f48a470e7953c2b1b58001ca5950e5d to your computer and use it in GitHub Desktop.
Handling SMS response from customers
""" more routes before, but does not affect the use case """
def follow_up_with_callee(twilio_data,event_id):
text = twilio_data.get('Body').upper()
task = import_module('switchboard.tasks')
if text == 'remove'.upper():
content = 'Great. will let him know.'
task.let_callee_know_cancelation.delay(event_id)
elif text == 'other way'.upper():
content = 'Okay. Will do.'
task.let_callee_know_other_way.delay(event_id)
elif text == 'nothing'.upper():
content = 'Okay.'
else:
content = 'Sorry, I did not quite get that, type "remove", "other way" or "nothing"'
resp = twiml.Response()
message = twiml.Message(content)
resp.append(message)
mr = make_response(str(resp))
mr.set_cookie('callee_follow_up',expires=0)# <= this never happens.
return mr
@csrf.exempt
@internals.route("/sms-response",methods=['GET','POST'])
def sms_response():
#disable newrelic JS, because it fucks up the XML other wise.
agent.disable_browser_autorum()
resp = twiml.Response()
convo_cookie = None
event_id = request.cookies.get('callee_follow_up',False)
if event_id:
return follow_up_with_callee(request.form,event_id)
from_phone = request.form.get('From')
current_app.logger.debug("Getting SMS response from {}".format(from_phone))
rc = redis.from_url(current_app.config['BROKER_URL'])
uuid = rc.get(from_phone)
current_app.logger.debug("found uuid {}".format(uuid))
if uuid:
uuid = uuid.decode("utf-8")
event = Event.query.filter(Event.uuid==uuid).first()
text = request.form.get('Body').upper()
if text == 'call now'.upper():
content = 'Roger that, calling now.'
connect_call(event,current_app.config.get('SERVER_NAME'))
rc.delete(from_phone)
elif text == 'snooze'.upper():
content = 'Of course, snoozing 5 minutes.'
event.start_date = event.start_date + timedelta(minutes=5)
rc.delete(from_phone)
elif text == 'remove'.upper():
content = 'Consider it done.'
event.state = 'DEL'
task = import_module('switchboard.tasks')
task.may_follow_up_with_callee.apply_async(args=[event.id,'sms'], eta=datetime.utcnow() + timedelta(seconds=5))
rc.delete(from_phone)
convo_cookie = {'key':'callee_follow_up','value':str(event.id),'expires':datetime.utcnow() + timedelta(minutes=5)}
else:
content = 'Sorry, I did not quite get that, type "call now", "snooze" or "remove"'
db.session.commit()
else:
content = 'Hello there! Thanks for reaching out. We are not quite ready to have a conversation via SMS. If you want to chat feel free to email us at hello@switchboardup.com'
message = twiml.Message(content)
resp.append(message)
mr = make_response(str(resp))
if convo_cookie:
mr.set_cookie(convo_cookie['key'],value=convo_cookie['value'],expires=convo_cookie['expires'])
return mr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment