Greg Thompson commited on
Commit
9be6a6a
1 Parent(s): d14bd4c

Update the conversation manager's inputs from and outputs to Turn.io

Browse files
mathtext_fastapi/v2_conversation_manager.py CHANGED
@@ -153,21 +153,53 @@ def lookup_local_state(next_state):
153
  return microlesson
154
 
155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  def manage_conversation_response(data_json):
157
  """ Calls functions necessary to determine message and context data """
158
  print("V2 ENDPOINT")
159
 
160
  user_message = ''
 
 
 
 
 
 
 
 
 
161
 
162
  # nlu_response = evaluate_message_with_nlu(message_data)
163
 
164
- context_data = {
165
- 'contact_uuid': 'abcdefg',
166
- 'current_state': 'N1.1.1_G2',
167
- 'user_message': '1',
168
- 'local_state': ''
169
- }
170
  print("STEP 1")
 
171
  if not context_data['current_state']:
172
  context_data['current_state'] = 'N1.1.1_G1'
173
 
@@ -191,6 +223,35 @@ def manage_conversation_response(data_json):
191
 
192
  microlesson_content = retrieve_microlesson_content(context_data, context_data['user_message'], microlesson, context_data['contact_uuid'])
193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  print("STEP 4")
195
  # combine microlesson content and context_data object
196
- return context_data
 
 
 
 
 
 
 
 
 
 
 
 
153
  return microlesson
154
 
155
 
156
+ def create_text_message(message_text, whatsapp_id):
157
+ """ Fills a template with input values to send a text message to Whatsapp
158
+
159
+ Inputs
160
+ - message_text: str - the content that the message should display
161
+ - whatsapp_id: str - the message recipient's phone number
162
+
163
+ Outputs
164
+ - message_data: dict - a preformatted template filled with inputs
165
+ """
166
+ message_data = {
167
+ "preview_url": False,
168
+ "recipient_type": "individual",
169
+ "to": whatsapp_id,
170
+ "type": "text",
171
+ "text": {
172
+ "body": message_text
173
+ }
174
+ }
175
+ return message_data
176
+
177
+
178
  def manage_conversation_response(data_json):
179
  """ Calls functions necessary to determine message and context data """
180
  print("V2 ENDPOINT")
181
 
182
  user_message = ''
183
+ # whatsapp_id = data_json['author_id']
184
+ message_data = data_json['message_data']
185
+ context_data = data_json['context_data']
186
+ whatsapp_id = message_data['author_id']
187
+ print("MESSAGE DATA")
188
+ print(message_data)
189
+ print("CONTEXT DATA")
190
+ print(context_data)
191
+ print("=================")
192
 
193
  # nlu_response = evaluate_message_with_nlu(message_data)
194
 
195
+ # context_data = {
196
+ # 'contact_uuid': 'abcdefg',
197
+ # 'current_state': 'N1.1.1_G2',
198
+ # 'user_message': '1',
199
+ # 'local_state': ''
200
+ # }
201
  print("STEP 1")
202
+ print(data_json)
203
  if not context_data['current_state']:
204
  context_data['current_state'] = 'N1.1.1_G1'
205
 
 
223
 
224
  microlesson_content = retrieve_microlesson_content(context_data, context_data['user_message'], microlesson, context_data['contact_uuid'])
225
 
226
+ headers = {
227
+ 'Authorization': f"Bearer {os.environ.get('TURN_AUTHENTICATION_TOKEN')}",
228
+ 'Content-Type': 'application/json'
229
+ }
230
+
231
+ # Send all messages for the current state before a user input prompt (text/button input request)
232
+ for message in microlesson_content['messages']:
233
+ data = create_text_message(message, whatsapp_id)
234
+
235
+ print("data")
236
+ print(data)
237
+
238
+ r = requests.post(
239
+ f'https://whatsapp.turn.io/v1/messages',
240
+ data=json.dumps(data),
241
+ headers=headers
242
+ )
243
+
244
  print("STEP 4")
245
  # combine microlesson content and context_data object
246
+
247
+ updated_context = {
248
+ "context": {
249
+ "contact_id": whatsapp_id,
250
+ "contact_uuid": context_data['contact_uuid'],
251
+ "state": microlesson_content['state'],
252
+ "bot_message": microlesson_content['input_prompt'],
253
+ "user_message": user_message,
254
+ "type": 'ask'
255
+ }
256
+ }
257
+ return updated_context
scripts/make_request.py CHANGED
@@ -22,7 +22,11 @@ def add_message_text_to_sample_object(message_text):
22
  message_data = '{' + f'"author_id": "+57787919091", "author_type": "OWNER", "contact_uuid": "j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09", "message_body": "{message_text}", "message_direction": "inbound", "message_id": "4kl209sd0-a7b8-2hj3-8563-3hu4a89b32", "message_inserted_at": "2023-01-10T02:37:28.477940Z", "message_updated_at": "2023-01-10T02:37:28.487319Z"' + '}'
23
  # context_data = '{' + '"user":"", "state":"addition-question-sequence", "bot_message":"", "user_message":"{message_text}"' + '}'
24
 
25
- context_data = '{' + '"user":"", "state":"start-conversation", "bot_message":"", "user_message":"{message_text}"' + '}'
 
 
 
 
26
 
27
  # context_data = '{' + '"user":"", "state":"addition-question-sequence", "bot_message":"", "user_message":"{message_text}","text": "What is 2+3?","question_numbers": [4,3],"right_answer": 7,"number_correct": 2, "number_incorrect": 0, "hints_used": 0, "level": "easy"' + '}'
28
 
@@ -76,6 +80,7 @@ def run_simulated_request(endpoint, sample_answer, context=None):
76
  # run_simulated_request('nlu', 'Today is a wonderful day')
77
  # run_simulated_request('nlu', 'IDK 5?')
78
  run_simulated_request('v2/manager', '')
 
79
  # run_simulated_request('manager', '')
80
  # run_simulated_request('manager', 'add')
81
  # run_simulated_request('manager', 'subtract')
 
22
  message_data = '{' + f'"author_id": "+57787919091", "author_type": "OWNER", "contact_uuid": "j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09", "message_body": "{message_text}", "message_direction": "inbound", "message_id": "4kl209sd0-a7b8-2hj3-8563-3hu4a89b32", "message_inserted_at": "2023-01-10T02:37:28.477940Z", "message_updated_at": "2023-01-10T02:37:28.487319Z"' + '}'
23
  # context_data = '{' + '"user":"", "state":"addition-question-sequence", "bot_message":"", "user_message":"{message_text}"' + '}'
24
 
25
+ # V1
26
+ # context_data = '{' + '"user":"", "state":"start-conversation", "bot_message":"", "user_message":"{message_text}"' + '}'
27
+
28
+ #V2
29
+ context_data = '{' + '"contact_uuid": "j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09", "current_state":"", "local_state": "", "user_message":""' + '}'
30
 
31
  # context_data = '{' + '"user":"", "state":"addition-question-sequence", "bot_message":"", "user_message":"{message_text}","text": "What is 2+3?","question_numbers": [4,3],"right_answer": 7,"number_correct": 2, "number_incorrect": 0, "hints_used": 0, "level": "easy"' + '}'
32
 
 
80
  # run_simulated_request('nlu', 'Today is a wonderful day')
81
  # run_simulated_request('nlu', 'IDK 5?')
82
  run_simulated_request('v2/manager', '')
83
+ run_simulated_request('v2/manager', '5')
84
  # run_simulated_request('manager', '')
85
  # run_simulated_request('manager', 'add')
86
  # run_simulated_request('manager', 'subtract')