File size: 14,288 Bytes
b71b7ec
 
007ec3d
 
b71b7ec
 
cc67cbe
007ec3d
 
 
48c823d
7c00589
47c0422
284b919
cc67cbe
007ec3d
47c0422
 
 
007ec3d
 
cc67cbe
 
 
 
007ec3d
 
249f836
8acf519
fbc5903
f549aa3
 
 
 
 
8acf519
f549aa3
 
249f836
 
 
 
 
 
 
fbc5903
f549aa3
 
249f836
 
f549aa3
 
 
 
 
8acf519
f549aa3
 
 
249f836
 
 
 
 
 
fbc5903
249f836
 
 
 
 
f549aa3
249f836
8acf519
249f836
f549aa3
fbc5903
8acf519
fbc5903
8acf519
f549aa3
 
 
 
 
 
249f836
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b71b7ec
 
 
 
 
b825f41
47c0422
bf90676
 
 
 
 
c6f9773
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf90676
47c0422
 
 
 
bf90676
 
 
 
 
47c0422
bf90676
c6f9773
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf90676
47c0422
bf90676
 
 
 
 
 
 
 
47c0422
bf90676
 
 
 
 
b71b7ec
47c0422
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc67cbe
8acf519
007ec3d
f549aa3
8acf519
f549aa3
a9b208c
f549aa3
8acf519
f549aa3
8acf519
 
 
249f836
 
 
 
b1bc7e0
 
cc67cbe
47c0422
 
 
c6f9773
 
cc67cbe
b1bc7e0
 
 
 
47c0422
c6f9773
 
 
 
 
 
47c0422
c6f9773
 
 
47c0422
 
 
 
 
 
 
 
 
b1bc7e0
43f7f8e
 
249f836
cc67cbe
43f7f8e
b1bc7e0
249f836
47c0422
 
 
 
 
 
 
 
 
b1bc7e0
6335789
 
 
 
 
 
fbc5903
249f836
 
 
 
 
 
 
 
47c0422
f549aa3
 
47c0422
 
 
f549aa3
8acf519
 
f549aa3
 
8acf519
f549aa3
 
 
 
 
 
 
 
 
 
 
 
038006a
fbc5903
038006a
 
cc67cbe
48c823d
 
 
fbc5903
47c0422
 
 
 
 
 
 
 
 
 
 
 
007ec3d
 
 
 
 
a901bc8
f549aa3
249f836
 
43f7f8e
 
 
 
8acf519
fbc5903
 
8acf519
 
a901bc8
f549aa3
47c0422
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f549aa3
249f836
 
 
a8ebc1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbc5903
a8ebc1e
 
 
 
 
 
fbc5903
a8ebc1e
 
 
 
 
fbc5903
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import base64
import dill
import os
import json
import jsonpickle
import pickle
import random
import requests

from dotenv import load_dotenv
from mathtext_fastapi.nlu import evaluate_message_with_nlu
from mathtext_fastapi.math_quiz_fsm import MathQuizFSM
from mathtext_fastapi.math_subtraction_fsm import MathSubtractionFSM
from supabase import create_client
from transitions import Machine

from scripts.quiz.generators import start_interactive_math
from scripts.quiz.hints import generate_hint

load_dotenv()

SUPA = create_client(
    os.environ.get('SUPABASE_URL'),
    os.environ.get('SUPABASE_KEY')
)


def create_text_message(message_text, whatsapp_id):
    """ Fills a template with input values to send a text message to Whatsapp

    Inputs
    - message_text: str - the content that the message should display
    - whatsapp_id: str - the message recipient's phone number

    Outputs
    - message_data: dict - a preformatted template filled with inputs
    """
    message_data = {
        "preview_url": False,
        "recipient_type": "individual",
        "to": whatsapp_id,
        "type": "text",
        "text": {
            "body": message_text
        }
    }
    return message_data


def create_button_objects(button_options):
    """ Creates a list of button objects using the input values
    Input
    - button_options: list - a list of text to be displayed in buttons

    Output
    - button_arr: list - preformatted button objects filled with the inputs

    NOTE: Not fully implemented and tested
    """
    button_arr = []
    for option in button_options:
        button_choice = {
            "type": "reply",
            "reply": {
                "id": "inquiry-yes",
                "title": option['text']
            }
        }
        button_arr.append(button_choice)
    return button_arr


