Spaces:
Runtime error
Runtime error
Greg Thompson
commited on
Commit
•
2887020
1
Parent(s):
a0fb1a2
Updated v2_conversation_manager to handle message_package from data-driven quiz
Browse files
mathtext_fastapi/v2_conversation_manager.py
CHANGED
@@ -105,9 +105,8 @@ def manage_math_quiz_fsm(user_message, contact_uuid, type):
|
|
105 |
|
106 |
|
107 |
def retrieve_microlesson_content(context_data, user_message, microlesson, contact_uuid):
|
108 |
-
|
109 |
-
|
110 |
-
microlesson == 'addition':
|
111 |
messages = manage_math_quiz_fsm(user_message, contact_uuid, 'addition')
|
112 |
|
113 |
if user_message == 'exit':
|
@@ -121,11 +120,12 @@ def retrieve_microlesson_content(context_data, user_message, microlesson, contac
|
|
121 |
'input_prompt': input_prompt,
|
122 |
'state': state_label
|
123 |
}
|
124 |
-
elif context_data['local_state'] =='addition2' or microlesson == 'addition2':
|
125 |
if user_message == 'harder' or user_message == 'easier':
|
126 |
user_message = ''
|
127 |
message_package = num_one.process_user_message(contact_uuid, user_message)
|
128 |
message_package['state'] = 'addition2'
|
|
|
129 |
|
130 |
elif context_data['local_state'] == 'subtraction-question-sequence' or \
|
131 |
user_message == 'subtract' or \
|
@@ -187,11 +187,11 @@ def manage_conversation_response(data_json):
|
|
187 |
""" Calls functions necessary to determine message and context data """
|
188 |
print("V2 ENDPOINT")
|
189 |
|
190 |
-
user_message = ''
|
191 |
# whatsapp_id = data_json['author_id']
|
192 |
message_data = data_json['message_data']
|
193 |
context_data = data_json['context_data']
|
194 |
whatsapp_id = message_data['author_id']
|
|
|
195 |
print("MESSAGE DATA")
|
196 |
print(message_data)
|
197 |
print("CONTEXT DATA")
|
@@ -208,20 +208,24 @@ def manage_conversation_response(data_json):
|
|
208 |
# }
|
209 |
print("STEP 1")
|
210 |
print(data_json)
|
|
|
211 |
if not context_data['current_state']:
|
212 |
context_data['current_state'] = 'N1.1.1_G1'
|
|
|
213 |
|
214 |
curriculum_copy = copy.deepcopy(gsm.curriculum)
|
215 |
-
|
216 |
print("STEP 2")
|
217 |
-
if
|
218 |
curriculum_copy.left()
|
219 |
next_state = curriculum_copy.state
|
220 |
-
elif
|
221 |
curriculum_copy.right()
|
222 |
next_state = curriculum_copy.state
|
223 |
else:
|
224 |
next_state = context_data['current_state']
|
|
|
|
|
225 |
|
226 |
print("STEP 3")
|
227 |
microlesson = lookup_local_state(next_state)
|
@@ -229,7 +233,7 @@ def manage_conversation_response(data_json):
|
|
229 |
print("microlesson")
|
230 |
print(microlesson)
|
231 |
|
232 |
-
microlesson_content = retrieve_microlesson_content(context_data,
|
233 |
|
234 |
headers = {
|
235 |
'Authorization': f"Bearer {os.environ.get('TURN_AUTHENTICATION_TOKEN')}",
|
@@ -240,8 +244,8 @@ def manage_conversation_response(data_json):
|
|
240 |
for message in microlesson_content['messages']:
|
241 |
data = create_text_message(message, whatsapp_id)
|
242 |
|
243 |
-
print("data")
|
244 |
-
print(data)
|
245 |
|
246 |
r = requests.post(
|
247 |
f'https://whatsapp.turn.io/v1/messages',
|
@@ -256,10 +260,12 @@ def manage_conversation_response(data_json):
|
|
256 |
"context": {
|
257 |
"contact_id": whatsapp_id,
|
258 |
"contact_uuid": context_data['contact_uuid'],
|
259 |
-
"current_state":
|
|
|
260 |
"bot_message": microlesson_content['input_prompt'],
|
261 |
"user_message": user_message,
|
262 |
"type": 'ask'
|
263 |
}
|
264 |
}
|
|
|
265 |
return updated_context
|
|
|
105 |
|
106 |
|
107 |
def retrieve_microlesson_content(context_data, user_message, microlesson, contact_uuid):
|
108 |
+
# TODO: This is being filtered by both the local and global states, so not changing
|
109 |
+
if microlesson == 'addition':
|
|
|
110 |
messages = manage_math_quiz_fsm(user_message, contact_uuid, 'addition')
|
111 |
|
112 |
if user_message == 'exit':
|
|
|
120 |
'input_prompt': input_prompt,
|
121 |
'state': state_label
|
122 |
}
|
123 |
+
elif context_data['local_state'] == 'addition2' or microlesson == 'addition2':
|
124 |
if user_message == 'harder' or user_message == 'easier':
|
125 |
user_message = ''
|
126 |
message_package = num_one.process_user_message(contact_uuid, user_message)
|
127 |
message_package['state'] = 'addition2'
|
128 |
+
message_package['input_prompt'] = '?'
|
129 |
|
130 |
elif context_data['local_state'] == 'subtraction-question-sequence' or \
|
131 |
user_message == 'subtract' or \
|
|
|
187 |
""" Calls functions necessary to determine message and context data """
|
188 |
print("V2 ENDPOINT")
|
189 |
|
|
|
190 |
# whatsapp_id = data_json['author_id']
|
191 |
message_data = data_json['message_data']
|
192 |
context_data = data_json['context_data']
|
193 |
whatsapp_id = message_data['author_id']
|
194 |
+
user_message = message_data['message_body']
|
195 |
print("MESSAGE DATA")
|
196 |
print(message_data)
|
197 |
print("CONTEXT DATA")
|
|
|
208 |
# }
|
209 |
print("STEP 1")
|
210 |
print(data_json)
|
211 |
+
print(f"1: {context_data['current_state']}")
|
212 |
if not context_data['current_state']:
|
213 |
context_data['current_state'] = 'N1.1.1_G1'
|
214 |
+
print(f"2: {context_data['current_state']}")
|
215 |
|
216 |
curriculum_copy = copy.deepcopy(gsm.curriculum)
|
217 |
+
curriculum_copy.state = context_data['current_state']
|
218 |
print("STEP 2")
|
219 |
+
if user_message == 'easier':
|
220 |
curriculum_copy.left()
|
221 |
next_state = curriculum_copy.state
|
222 |
+
elif user_message == 'harder':
|
223 |
curriculum_copy.right()
|
224 |
next_state = curriculum_copy.state
|
225 |
else:
|
226 |
next_state = context_data['current_state']
|
227 |
+
print("next_state")
|
228 |
+
print(next_state)
|
229 |
|
230 |
print("STEP 3")
|
231 |
microlesson = lookup_local_state(next_state)
|
|
|
233 |
print("microlesson")
|
234 |
print(microlesson)
|
235 |
|
236 |
+
microlesson_content = retrieve_microlesson_content(context_data, user_message, microlesson, context_data['contact_uuid'])
|
237 |
|
238 |
headers = {
|
239 |
'Authorization': f"Bearer {os.environ.get('TURN_AUTHENTICATION_TOKEN')}",
|
|
|
244 |
for message in microlesson_content['messages']:
|
245 |
data = create_text_message(message, whatsapp_id)
|
246 |
|
247 |
+
# print("data")
|
248 |
+
# print(data)
|
249 |
|
250 |
r = requests.post(
|
251 |
f'https://whatsapp.turn.io/v1/messages',
|
|
|
260 |
"context": {
|
261 |
"contact_id": whatsapp_id,
|
262 |
"contact_uuid": context_data['contact_uuid'],
|
263 |
+
"current_state": next_state,
|
264 |
+
"local_state": microlesson_content['state'],
|
265 |
"bot_message": microlesson_content['input_prompt'],
|
266 |
"user_message": user_message,
|
267 |
"type": 'ask'
|
268 |
}
|
269 |
}
|
270 |
+
print(updated_context)
|
271 |
return updated_context
|