This repository has been archived by the owner on Jan 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
application.py
360 lines (315 loc) · 12 KB
/
application.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
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
import os
import sys
import random
import logging
import telegram
import time
import json
import sqlite3
import pygal
import emoji
from time import sleep
from telegram import Bot, User, ReplyKeyboardRemove
from telegram.ext import Updater, CommandHandler, Dispatcher,\
MessageHandler, Filters
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
from bot.communication import Communication
from bot.config_reader import retrieve_default
from bot.communication import Communication
from bot.dbutils import DBUtils as dbu
class Application:
"""
The chatbot per se, it contains the Communication class to deal with
oncoming messages and also handles all Telegram related commands.
Might soon have a sibling to deal with Facebook.
"""
def __init__(self, token, train=True, use_watson=True):
if train:
self.comm = Communication(use_watson)
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO,
)
self.emotion_handler = False
self.logger = logging.getLogger("LOG")
self.app = Bot(token)
self.updater = Updater(token)
self.dispatcher = self.updater.dispatcher
start_handler = CommandHandler("start", self.start)
self.dispatcher.add_handler(start_handler)
info_handler = CommandHandler("info", self.info)
self.dispatcher.add_handler(info_handler)
helpme_handler = CommandHandler("helpme", self.helpme)
self.dispatcher.add_handler(helpme_handler)
contatos_handler = CommandHandler("contatos", self.contatos)
self.dispatcher.add_handler(contatos_handler)
emotion_handler = CommandHandler("emotion", self.emotional_state)
self.dispatcher.add_handler(emotion_handler)
emotions_chart_handler = CommandHandler(
"emotionchart", self.emotional_state_chart
)
self.dispatcher.add_handler(emotions_chart_handler)
message_handler = MessageHandler(Filters.text, self.text_message)
self.dispatcher.add_handler(message_handler)
self.dispatcher.add_error_handler(self.error)
def send_type_action(self, bot, update):
"""
Shows status typing when sending message
"""
bot.send_chat_action(
chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING
)
sleep(1)
def start(self, bot, update):
"""
Start command to receive /start message on Telegram.
@bot = information about the bot
@update = the user info.
"""
self.send_type_action(bot, update)
name = update.message["chat"]["first_name"]
start_text = (
f"Olá {name}, eu sou o Rabot."
"\nUm robô bem simpático criado para alegrar seu dia!\n"
)
bot.send_message(chat_id=update.message.chat_id, text=start_text)
start_text = (
"Se quiser saber mais sobre mim ou meus criadores "
"basta digitar `/info` ;)"
)
bot.send_message(
chat_id=update.message.chat_id,
text=start_text,
parse_mode=telegram.ParseMode.MARKDOWN,
)
start_text = "Agora vamos lá. Em que posso ajudá-lo?"
bot.send_message(
chat_id=update.message.chat_id,
text=start_text,
reply_markup=telegram.ReplyKeyboardRemove()
)
return 0
def info(self, bot, update):
"""
Info command to know more about the developers.
@bot = information about the bot
@update = the user info.
"""
self.send_type_action(bot, update)
with open(f"{os.getcwd()}/bot/dialogs/commands/info.md")\
as info_file:
info_text = info_file.read()
bot.send_message(
chat_id=update.message.chat_id,
text=info_text,
parse_mode=telegram.ParseMode.MARKDOWN,
)
return 0
return 1
def helpme(self, bot, update):
"""
Helpme command to show information about help sources
for people in critical situations
"""
bot.send_chat_action(
chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING
)
with open(f"{os.getcwd()}/bot/dialogs/commands/help.md") as help_file:
helpme_text = help_file.read()
bot.send_message(
chat_id=update.message.chat_id,
text=helpme_text,
parse_mode=telegram.ParseMode.MARKDOWN,
)
def contatos(self, bot, update):
"""
Shows all contact centers to the bot user
"""
self.send_type_action(bot, update)
with open(f"{os.getcwd()}/bot/dialogs/contacts.md") as contatos_file:
contatos_text = contatos_file.read()
bot.send_message(
chat_id=update.message.chat_id,
text=contatos_text,
parse_mode=telegram.ParseMode.MARKDOWN,
)
def text_message(self, bot, update):
self.send_type_action(bot, update)
if not self.check_for_emotion(update):
message = update.effective_message.text
update.effective_message.reply_text(
str(self.comm.respond(message))
)
return 0
def check_for_emotion(self, update):
if self.emotion_handler:
self.emotional_state_collect_emotion(
update,
update.effective_message.text
)
if len(update.effective_message.text) <= 1:
return True
return False
def emotional_state_collect_emotion(self, update, msg_text):
"""
When the user chooses an option based on how he or she is feeling
a certain moment. This method is called to evaluate which option
has been choosen and then call the method responsible for storing
the information.
@update = the user info.
@msg_text = the option the user has choosen
"""
with open("bot/dialogs/emotions.json", "r") as rf:
data = json.load(rf)
response = random.choice(data[emoji.demojize(msg_text)]["statements"])
response = str(response)
emotion_type = int(data[emoji.demojize(msg_text)]["emotion_type"])
update.effective_message.reply_text(response)
update.effective_message.reply_text(
"Em breve, eu te apresentarei um diário com maiores informações.",
reply_markup=ReplyKeyboardRemove(),
)
self.emotion_handler = False
self.emotional_state_store_emotion(update, emotion_type)
return 0
def emotional_state_store_emotion(self, update, emotion_type):
"""
This method stores the emotion the user are feeling in a certain
moment in a database.
@emotion_type = a numeric value which is associated with an emotion
"""
user_id = str(update.message.from_user.id)
datetime_statement = time.time()
connection = dbu.db_connect("emotions.sqlite3")
dbu.create_emotion(
connection,
user_id,
datetime_statement,
emotion_type
)
connection.close()
return 0
def emotional_state(self, bot, update):
"""
Asks how the user is feeling in a certain moment
@bot = information about the bot
@update = the user info.
"""
self.send_type_action(bot, update)
name = update.message["chat"]["first_name"]
start_text = (
f"{name}, eu gostaria de saber como você está se sentindo agora\n"
)
bot.send_message(chat_id=update.message.chat_id, text=start_text)
faces_keyboard = []
with open("bot/dialogs/emotions.json", "r") as rf:
data = json.load(rf)
for item in data:
faces_keyboard.append(
[{"text": emoji.emojize(item, use_aliases=True)}]
)
# faces_keyboard = [
# [{"text": emoji.emojize(":laughing:", use_aliases=True)}],
# [{"text": emoji.emojize(":smile:", use_aliases=True)}],
# [{"text": emoji.emojize(
# ":expressionless_face:", use_aliases=True
# )}],
# [{"text": emoji.emojize(":disappointed:", use_aliases=True)}],
# [{"text": emoji.emojize(":angry_face:", use_aliases=True)}],
# ]
bot.send_message(
chat_id=update.message.chat_id,
text="Qual dessas caras exprime melhor o seu estado atual?\n",
reply_markup={
"keyboard": faces_keyboard,
"resize_keyboard": False,
"one_time_keyboard": True,
},
)
self.emotion_handler = True
return 0
def emotional_state_chart(self, bot, update):
"""
Generate charts based on the emotional reports the user has sent
@bot = information about the bot
@update = the user info.
"""
self.send_type_action(bot, update)
bot.send_message(
chat_id=update.message.chat_id,
text="Vou mostrar para você o que tenho até o momento.",
)
# make a query to the sqlite3 database to retrieve the recorded
# information about the user's current emotional state
connector = dbu.db_connect("emotions.sqlite3")
rows = dbu.select_emotions_count(
connector,
str(update.message.from_user.id)
)
if rows is None or len(rows) == 0:
bot.send_message(
chat_id=update.message.chat_id,
text="Parece que você ainda não utilizou o comando /emotion"
"para que você possa me dizer algumas informações.",
)
return 0
emotions = {
1: "Raiva",
2: "Triste",
3: "Normal",
4: "Bom",
5: "Excelente"
}
pie_chart = pygal.Pie(
half_pie=True,
legend_at_bottom=True,
print_values=True,
style=pygal.style.LightSolarizedStyle,
)
pie_chart.title = "Gráfico das suas ultimas emoções"
for row in rows:
pie_chart.add(emotions[row[0]], row[1])
chart_name = f"output_pie_{str(update.message.from_user.id)}.png"
rd = pie_chart.render_to_png(chart_name)
bot.send_photo(
chat_id=update.message.chat_id,
photo=open(chart_name, "rb")
)
if os.path.exists(chart_name):
os.remove(chart_name)
return 0
def error(self, bot, update, error):
self.logger.warning(f'Update "{update}" caused error "{error}"')
return 0
def run(self):
# Start the Bot
self.updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
self.updater.idle()
return 0
if __name__ == "__main__":
# Variables set on Heroku
TOKEN = os.environ.get("TOKEN")
NAME = os.environ.get("NAME")
# Port is given by Heroku
PORT = os.environ.get("PORT")
if TOKEN is not None:
bot = Application(TOKEN, use_watson=False)
bot.updater.start_webhook(
listen="0.0.0.0",
port=int(PORT),
url_path=TOKEN)
bot.updater.bot.set_webhook(f"https://{NAME}.herokuapp.com/{TOKEN}")
bot.updater.idle()
# Run on local system once detected that it's not on Heroku
else:
try:
token = retrieve_default("TELEGRAM")["token"]
watson = eval(retrieve_default()["IBM Watson"])
x = Application(token=token, use_watson=watson)
x.run()
except FileNotFoundError:
print("Configuration file not found.")
sys.exit(1)