File size: 8,224 Bytes
24df821
4cf8d83
 
24df821
4cf8d83
 
 
185d33a
24df821
 
 
4cf8d83
 
 
24df821
 
 
 
4cf8d83
 
3f8ccf9
12058fc
ff85563
271c78c
 
 
 
 
12058fc
271c78c
 
3f8ccf9
 
 
 
 
 
 
ff85563
271c78c
 
3f8ccf9
 
271c78c
 
 
 
 
12058fc
271c78c
 
 
3f8ccf9
 
 
 
 
 
ff85563
3f8ccf9
 
 
 
 
271c78c
3f8ccf9
12058fc
3f8ccf9
271c78c
ff85563
12058fc
ff85563
12058fc
271c78c
 
 
 
 
 
3f8ccf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24df821
12058fc
4cf8d83
271c78c
12058fc
271c78c
e33f267
271c78c
12058fc
271c78c
12058fc
 
 
3f8ccf9
 
 
 
 
24df821
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f8ccf9
24df821
 
 
3f8ccf9
 
 
 
 
 
 
 
 
ff85563
254976b
 
 
 
 
 
 
ff85563
3f8ccf9
 
 
 
 
 
 
 
271c78c
 
 
12058fc
 
271c78c
 
12058fc
271c78c
 
 
 
 
 
 
 
 
 
 
 
e5a763d
ff85563
e5a763d
 
24df821
185d33a
 
 
ff85563
12058fc
ff85563
24df821
 
12058fc
4cf8d83
 
 
 
 
b439c42
271c78c
3f8ccf9
 
12058fc
ff85563
 
12058fc
 
b439c42
271c78c
 
 
ff85563
 
 
 
271c78c
 
 
3f8ccf9
 
 
e7eca0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff85563
e7eca0d
 
 
 
 
 
ff85563
e7eca0d
 
 
 
 
ff85563
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
import dill as pickle
import os
import json
import random
import requests

from dotenv import load_dotenv
from mathtext_fastapi.nlu import evaluate_message_with_nlu
from math_quiz_fsm import MathQuizFSM

from transitions import Machine

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 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 user_message == 'add':

        fsm_check = SUPA.table('state_machines').select("*").eq(
            "uuid",
            contact_uuid
        ).execute()

        if fsm_check.data == []:
            math_quiz_state_machine = MathQuizFSM()
            messages = [math_quiz_state_machine.response_text]
            dump = pickle.dumps(math_quiz_state_machine)

            # TODO: Check how to save - JSONB?
            SUPA.table('state_machines').insert(dump).execute()
        else:
            math_quiz_state_machine = pickle.loads(fsm_check.data['add'])
            math_quiz_state_machine.student_answer
            messages = math_quiz_state_machine.validate()
            dump = pickle.dumps(math_quiz_state_machine)            
            SUPA.table('state_machines').update(dump).eq(
                "contact_uuid", contact_uuid
            ).execute()

        message_package = {
            'messages': messages,
            'input_prompt': "temporary value",
            'state': "addition-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"
        }
    elif 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"
        }
    return 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)

    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)
        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
    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"
    #                     }
    #                 }
    #             ]
    #         }
    #     }
    # }