Spaces:
Runtime error
Runtime error
Greg Thompson commited on
Commit ·
3f8ccf9
1
Parent(s): c13c2b5
Update conversation_manager to handle sending multiple messages
Browse filesFiles changed (2)
54 unmodified lines55565758596061626364656667686970717254 unmodified linesprint(request)data_dict = await request.json()print("FULL REQUEST")print(data_dict)message_data = data_dict.get('message_data', '')context_data = data_dict.get('context', '')print("MESSAGE_DATA")print(message_data)print("CONTEXT_DATA")print(context_data)context = generate_message(message_data, context_data)return JSONResponse(context)@app.post("/nlu")54 unmodified lines55565758596061626364656667686970717254 unmodified linesprint(request)data_dict = await request.json()# print("FULL REQUEST")# print(data_dict)# message_data = data_dict.get('message_data', '')# context_data = data_dict.get('context', '')# print("MESSAGE_DATA")# print(message_data)# print("CONTEXT_DATA")# print(context_data)context = generate_message(data_dict)return JSONResponse(context)@app.post("/nlu")
8 unmodified lines91011121314159 unmodified lines25262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510621 unmodified lines1281291301311321331341351361371381391401411421431441451461471488 unmodified lines# os.environ.get('SUPABASE_URL')def generate_message(data_json, context_data):""" pendingREQUIREMENTS9 unmodified linesNeed to make util functions that apply to both /nlu and /conversation_manager"""# Intent Labelling ######################## Call to Wit.ai for intent recognition# message = data_json['messages'][0]['text']['body']# formatted_message = message.replace(' ', '%20')# Send a custom message with buttonsheaders = {'Authorization': f"Bearer {os.environ.get('TURN_AUTHENTICATION_TOKEN')}",'Content-Type': 'application/json'}whatsapp_id = data_json['message']['_vnd']['v1']['chat']['owner'].replace("+","")print("DATA JSON")print(data_json)user_message = data_json['message']['text']['body']if user_message == 'add':data = {"preview_url": False,"recipient_type": "individual","to": whatsapp_id,"type": "text","text": {"body": "What's 2+2?"}}elif user_message == 'substract':data = {"preview_url": False,"recipient_type": "individual","to": whatsapp_id,"type": "text","text": {"body": "What's 1-1?"}}else:data = {"to": whatsapp_id,# "to": "alan","type": "interactive","interactive": {"type": "button",# "header": { },"body": {"text": "Please choose one of the following options."},# "footer": { },"action": {"buttons": [{"type": "reply","reply": {"id": "inquiry-yes","title": "add"}},{"type": "reply","reply": {"id": "inquiry-no","title": "subtract"}}]}}}# data = {# "to": whatsapp_id,# # "to": "alan",# "type": "interactive",# "interactive": {# "type": "button",21 unmodified lines# ]# }# }# }r = requests.post(f'https://whatsapp.turn.io/v1/messages', data=json.dumps(data), headers=headers)print("==================")print("Headers")print(headers)print("Data")print(data)print("Request Info")print(r)print("--")# print(r.body)print("==================")context = {"context":{"user":"524754272", "state": "received-and-replied-state"}}return context8 unmodified lines910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061629 unmodified lines7273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314421 unmodified lines1661671681698 unmodified lines# os.environ.get('SUPABASE_URL')def create_text_message(message_text, whatsapp_id):data = {"preview_url": False,"recipient_type": "individual","to": whatsapp_id,"type": "text","text": {"body": message_text}}return datadef create_button_objects(button_options):button_arr = []for option in button_options:button_choice = {"type": "reply","reply": {"id": "inquiry-yes","title": "add"}}button_arr.append(button_choice)return button_arrdef create_interactive_message(message_text, button_options, whatsapp_id):button_arr = create_button_objects(button_options)data = {"to": whatsapp_id,"type": "interactive","interactive": {"type": "button",# "header": { },"body": {"text": message_text},# "footer": { },"action": {"buttons": button_arr}}}return datadef generate_message(data_json):""" pendingREQUIREMENTS9 unmodified linesNeed to make util functions that apply to both /nlu and /conversation_manager"""message_data = data_json.get('message_data', '')context_data = data_json.get('context', '')whatsapp_id = message_data['message']['_vnd']['v1']['chat']['owner'].replace("+","")user_message = message_data['message']['text']['body']if user_message == '':message_package = {'messages': [],'input_prompt': "Welcome to our math practice. What would you like to try? Type add or subtract.",'state': "welcome-sequence"}elif user_message == 'add':message_package = {'messages': ["Great, let's do some addition","First, we'll start with single digits.","Type your response as a number. For example, for '1 + 1', you'd write 2."],'input_prompt': "Here's the first one... What's 2+2?",'state': "add-question-sequence"}elif user_message == 'subtract':message_package = {'messages': ["Time for some subtraction!","Type your response as a number. For example, for '1 - 1', you'd write 0."],'input_prompt': "Here's the first one... What's 3-1?",'state': "subtract-question-sequence"}else:message_package = {'messages': ["Hmmm...sorry friend. I'm not really sure what to do."],'input_prompt': "Please type add or subtract to start a math activity.",'state': "reprompt-menu-options"}headers = {'Authorization': f"Bearer {os.environ.get('TURN_AUTHENTICATION_TOKEN')}",'Content-Type': 'application/json'}for message in message_package['messages']:data = create_text_message(message, whatsapp_id)r = requests.post(f'https://whatsapp.turn.io/v1/messages', data=json.dumps(data), headers=headers)# print("==================")# print("Headers")# print(headers)# print("Data")# print(data)# print("Request Info")# print(r)# print("==================")context = {"context":{"user": whatsapp_id, "state": message_package['state'], "bot_message": message_package['input_prompt'], "user_message": user_message}}return context# data = {# "to": whatsapp_id,# "type": "interactive",# "interactive": {# "type": "button",21 unmodified lines# ]# }# }# }
app.py+9-9
mathtext_fastapi/conversation_manager.py+103-82