-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_callback.py
executable file
·300 lines (188 loc) · 9.09 KB
/
send_callback.py
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
import requests
import json
# from speech_to_text_algorithm import *
# from pytimedinput import timedInput
######################################################## GET TRACKER
def get_tracker(headers):
response = requests.get('http://localhost:5005/conversations/tester/tracker', headers=headers)
if (response.status_code==200) and (response.headers["content-type"].strip().startswith("application/json")):
print(response)
print("tracker oook")
my_json = response.content.decode('utf8').replace("'", '"')#.replace("\\", "\\\\")
print("response ok")
# Load the JSON to a Python list & dump it back out as formatted JSON
data = json.loads(my_json)
sender_id = data["sender_id"]
slots = data["slots"]
latest_message=data["latest_message"]
events = data["events"]
latest_action = data["latest_action"]
followup_action = data["followup_action"]
return sender_id, slots, latest_message, events, latest_action, followup_action
else:
return [],[],[],[],[],[]
##################################### USER INPUT
def user_input(headers,message):
#message=m.encode('utf-8')
dct = {"sender": "tester", "message": message}
data = json.dumps(dct, indent=True)
response = requests.post('http://localhost:5005/webhooks/callback/webhook', headers=headers, data=data)
if response.status_code==200:
print("response ok")
# print(response.headers["content-type"].strip().startswith("application/json")) # einai false se autin tin periptwsi
#get_tacker
sender_id, slots, latest_message, events, latest_action, followup_action = get_tracker(headers)
return sender_id, slots, latest_message, events, latest_action, followup_action
else:
return [],[],[],[],[],[]
def trigger_intent(headers,data):
response = requests.post('http://localhost:5005/conversations/tester/trigger_intent', headers=headers, data=data)
if response.status_code==200:
print(response)
my_json = response.content.decode('utf8').replace("'", '"')
#print(my_json)
# Load the JSON to a Python list & dump it back out as formatted JSON
data = json.loads(my_json)
tracker = data["tracker"]
messages= data["messages"]
sender_id = tracker["sender_id"]
slots = tracker["slots"]
latest_message=tracker["latest_message"]
events = tracker["events"]
followup_action = tracker["followup_action"]
latest_action = tracker["latest_action"]
return sender_id, slots, latest_message, events, messages, latest_action, followup_action
else:
return [],[],[],[],[],[],[]
def set_slot(headers,data):
params = (('include_events', 'NONE'),)
#localhost rasa
response = requests.post('http://localhost:5005/conversations/tester/tracker/events', params=params, headers=headers, data=data)
my_json = response.content.decode('utf8').replace("'", '"')
if response.status_code==200:
print("response ok")
#get_tacker
sender_id, slots, latest_message, events, latest_action, followup_action = get_tracker(headers)
return sender_id, slots, latest_message, events, latest_action, followup_action
else:
return [],[],[],[],[],[]
def execute_action(headers,data):
response = requests.post('http://localhost:5005/conversations/tester/execute', headers=headers, data=data)
if response.status_code==200:
print(response)
my_json = response.content.decode('utf8').replace("'", '"')
# Load the JSON to a Python list & dump it back out as formatted JSON
data = json.loads(my_json)
# tracker = data["tracker"]
# messages= data["messages"]
sender_id, slots, latest_message, events, latest_action, followup_action = get_tracker(headers)
return sender_id, slots, latest_message, events, latest_action, followup_action
else:
return [],[],[],[],[],[]
def bot_response(events,len_bot_events_old):
bot_events = [x for x in events if x["event"]=="bot"]
diff = len(bot_events)-len_bot_events_old
print(len(bot_events))
print(len_bot_events_old)
print(diff)
len_bot_events_old = len(bot_events)
bot_events = bot_events[-1:-1-diff:-1]
bot_responses = [x["text"] for x in bot_events]
# print(bot_responses)
bot_responses = bot_responses[::-1]
# print(bot_responses)
return bot_responses, len_bot_events_old
def restart(headers):
data = '{"name":"action_reset_slots"}'
sender_id, slots, latest_message, events, latest_action, followup_action = execute_action(headers,data)
len_bot_events_old = 0
return len_bot_events_old
def talk_to_bot():
headers = {'Content-type': 'application/json',}
len_bot_events_old = restart(headers)
# data = '{"name":"action_set_slot_exhibit"}'
# sender_id, slots, latest_message, events, latest_action, followup_action = execute_action(headers,data)
# # ################## TRIGGER TO START #########################
# data = '{"name": "intent_start_conversation"}'
# sender_id, slots, latest_message, events, messages, latest_action, followup_action = trigger_intent(headers,data)
# print('--'*10,'sender id','--'*10)
# print(sender_id)
# print('--'*10,'slots','--'*10)
# print(slots)
# print('--'*10,'latest message','--'*10)
# print(latest_message)
# print('--'*10,'latest action','--'*10)
# print(latest_action)
# print('--'*10,'messages','--'*10)
# print(messages)
# print('--'*10,'followup_action','--'*10)
# print(followup_action)
# print('--'*10,'events','--'*10)
# for event in events:
# print('--'*10,'single event','--'*10)
# print(event)
# print('--'*50)
# bot_responses, len_bot_events_old = bot_response(events,len_bot_events_old)
# print('--'*10,'bot responses','--'*10)
# for response in bot_responses:
# print(response)
duration = 20 #seconds
while True:
##################################### USER INPUT ################################
# message, timedOut = timedInput("περιμένω user input: ",duration)
message = input("user input: ")
#beep
# message = speech_to_text(duration) #duration is how much time wait//2*duration total length of speech
#beep
if message=="exit":
break
if not message:
message="Σιωπή"
sender_id, slots, latest_message, events, latest_action, followup_action = user_input(headers,message)
print('--'*10,'sender id','--'*10)
print(sender_id)
print('--'*10,'slots','--'*10)
print(slots)
print('--'*10,'latest message','--'*10)
print(latest_message)
print('--'*10,'latest action','--'*10)
print(latest_action)
print('--'*10,'followup_action','--'*10)
print(followup_action)
print('--'*10,'events','--'*10)
for event in events:
print('--'*10,'single event','--'*10)
print(event)
print('--'*50)
bot_responses, len_bot_events_old = bot_response(events,len_bot_events_old)
print('--'*10,'bot responses','--'*10)
for response in bot_responses:
print(response)
print('--'*10,'slots','--'*10)
print(slots)
def talk_to_bot_via_rest():
headers = {'Content-type': 'application/json',}
while True:
##################################### USER INPUT ################################
message = input("user input: ")
if message=="exit":
break
#message=m.encode('utf-8')
dct = {"sender": "tester", "message": message}
data = json.dumps(dct, indent=True)
response = requests.post('http://localhost:5005/webhooks/rest/webhook', headers=headers, data=data)
print(response)
#
print(response.headers)
if (response.status_code==200) and (response.headers["content-type"].strip().startswith("application/json")):
print("response ok")
my_json = response.content.decode('utf8').replace("'", '"')
# Load the JSON to a Python list & dump it back out as formatted JSON
data = json.loads(my_json)
for bot_message in data:
recipient_id = bot_message["recipient_id"]
text = bot_message["text"]
print("'" + recipient_id +"': " + text)
if __name__ == "__main__":
talk_to_bot()
# talk_to_bot_via_rest()