Greg Thompson commited on
Commit
249f836
1 Parent(s): 63d32ce

Update conversation_manager to handle sending multiple messages

Browse files
Files changed (2) hide show
  1. app.py +9 -9
  2. mathtext_fastapi/conversation_manager.py +103 -82
app.py CHANGED
@@ -55,18 +55,18 @@ async def programmatic_message_manager(request: Request):
55
  print(request)
56
 
57
  data_dict = await request.json()
58
- print("FULL REQUEST")
59
- print(data_dict)
60
 
61
- message_data = data_dict.get('message_data', '')
62
- context_data = data_dict.get('context', '')
63
 
64
- print("MESSAGE_DATA")
65
- print(message_data)
66
- print("CONTEXT_DATA")
67
- print(context_data)
68
 
69
- context = generate_message(message_data, context_data)
70
  return JSONResponse(context)
71
 
72
  @app.post("/nlu")
 
55
  print(request)
56
 
57
  data_dict = await request.json()
58
+ # print("FULL REQUEST")
59
+ # print(data_dict)
60
 
61
+ # message_data = data_dict.get('message_data', '')
62
+ # context_data = data_dict.get('context', '')
63
 
64
+ # print("MESSAGE_DATA")
65
+ # print(message_data)
66
+ # print("CONTEXT_DATA")
67
+ # print(context_data)
68
 
69
+ context = generate_message(data_dict)
70
  return JSONResponse(context)
71
 
72
  @app.post("/nlu")
mathtext_fastapi/conversation_manager.py CHANGED
@@ -9,7 +9,54 @@ load_dotenv()
9
  # os.environ.get('SUPABASE_URL')
10
 
11
 
12
- def generate_message(data_json, context_data):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  """ pending
14
 
15
  REQUIREMENTS
@@ -25,82 +72,73 @@ def generate_message(data_json, context_data):
25
  Need to make util functions that apply to both /nlu and /conversation_manager
26
  """
27
 
28
- # Intent Labelling #######################
29
- # Call to Wit.ai for intent recognition
30
-
31
- # message = data_json['messages'][0]['text']['body']
32
- # formatted_message = message.replace(' ', '%20')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
- # Send a custom message with buttons
35
  headers = {
36
  'Authorization': f"Bearer {os.environ.get('TURN_AUTHENTICATION_TOKEN')}",
37
  'Content-Type': 'application/json'
38
  }
39
 
40
- whatsapp_id = data_json['message']['_vnd']['v1']['chat']['owner'].replace("+","")
 
 
41
 
42
- print("DATA JSON")
43
- print(data_json)
 
 
 
 
 
 
44
 
45
- user_message = data_json['message']['text']['body']
46
 
47
- if user_message == 'add':
48
- data = {
49
- "preview_url": False,
50
- "recipient_type": "individual",
51
- "to": whatsapp_id,
52
- "type": "text",
53
- "text": {
54
- "body": "What's 2+2?"
55
- }
56
- }
57
- elif user_message == 'substract':
58
- data = {
59
- "preview_url": False,
60
- "recipient_type": "individual",
61
- "to": whatsapp_id,
62
- "type": "text",
63
- "text": {
64
- "body": "What's 1-1?"
65
- }
66
- }
67
- else:
68
- data = {
69
- "to": whatsapp_id,
70
- # "to": "alan",
71
- "type": "interactive",
72
- "interactive": {
73
- "type": "button",
74
- # "header": { },
75
- "body": {
76
- "text": "Please choose one of the following options."
77
- },
78
- # "footer": { },
79
- "action": {
80
- "buttons": [
81
- {
82
- "type": "reply",
83
- "reply": {
84
- "id": "inquiry-yes",
85
- "title": "add"
86
- }
87
- },
88
- {
89
- "type": "reply",
90
- "reply": {
91
- "id": "inquiry-no",
92
- "title": "subtract"
93
- }
94
- }
95
- ]
96
- }
97
- }
98
- }
99
 
100
 
101
  # data = {
102
  # "to": whatsapp_id,
103
- # # "to": "alan",
104
  # "type": "interactive",
105
  # "interactive": {
106
  # "type": "button",
@@ -128,21 +166,4 @@ def generate_message(data_json, context_data):
128
  # ]
129
  # }
130
  # }
