This repository has been archived by the owner on Apr 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
224 lines (176 loc) · 6.7 KB
/
app.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
#!/usr/bin/env python3
import logging
logger = logging.getLogger(__name__)
import requests
import os
import pprint
pp = pprint.PrettyPrinter(indent=4)
import connexion
from flask_cors import CORS
from prefclient import PrefStore
PS = PrefStore("matrix_adapter", [
"watson_assistant_version",
"watson_assistant_url",
"watson_assistant_workspace_id",
"watson_assistant_username",
"watson_assistant_password",
"matrix_host",
"matrix_username",
"matrix_password",
"watson_tts_url",
"watson_tts_apikey"
])
from watson_developer_cloud import AssistantV1
WATSON = AssistantV1(
version = PS.get_pref("watson_assistant_version"),
username = PS.get_pref("watson_assistant_username"),
password = PS.get_pref("watson_assistant_password"),
url = PS.get_pref("watson_assistant_url")
)
print(WATSON)
from watson_developer_cloud import TextToSpeechV1
TTS = TextToSpeechV1(
url = PS.get_pref("watson_tts_url"),
iam_apikey = PS.get_pref("watson_tts_apikey")
)
print(TTS)
from bot import BottyMcBotface
MATRIX_BOT = BottyMcBotface(PS.get_pref("matrix_host"), PS.get_pref("matrix_username"), PS.get_pref("matrix_password"))
class CentralNodeConnection:
def __init__(self, base_url):
self.base_url = base_url
def pref_reset(self, user_handle, input_service):
res = requests.delete("{}/preferences/user/{}#{}".format(self.base_url, user_handle, input_service))
assert (res.status_code == 200 or res.status_code == 204)
def user_exists(self, user_handle, input_service):
res = requests.post("{}/request".format(self.base_url), json = {
"skill": "preferences",
"type": "get_user_prefs",
"payload": { "keys": ["name"] },
"user_handle": user_handle,
"input_service": input_service
})
assert res.status_code == 200
if "name" in res.json():
return True
else:
return False
def tts_on(self, user_handle, input_service):
res = requests.post("{}/request".format(self.base_url), json = {
"skill": "preferences",
"type": "get_user_prefs",
"payload": { "keys": ["tts"] },
"user_handle": user_handle,
"input_service": input_service
})
assert res.status_code == 200
json = res.json()
if "tts" in json:
if json["tts"] == "True" or json["tts"] == True or json["tts"] == "true":
return True
else:
return False
else:
return False
def skill_request(self, skill, stype, payload, user_handle, input_service):
print("Skill request: {} {} {} {} {}".format(skill, stype, payload, user_handle, input_service))
res = requests.post("{}/request".format(self.base_url), json = {
"skill": skill,
"type": stype,
"payload": payload,
"user_handle": user_handle,
"input_service": input_service
})
assert res.status_code == 200
return res.json()
CENTRAL_NODE = CentralNodeConnection(os.environ["CENTRAL_NODE_BASE_URL"])
class MatrixAgent:
def __init__(self, central_node, bot, tts, watson, watson_workspace_id):
self.central_node = central_node
self.bot = bot
self.tts = tts
self.watson = watson
self.watson_workspace_id = watson_workspace_id
self.bot.set_receive_handler(self.new_message)
self.bot.set_reset_handler(self.reset)
self.context = dict()
def reset(self, sender, is_pref):
if is_pref:
self.context = dict()
self.central_node.pref_reset(sender, "matrix")
self.bot.send("User preferences ({}#matrix) and context reset...".format(sender))
else:
self.context = dict()
self.bot.send("Context reset...")
def new_message(self, room, sender, content):
print("New message: {} {} {}".format(room, sender, content))
# TODO: Optimize
self.context["is_new_user"] = not self.central_node.user_exists(sender, "matrix")
self.context["timezone"] = "Europe/Berlin"
res = self.watson.message(
workspace_id = self.watson_workspace_id,
input = {
"text": content
},
context = self.context
).get_result()
self.handle_watson_response(res, sender)
def handle_watson_response(self, res, sender):
print("Handle watson response")
pp.pprint(res)
self.context = res["context"]
tts_on = self.central_node.tts_on(sender, "matrix")
for msg in res["output"]["text"]:
self.bot.send(msg)
if tts_on:
self.tts_message(msg)
if "actions" in res:
self.watson_action(res["actions"], sender)
def watson_action(self, actions, sender):
print("Watson action {}".format(actions))
for action in actions:
skill, stype = action["name"].split("/")
skill_res = self.central_node.skill_request(
skill,
stype,
action["parameters"],
sender,
"matrix"
)
self.context[action["result_variable"]] = skill_res
res = self.watson.message(
workspace_id = self.watson_workspace_id,
context = self.context
).get_result()
self.handle_watson_response(res, sender)
def tts_message(self, msg):
res = self.tts.synthesize(msg, accept='audio/mpeg', voice="en-US_AllisonVoice").get_result()
ufile = self.bot.bot.client.api.media_upload(res.content, "audio/mpeg")
print(ufile)
self.bot.bot.client.api.send_content(
self.bot.primary_room.room_id,
ufile["content_uri"],
"bot_tts.m4a",
"m.audio"
)
def proactive_message(self, msg):
print("Proactive message: {}".format(msg))
self.context["info"] = msg
self.context["timezone"] = "Europe/Berlin"
res = self.watson.message(
workspace_id = self.watson_workspace_id,
context = self.context
).get_result()
self.handle_watson_response(res, msg["payload"]["user"])
MATRIX_AGENT = MatrixAgent(CENTRAL_NODE, MATRIX_BOT, TTS, WATSON, PS.get_pref("watson_assistant_workspace_id"))
app = connexion.App(__name__, specification_dir='openapi/')
app.add_api('openapi.yml')
# Set CORS headers
CORS(app.app)
# set the WSGI application callable to allow using uWSGI:
# uwsgi --http :8080 -w app
application = app.app
logger.info('App initialized')
if __name__ == '__main__':
# run our standalone server
app.run(port=8080)