File size: 4,031 Bytes
007ec3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a901bc8
 
 
 
 
 
a8ebc1e
 
 
 
28c0a86
a8ebc1e
 
 
 
 
 
 
 
 
28c0a86
a8ebc1e
 
 
 
 
 
 
 
 
a901bc8
007ec3d
 
 
 
 
 
a8ebc1e
007ec3d
 
 
 
 
 
 
 
a8ebc1e
007ec3d
 
 
 
 
 
a8ebc1e
007ec3d
 
 
 
 
 
 
a8ebc1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
642be75
 
 
 
 
 
 
 
 
a901bc8
642be75
 
007ec3d
 
 
 
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
import os
import json
import requests

from dotenv import load_dotenv

load_dotenv()

# os.environ.get('SUPABASE_URL')


def generate_message(data_json):
    """ pending

    REQUIREMENTS
    - implement logging of message
    - have a very simple activity which allows for different dialogue
      * add - Add the numbers, 1+1, 2+2
      * subtract - Subtract the numbers, 1-1, 2-2
      * menu - Choose one
    - send message data to retrieve dialogue state
    - retrieve response and build message object
    - send message object

    Need to make util functions that apply to both /nlu and /conversation_manager
    """

    # Intent Labelling #######################
    # Call to Wit.ai for intent recognition

    # message = data_json['messages'][0]['text']['body']
    # formatted_message = message.replace(' ', '%20')

    # Send a custom message with buttons
    headers = {
        'Authorization': f"Bearer {os.environ.get('TURN_AUTHENTICATION_TOKEN')}",
        'Content-Type': 'application/json'
    }

    whatsapp_id = data_json['message']['_vnd']['v1']['chat']['owner'].replace("+","")

    print("DATA JSON")
    print(data_json)

    user_message = data_json['message']['text']['body']

    if user_message == 'add':
        data = {
            "preview_url": False,
            "recipient_type": "individual",
            "to": whatsapp_id,
            "type": "text",
            "text": {
                "body": "What's 2+2?"
            }
        }
    elif user_message == 'substract':
        data = {
            "preview_url": False,
            "recipient_type": "individual",
            "to": whatsapp_id,
            "type": "text",
            "text": {
                "body": "What's 1-1?"
            }
        }    
    else:
        data = {
        "to": whatsapp_id,
        # "to": "alan",
        "type": "interactive",
        "interactive": {
            "type": "button",
            # "header": { },
            "body": {
                "text": "Please choose one of the following options."
            },
            # "footer": { },
            "action": {
                "buttons": [
                    {
                        "type": "reply",
                        "reply": {
                            "id": "inquiry-yes",
                            "title": "add" 
                        }
                    },
                    {
                        "type": "reply",
                        "reply": {
                            "id": "inquiry-no",
                            "title": "subtract" 
                        }
                    }
                ]
            }
        }
    }


    # data = {
    #     "to": whatsapp_id,
    #     # "to": "alan",
    #     "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" 
    #                     }
    #                 }
    #             ]
    #         }
    #     }
    # }

    r = requests.post(f'https://whatsapp.turn.io/v1/messages', data=json.dumps(data), headers=headers)
    print("==================")
    print("Headers")
    print(headers)
    print("Data")
    print(data)
    print("Request Info")
    print(r)
    print("--")
    # print(r.body)
    print("==================")


    context = {"content":{"user":"Alan", "state": "received-and-replied-state"}}

    return context