131
- # }
132
-
133
- r = requests.post(f'https://whatsapp.turn.io/v1/messages', data=json.dumps(data), headers=headers)
134
- print("==================")
135
- print("Headers")
136
- print(headers)
137
- print("Data")
138
- print(data)
139
- print("Request Info")
140
- print(r)
141
- print("--")
142
- # print(r.body)
143
- print("==================")
144
-
145
-
146
- context = {"context":{"user":"524754272", "state": "received-and-replied-state"}}
147
-
148
- return context
 
9
  # os.environ.get('SUPABASE_URL')
10
 
11
 
12
+ def create_text_message(message_text, whatsapp_id):
13
+ data = {
14
+ "preview_url": False,
15
+ "recipient_type": "individual",
16
+ "to": whatsapp_id,
17
+ "type": "text",
18
+ "text": {
19
+ "body": message_text
20
+ }
21
+ }
22
+ return data
23
+
24
+ def create_button_objects(button_options):
25
+ button_arr = []
26
+ for option in button_options:
27
+ button_choice = {
28
+ "type": "reply",
29
+ "reply": {
30
+ "id": "inquiry-yes",
31
+ "title": "add"
32
+ }
33
+ }
34
+ button_arr.append(button_choice)
35
+ return button_arr
36
+
37
+ def create_interactive_message(message_text, button_options, whatsapp_id):
38
+
39
+ button_arr = create_button_objects(button_options)
40
+
41
+ data = {
42
+ "to": whatsapp_id,
43
+ "type": "interactive",
44
+ "interactive": {
45
+ "type": "button",
46
+ # "header": { },
47
+ "body": {
48
+ "text": message_text
49
+ },
50
+ # "footer": { },
51
+ "action": {
52
+ "buttons": button_arr
53
+ }
54
+ }
55
+ }
56
+ return data
57
+
58
+
59
+ def generate_message(data_json):
60
  """ pending
61
 
62
  REQUIREMENTS
 
72
  Need to make util functions that apply to both /nlu and /conversation_manager
73
  """
74
 
75
+ message_data = data_json.get('message_data', '')
76
+ context_data = data_json.get('context', '')
77
+
78
+ whatsapp_id = message_data['message']['_vnd']['v1']['chat']['owner'].replace("+","")
79
+ user_message = message_data['message']['text']['body']
80
+
81
+ if user_message == '':
82
+ message_package = {
83
+ 'messages': [],
84
+ 'input_prompt': "Welcome to our math practice. What would you like to try? Type add or subtract.",
85
+ 'state': "welcome-sequence"
86
+ }
87
+ elif user_message == 'add':
88
+ message_package = {
89
+ 'messages': [
90
+ "Great, let's do some addition",
91
+ "First, we'll start with single digits.",
92
+ "Type your response as a number. For example, for '1 + 1', you'd write 2."
93
+ ],
94
+ 'input_prompt': "Here's the first one... What's 2+2?",
95
+ 'state': "add-question-sequence"
96
+ }
97
+ elif user_message == 'subtract':
98
+ message_package = {
99
+ 'messages': [
100
+ "Time for some subtraction!",
101
+ "Type your response as a number. For example, for '1 - 1', you'd write 0."
102
+ ],
103
+ 'input_prompt': "Here's the first one... What's 3-1?",
104
+ 'state': "subtract-question-sequence"
105
+ }
106
+ else:
107
+ message_package = {
108
+ 'messages': [
109
+ "Hmmm...sorry friend. I'm not really sure what to do."
110
+ ],
111
+ 'input_prompt': "Please type add or subtract to start a math activity.",
112
+ 'state': "reprompt-menu-options"
113
+ }
114
 
 
115
  headers = {
116
  'Authorization': f"Bearer {os.environ.get('TURN_AUTHENTICATION_TOKEN')}",
117
  'Content-Type': 'application/json'
118
  }
119
 
120
+ for message in message_package['messages']:
121
+ data = create_text_message(message, whatsapp_id)
122
+ r = requests.post(f'https://whatsapp.turn.io/v1/messages', data=json.dumps(data), headers=headers)
123
 
124
+ # print("==================")
125
+ # print("Headers")
126
+ # print(headers)
127
+ # print("Data")
128
+ # print(data)
129
+ # print("Request Info")
130
+ # print(r)
131
+ # print("==================")
132
 
 
133
 
134
+ context = {"context":{"user": whatsapp_id, "state": message_package['state'], "bot_message": message_package['input_prompt'], "user_message": user_message}}
135
+
136
+ return context
137
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
 
139
 
140
  # data = {
141
  # "to": whatsapp_id,
 
142
  # "type": "interactive",
143
  # "interactive": {
144
  # "type": "button",
 
166
  # ]
167
  # }
168
  # }
169
+ # }