def handle_donation(donor, recipient): """chooses inactive/undeclined volunteer and sends them invite to mission""" sms = SMS() # find a valid volunteer (i.e., not already busy or declined) volunteer = choose_volunteer() # globally-store the fact we've invited vol to deliver invitations[volunteer.mobile] = (donor, recipient) # issue the message sms.send_msg(volunteer.mobile, "A food pickup is ready for you at {} ({}). Text 'Yes' to accept this mission, or 'No' to decline.".format( donor.name, donor.location))
def respond(): """receives all incoming SMS messages""" try: resp = twiml.Response() sms = SMS() # grab the incoming volunteer phone number and the msg they sent from_number = request.form.get('From', '') body = request.form.get('Body', '') # find the volunteer entry by phone number volunteer = volunteer_map.get(from_number, None) # volunteer is at delivery phase, finishing! if 'in-delivery' in session: # send two messages to both feedback_url = "http://google.com" sms.send_msg(volunteer.mobile, "Thank you for your service! Please visit {} to rate your experience!".format(feedback_url)) # sms.send_msg(recipient.mobile, ) cleanup(volunteer.mobile) # volunteer is at picking up stage, time to deliver parcel elif 'claimed' in session: (donor, recipient) = deliveries[body] = invitations[from_number] resp.message("Please deliver the parcel to {}, and text 'done' when delivered.".format(recipient.location)) session['in-delivery'] = True # volunteer is responding to initial invitation, so decide yes or no elif from_number in invitations: # if no, need to mark volunteer as declined and retry the invite if 'n' in body.lower(): # remove from invitations invite = invitations.pop(from_number) declined[from_number] = True # re-invite next volunteer handle_donation(*invite) # otherwise they said yes else: # claim the pickup for the volunteer session['claimed'] = from_number # ...and msg them to text back when they pickup the parcel resp.message("Wonderful! When you pickup the parcel, text 'pickup' back to us, and we'll let them know you are coming!".format(body)) else: resp.message("Visit our site to find out more!")# debug(resp) return str(resp) except Exception as e: print e