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 lines55565758595859606162616263646566676465666768696970717254 unmodified lines print(request)
data_dict = await request.json() print("FULL REQUEST") print(data_dict) # print("FULL REQUEST") # print(data_dict)
message_data = data_dict.get('message_data', '') context_data = data_dict.get('context', '') # 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) # print("MESSAGE_DATA") # print(message_data) # print("CONTEXT_DATA") # print(context_data)
context = generate_message(message_data, context_data) context = generate_message(data_dict) return JSONResponse(context)
@app.post("/nlu")8 unmodified lines91011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575812596061629 unmodified lines72737428293031327576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311434115116117118119401201211221234243124125126127128129130131132451334748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979813413513613713813914014110314214314421 unmodified lines1661671681311321331341351361371381391401411421431441451461471481698 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 data
def 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_arr
def 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 data
def generate_message(data_json, context_data):def generate_message(data_json): """ pending
REQUIREMENTS9 unmodified lines Need 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') 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" }
# Send a custom message with buttons headers = { 'Authorization': f"Bearer {os.environ.get('TURN_AUTHENTICATION_TOKEN')}", 'Content-Type': 'application/json' }
whatsapp_id = data_json['message']['_vnd']['v1']['chat']['owner'].replace("+","") 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("DATA JSON") print(data_json) # print("==================") # print("Headers") # print(headers) # print("Data") # print(data) # print("Request Info") # print(r) # print("==================")
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" } } ] } } } 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, # # "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 context # }app.py+9-9
mathtext_fastapi/conversation_manager.py+103-82