def create_interactive_message(message_text, button_options, whatsapp_id):
    """ Fills a template to create a button message for Whatsapp

    * NOTE: Not fully implemented and tested
    * NOTE/TODO: It is possible to create other kinds of messages
                 with the 'interactive message' template
    * Documentation:
      https://whatsapp.turn.io/docs/api/messages#interactive-messages

    Inputs
    - message_text: str - the content that the message should display
    - button_options: list - what each button option should display
    - whatsapp_id: str - the message recipient's phone number
    """
    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 pickle_and_encode_state_machine(state_machine):
    dump = pickle.dumps(state_machine)
    dump_encoded = base64.b64encode(dump).decode('utf-8')
    return dump_encoded


def manage_math_quiz_fsm(user_message, contact_uuid, type):
    fsm_check = SUPA.table('state_machines').select("*").eq(
        "contact_uuid",
        contact_uuid
    ).execute()

    # This doesn't allow for when one FSM is present and the other is empty
    """
    1
    data=[] count=None
    
    2
    data=[{'id': 29, 'contact_uuid': 'j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09', 'addition3': None, 'subtraction': None, 'addition': 

    - but problem is there is no subtraction , but it's assuming there's a subtration


    Cases
    - make a completely new record
    - update an existing record with an existing FSM
    - update an existing record without an existing FSM
    """


    # Make a completely new entry
    if fsm_check.data == []:
        if type == 'addition':
            math_quiz_state_machine = MathQuizFSM()
        else:
            math_quiz_state_machine = MathSubtractionFSM()
        messages = [math_quiz_state_machine.response_text]
        dump_encoded = pickle_and_encode_state_machine(math_quiz_state_machine)

        SUPA.table('state_machines').insert({
            'contact_uuid': contact_uuid,
            f'{type}': dump_encoded
        }).execute()
    # Update an existing record with a new state machine
    elif not fsm_check.data[0][type]:
        if type == 'addition':
            math_quiz_state_machine = MathQuizFSM()
        else:
            math_quiz_state_machine = MathSubtractionFSM()
        messages = [math_quiz_state_machine.response_text]
        dump_encoded = pickle_and_encode_state_machine(math_quiz_state_machine)

        SUPA.table('state_machines').update({
            f'{type}': dump_encoded
        }).eq(
            "contact_uuid", contact_uuid
        ).execute()      
    # Update an existing record with an existing state machine
    elif fsm_check.data[0][type]:
        undump_encoded = base64.b64decode(
            fsm_check.data[0][type].encode('utf-8')
        )
        math_quiz_state_machine = pickle.loads(undump_encoded)

        math_quiz_state_machine.student_answer = user_message
        math_quiz_state_machine.correct_answer = str(math_quiz_state_machine.correct_answer)
        messages = math_quiz_state_machine.validate_answer()
        dump_encoded = pickle_and_encode_state_machine(math_quiz_state_machine)          
        SUPA.table('state_machines').update({
            f'{type}': dump_encoded
        }).eq(
            "contact_uuid", contact_uuid
        ).execute()
    return messages


def use_quiz_module_approach(user_message, context_data):
    print("USER MESSAGE")
    print(user_message)
    print("=======================")
    if user_message == 'add':
        context_result = start_interactive_math()
        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': context_result['text'],
            'state': "addition-question-sequence"
        }

    elif user_message == context_data.get('right_answer'):
        context_result = start_interactive_math(
            context_data['number_correct'],
            context_data['number_incorrect'],
            context_data['level']
        )
        message_package = {
            'messages': [
                "That's right, great!",
            ],
            'input_prompt': context_result['text'],
            'state': "addition-question-sequence"
        }
    else:
        context_result = generate_hint(
            context_data['question_numbers'],
            context_data['right_answer'],
            context_data['number_correct'],
            context_data['number_incorrect'],
            context_data['level'],
            context_data['hints_used']
        )
        message_package = {
            'messages': [
                context_result['text'],
            ],
            'input_prompt': context_data['text'],
            'state': "addition-question-sequence"
        }
    return message_package, context_result


