def receive_messages(): """Handle inbound messages and send responses through the Chat Engine API""" # ensure that the signature on the request is valid if not verify_signature(request): return Response(status=403, response="invalid signature") messages = request.json["messages"] responses = [] for message in messages: print message # create a response to each received message just echoing the body text if message["type"] == "text": if message["body"] == "mirror": responses.append( { "type": "text", "to": message["from"], "body": 'You said: "{}"'.format(message["body"]), "typeTime": 0, } ) if (message["body"] == "Help") or (message["body"] == "Cancel"): responses.append( { "type": "text", "to": message["from"], "body": "Welcome to the UW bot. I can get you UW related information. What would you like to know about?", "suggestedResponses": defaultSuggestedResponses, "typeTime": 0, } ) if message["body"] == "Weather": addWeatherResponse(responses, message) if message["body"] == "Courses": courseIntroResponse(responses, message) if message["body"] == "Exam Schedule": examScheduleResponse(responses, message, "CS", 245) if message["body"] == "Info Sessions": infosessionsResponse(responses, message) if message["body"] == "Location": classLocationResponse(responses, message, "CS", 245, 1) if message["body"] == "Prerequisites": coursePrerequisitesResponse(responses, message, "CS", 245) if responses: # send the responses through the Chat Engine API requests.post( "https://engine.apikik.com/api/v1/message", auth=(os.environ["USERNAME"], os.environ["API_KEY"]), json={"messages": responses}, ) return Response(status=200)
def receive_messages(): """Handle inbound messages and send responses through the Chat Engine API""" # ensure that the signature on the request is valid if not verify_signature(request): return Response(status=403, response='invalid signature') messages = request.json['messages'] responses = [] for message in messages: if message['type'] == 'scan-data': if database.lookUpUser(message['from']) == False: responses.append({ 'type': 'text', 'to': message['from'], 'body': 'Welcome to SMS Bot. I can help you send an SMS message to any number in the United States or Canada for only 10 Kik Points! What would you like to do?', 'suggestedResponses': ['Send a new message'] }) database.addUser('false', 'false', message['from'], '0', '0', 'false') #Other responses elif message['type'] == 'text': if database.lookUpUser(message['from']) == False: responses.append({ 'type': 'text', 'to': message['from'], 'body': 'Welcome to SMS Bot. I can help you send an SMS message to any number in the United States or Canada for only 10 Kik Points! What would you like to do?', 'suggestedResponses': ['Send a new message'] }) database.addUser('false', 'false', message['from'], '0', '0', 'false') elif ((message['body'] == 'Send a new message' or message['body'] == 'Send another message') and database.hasPaid( message['from']) == False): #TODO:Regex this chargePoints(message['from']) elif message['body'] == 'Change my message' and database.hasPaid( message['from']) == True: responses.append({ 'type': 'text', 'to': message['from'], 'body': 'Enter the phone number you\'d like to send a message to.' }) database.setGivenNum(message['from'], 'true') elif database.hasGivenNum( message['from']) == True and database.hasGivenMessage( message['from']) == False: phone_number_check = re.search( '^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$', message['body'] ) #Use Regex to check for proper phone-number format if (phone_number_check): responses.append({ 'type': 'text', 'to': message['from'], 'body': 'Please enter the message you\'d like to send to ' + message['body'] }) database.setGivenMessage(message['from'], 'true') database.storePhoneNum(message['from'], message['body']) else: responses.append({ 'type': 'text', 'to': message['from'], 'body': message['body'] + ' is not a valid US or Canada number. Please enter a valid number! Tip: Do NOT add +1 to the number!' }) elif ((database.hasGivenNum(message['from']) == True and database.hasGivenMessage(message['from']) == True) or (message['body'] == 'Retry' and database.hasPaid(message['from']) == True)): if twilio_api.sendsms(database.getPhoneNumber(message['from']), message['body']): responses.append({ 'type': 'text', 'to': message['from'], 'body': 'SMS Sent: \n To: ' + database.getPhoneNumber(message['from']) + '\n Message: ' + message['body'] + '\n What would you like to do next?', 'suggestedResponses': ['Send another message'] }) database.setGivenMessage(message['from'], 'false') database.setGivenNum(message['from'], 'false') database.setHasPaid(message['from'], 'false') else: responses.append({ 'type': 'text', 'to': message['from'], 'body': 'I could not send your message for some reason. What would you like to do?', 'suggestedResponses': ['Change my message', 'Retry'] }) database.setGivenMessage(message['from'], 'false') database.setGivenNum(message['from'], 'false') else: responses.append({ 'type': 'text', 'to': message['from'], 'body': 'I\'m not sure what you\'re trying to tell me. Please provide a valid command such as \'Send a new message\'.' }) elif message['type'] == 'picture': responses.append({ 'type': 'text', 'to': message['from'], 'body': 'There\'s not much I can do with this picture...' }) if responses: # send the responses through the Chat Engine API requests.post('https://engine.apikik.com/api/v1/message', auth=(os.environ['USERNAME'], os.environ['API_KEY']), json={'messages': responses}) return Response(status=200)
def receive_messages(): """Handle inbound messages and send responses through the Chat Engine API""" # ensure that the signature on the request is valid if not verify_signature(request): return Response(status=403, response='invalid signature') messages = request.json['messages'] responses = [] for message in messages: if message['type'] == 'scan-data': if database.lookUpUser(message['from']) == False: responses.append({ 'type': 'text', 'to': message['from'], 'body': 'Welcome to SMS Bot. I can help you send an SMS message to any number in the United States or Canada for only 10 Kik Points! What would you like to do?', 'suggestedResponses': ['Send a new message'] }) database.addUser('false','false', message['from'],'0','0','false') #Other responses elif message['type'] == 'text': if database.lookUpUser(message['from']) == False: responses.append({ 'type': 'text', 'to': message['from'], 'body': 'Welcome to SMS Bot. I can help you send an SMS message to any number in the United States or Canada for only 10 Kik Points! What would you like to do?', 'suggestedResponses': ['Send a new message'] }) database.addUser('false','false', message['from'],'0','0','false') elif ((message['body'] == 'Send a new message' or message['body'] == 'Send another message') and database.hasPaid(message['from']) == False): #TODO:Regex this chargePoints(message['from']) elif message['body'] == 'Change my message' and database.hasPaid(message['from']) == True: responses.append({ 'type': 'text', 'to': message['from'], 'body': 'Enter the phone number you\'d like to send a message to.' }) database.setGivenNum(message['from'],'true') elif database.hasGivenNum(message['from']) == True and database.hasGivenMessage(message['from']) == False: phone_number_check = re.search('^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$',message['body']) #Use Regex to check for proper phone-number format if (phone_number_check): responses.append({ 'type': 'text', 'to': message['from'], 'body': 'Please enter the message you\'d like to send to ' + message['body'] }) database.setGivenMessage(message['from'],'true') database.storePhoneNum(message['from'], message['body']) else: responses.append({ 'type': 'text', 'to': message['from'], 'body': message['body'] + ' is not a valid US or Canada number. Please enter a valid number! Tip: Do NOT add +1 to the number!' }) elif ((database.hasGivenNum(message['from']) == True and database.hasGivenMessage(message['from']) == True) or (message['body'] == 'Retry' and database.hasPaid(message['from']) == True)): if twilio_api.sendsms(database.getPhoneNumber(message['from']), message['body']): responses.append({ 'type': 'text', 'to': message['from'], 'body': 'SMS Sent: \n To: ' + database.getPhoneNumber(message['from']) + '\n Message: ' + message['body'] + '\n What would you like to do next?', 'suggestedResponses': ['Send another message'] }) database.setGivenMessage(message['from'], 'false') database.setGivenNum(message['from'], 'false') database.setHasPaid(message['from'],'false') else: responses.append({ 'type': 'text', 'to': message['from'], 'body': 'I could not send your message for some reason. What would you like to do?', 'suggestedResponses': ['Change my message','Retry'] }) database.setGivenMessage(message['from'], 'false') database.setGivenNum(message['from'], 'false') else: responses.append({ 'type': 'text', 'to': message['from'], 'body': 'I\'m not sure what you\'re trying to tell me. Please provide a valid command such as \'Send a new message\'.' }) elif message['type'] == 'picture': responses.append({ 'type': 'text', 'to': message['from'], 'body': 'There\'s not much I can do with this picture...' }) if responses: # send the responses through the Chat Engine API requests.post( 'https://engine.apikik.com/api/v1/message', auth=(os.environ['USERNAME'], os.environ['API_KEY']), json={'messages': responses} ) return Response(status=200)