def return_next_conversational_state(context_data, user_message, contact_uuid):
    """ Evaluates the conversation's current state to determine the next state

    Input
    - context_data: dict - data about the conversation's current state
    - user_message: str - the message the user sent in response to the state

    Output
    - message_package: dict - a series of messages and prompt to send
    """
    if context_data['user_message'] == '' and \
       context_data['state'] == 'start-conversation':
        message_package = {
            'messages': [],
            'input_prompt': "Welcome to our math practice.  What would you like to try?  Type add or subtract.",
            'state': "welcome-sequence"
        }
    elif context_data['state'] == 'addition-question-sequence' or \
        user_message == 'add':

        # Used in FSM
        # messages = manage_math_quiz_fsm(user_message, contact_uuid)

        # message_package, context_result = use_quiz_module_approach(user_message, context_data)
        messages = manage_math_quiz_fsm(user_message, contact_uuid, 'addition')

        if user_message == 'exit':
            state_label = 'exit'
        else:
            state_label = 'addition-question-sequence'
        # Used in FSM
        input_prompt = messages.pop()
        message_package = {
            'messages': messages,
            'input_prompt': input_prompt,
            'state': state_label
        }

        # Used in quiz w/ hints
        # context_data = context_result
        # message_package['state'] = state_label

    elif context_data['state'] == 'subtraction-question-sequence' or \
        user_message == 'subtract':
        messages = manage_math_quiz_fsm(user_message, contact_uuid, 'subtraction')

        if user_message == 'exit':
            state_label = 'exit'
        else:
            state_label = 'subtraction-question-sequence'

        input_prompt = messages.pop()

        message_package = {
            'messages': messages,
            'input_prompt': input_prompt,
            'state': state_label
        }

        # 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"
        # }
    elif context_data['state'] == 'exit' or user_message == 'exit':
        message_package = {
            'messages': [
                "Great, thanks for practicing math today.  Come back any time."
            ],
            'input_prompt': "",
            'state': "exit"
        }
    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"
        }
    # Used in FSM
    return message_package

    # Used in quiz folder approach
    # return context_result, message_package


def manage_conversation_response(data_json):
    """ Calls functions necessary to determine message and context data to send

    Input
    - data_json: dict - message data from Turn.io/Whatsapp

    Output
    - context: dict - a record of the state at a given point a conversation

    TODOs
    - implement logging of message
    - test interactive messages
    - review context object and re-work to use a standardized format
    - review ways for more robust error handling
    - need 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_data', '')

    whatsapp_id = message_data['author_id']
    user_message = message_data['message_body']
    contact_uuid = message_data['contact_uuid']

    # TODO: Need to incorporate nlu_response into wormhole by checking answers against database (spreadsheet?)
    nlu_response = evaluate_message_with_nlu(message_data)

    if context_data['state'] == 'addition':
        context_result, message_package = return_next_conversational_state(
            context_data,
            user_message,
            contact_uuid
        )
    else:
        message_package = return_next_conversational_state(
            context_data,
            user_message,
            contact_uuid
        )

    headers = {
        'Authorization': f"Bearer {os.environ.get('TURN_AUTHENTICATION_TOKEN')}",
        'Content-Type': 'application/json'
    }

    # Send all messages for the current state before a user input prompt (text/button input request)
    for message in message_package['messages']:
        data = create_text_message(message, whatsapp_id)

        print("data")
        print(data)

        r = requests.post(
            f'https://whatsapp.turn.io/v1/messages',
            data=json.dumps(data),
            headers=headers
        )

    # Update the context object with the new state of the conversation
    if context_data['state'] == 'addition':
        context = {
            "context": {
                "user": whatsapp_id,
                "state": message_package['state'],
                "bot_message": message_package['input_prompt'],
                "user_message": user_message,
                "type": 'ask',
                # Necessary for quiz folder approach
                "text": context_result.get('text'),
                "question_numbers": context_result.get('question_numbers'),
                "right_answer": context_result.get('right_answer'),
                "number_correct": context_result.get('number_correct'),
                "hints_used": context_result.get('hints_used'),
            }
        }
    else:
        context = {
            "context": {
                "user": whatsapp_id,
                "state": message_package['state'],
                "bot_message": message_package['input_prompt'],
                "user_message": user_message,
                "type": 'ask',
            }
        }

    return context

    # data = {
    #     "to": whatsapp_id,
    #     "type": "interactive",
    #     "interactive": {
    #         "type": "button",
    #         # "header": { },
    #         "body": {
    #             "text": "Did I answer your question?"
    #         },
    #         # "footer": { },
    #         "action": {
    #             "buttons": [
    #                 {
    #                     "type": "reply",
    #                     "reply": {
    #                         "id": "inquiry-yes",
    #                         "title": "Yes"
    #                     }
    #                 },
    #                 {
    #                     "type": "reply",
    #                     "reply": {
    #                         "id": "inquiry-no",
    #                         "title": "No"
    #                     }
    #                 }
    #             ]
    #         }
    #     }
    # }