-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimteBot.py
executable file
·665 lines (582 loc) · 30.2 KB
/
SimteBot.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
#! /usr/bin/python3
# -*- coding: utf-8 -*-
"""
Este es el bot de telegram para la coordinación y
manejo de las tareas del grupo,se van a utilizar
como API para la comunicación con Python
python-telegram-bot, esta es la versión para Python3
"""
# import sys
# import imp
import os # Heroku
import pickle
import logging
from telegram import (ReplyKeyboardMarkup, ReplyKeyboardHide, ParseMode)
from telegram.ext import (Updater, CommandHandler, MessageHandler,
Filters, RegexHandler, ConversationHandler)
token = os.environ.get('TOKEN') # Heroku
appname = os.environ.get('APPNAME') # Heroku
port = int(os.environ.get('PORT', '5000')) # Heroku
clave = os.environ.get('CLAVE') # Heroku
# Para evitar problemas con algunos caracteres poco comunes en el servidor
# imp.reload(sys)
# sys.setdefaultencoding('utf8')
# No funciona en python3
class Tarea:
"""Las tareas que va a realizar el grupo"""
def __init__(self, title, shortAbout, about, coordinator, p):
self.title = title
if len(shortAbout.split()) < 11:
self.shortAbout = shortAbout
else:
raise ValueError("Invalid length shortAbout",
shortAbout)
self.about = about
self.coordinator = coordinator
self.group = list()
self.group.append(coordinator)
self.p = int(p)
def edit(self, user, p, about=None):
if user in self.group:
if about is not None:
self.about = about
self.p = int(p)
def add(self, user):
if user not in self.group:
self.group.append(user)
return True
else:
return False
def remove(self, user):
if not user == self.coordinator and user in self.group:
self.group.remove(user)
return True
else:
return False
def delegate(self, user, aux):
if user == self.coordinator:
self.coordinator = aux
return True
else:
return False
def showGroup(self):
cdn = "*Grupo de trabajo:*\n"
for x in self.group:
cdn = cdn + "\t @" + x + "\n"
return cdn
def show(self):
text = "*Titulo:*\t"+self.title
text += "\n*Descripción corta:*\t"+self.shortAbout
text += "\n*Descripción:* \n"+self.about
text += "\n*coordinador:*\n @"+self.coordinator
text += "\n"+self.showGroup()+"\n*Avance* "+str(self.p)+" % "
return text
def showShort(self):
text = "[ <b>"+self.title+"</b>\t"+str(self.p)+"% @"
text += self.coordinator + "\n" + self.shortAbout + "\n<b>"
text += self.showGroup()+"</b>]\n"
return text
# EndClass
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
CHOOSING, OPCION, REPLY, CHOICE, TITLE, DC, DL, COOR, AVAN, DONE = range(10)
filename = clave+".data"
reply_keyboardg = [['/verTareas'],
['salir']]
reply_keyboardp = [['/verTareas', 'Agregar tarea',
'Modificar avance'],
['Unirse', 'Retirarse', 'Delegar'],
['salir']]
ok_keyboard = [['Aceptar', '/Cancelar']]
cancel_keyboard = [['/Cancelar']]
term_key = [['Siguiente']]
markupg = ReplyKeyboardMarkup(reply_keyboardg,
one_time_keyboard=True,
selective=True)
markupp = ReplyKeyboardMarkup(reply_keyboardp, one_time_keyboard=True)
markupo = ReplyKeyboardMarkup(ok_keyboard,
resize_keyboard=True,
one_time_keyboard=True)
markupc = ReplyKeyboardMarkup(cancel_keyboard,
resize_keyboard=True,
one_time_keyboard=True)
markupt = ReplyKeyboardMarkup(term_key,
resize_keyboard=True,
one_time_keyboard=True)
def saveList(obj):
with open(filename, 'wb') as output:
pickle.dump(obj, output, pickle.HIGHEST_PROTOCOL)
def unsaveList():
try:
with open(filename, 'rb') as input:
return pickle.load(input)
except (IOError, OSError):
return list()
TAREAS = unsaveList()
def user_str(user_data):
facts = list()
for key, value in user_data.items():
facts.append('%s - %s' % (key, value))
return "\n".join(facts).join(['\n', '\n'])
def start(bot, update):
teclado = markupg
if(update['message']['chat']['type'] == 'private'):
teclado = markupp
update.message.reply_text(
"Hola, soy *SimteBot* espero poderte ayudar,"
"selecciona alguna de las opciones que aparecen"
" a continuación",
parse_mode=ParseMode.MARKDOWN,
reply_markup=teclado)
return CHOOSING
def listar(bot, update):
teclado = markupg
if(update['message']['chat']['type'] == 'private'):
teclado = markupp
if len(TAREAS) > 0:
for t in TAREAS:
bot.sendMessage(chat_id=update.message.chat_id,
text=t.show(),
parse_mode=ParseMode.MARKDOWN,
reply_markup=teclado)
else:
bot.sendMessage(chat_id=update.message.chat_id,
text="No hay tareas almacenadas",
reply_markup=teclado)
else:
if len(TAREAS) > 0:
bot.sendMessage(chat_id=update.message.chat_id,
text="Las tareas del grupo son:\n",
reply_markup=teclado)
for t in TAREAS:
bot.sendMessage(chat_id=update.message.chat_id,
text=t.showShort(),
parse_mode=ParseMode.HTML,
reply_markup=teclado)
else:
bot.sendMessage(chat_id=update.message.chat_id,
text="No hay tareas almacenadas.",
reply_markup=teclado)
return CHOOSING
def addWork(bot, update, user_data):
update.message.reply_text(
"Vas a crear una nueva tarea tienes que llenar unos"
"datos que vamos a solicitar uno a la vez; si te arrepientes"
" cancela la operación.\n*Agrega el titulo*\n"
"Escribe el *titulo* de la tarea a continuación,"
" intenta ser claro y breve",
parse_mode=ParseMode.MARKDOWN,
reply_markup=markupc)
return TITLE
def addTitle(bot, update, user_data):
text = update.message.text
user_data['Titulo'] = text.upper()
update.message.reply_text("*Agrega tu descripción breve*\n"
"Escribe una descripción breve de tu"
" tarea máximo 10 palabras",
parse_mode=ParseMode.MARKDOWN,
reply_markup=markupc)
return DC
def addDC(bot, update, user_data):
text = update.message.text
if len(text.split(' ')) > 10:
update.message.reply_text("*ERROR*"
"Su descripción corta excede"
" el máximo de *diez* palabras,"
" vuelve a escribirla",
parse_mode=ParseMode.MARKDOWN,
reply_markup=markupc)
return DC
user_data['DescripciónC'] = text
update.message.reply_text("*Agrega la descripción larga*"
"Escribe la descripción de la tarea,"
" intenta ser claro no te excedas del"
" máximo numero de caracteres de Telegram",
parse_mode=ParseMode.MARKDOWN,
reply_markup=markupc)
return DL
def addDL(bot, update, user_data):
text = update.message.text
user_data['DescripciónL'] = text
update.message.reply_text("*Agrega el avance*\n"
"Escribe cuanto llevas de avance en la tarea"
"usa un número entero entre *0* y *100*."
"Donde 100 significa que la tarea esta"
" completada.",
parse_mode=ParseMode.MARKDOWN,
reply_markup=markupc)
return AVAN
def addAvan(bot, update, user_data):
text = update.message.text
if not text.isdigit() or int(text) < 0 or int(text) > 100:
update.message.reply_text("*ERROR*\n"
"Escribe un numero entero entre *0*"
" y *100* no agregues espacios.",
parse_mode=ParseMode.MARKDOWN)
return AVAN
user_data['avance'] = text
user = update.message.from_user.username
user_data['coordinador'] = user
update.message.reply_text("Vamos a crear una tarea %s"
% user_str(user_data),
reply_markup=markupo)
return COOR
def addCoor(bot, update, user_data):
tareatmp = Tarea(user_data['Titulo'],
user_data['DescripciónC'],
user_data['DescripciónL'],
user_data['coordinador'],
user_data['avance'])
TAREAS.append(tareatmp)
saveList(TAREAS)
update.message.reply_text("se agrego exitosamente le tarea"
" \n %s \ngracias " % tareatmp.show(),
parse_mode=ParseMode.MARKDOWN,
reply_markup=markupp)
user_data.clear()
return ConversationHandler.END
def cancelO(bot, update, user_data):
update.message.reply_text("Se cancelo la operación, Adiós",
reply_markup=markupp)
user_data.clear()
return ConversationHandler.END
def showWorks():
nombres = list()
for x, y in enumerate(TAREAS):
nombres.append("<b>%s</b> - %s" % (str(x), y.title))
return "\n".join(nombres).join(['\n', '\n'])
def obtener(bot, update, user_data):
i = update.message.text
if not i.isdigit() or int(i) < 0 or int(i) >= len(TAREAS):
update.message.reply_text("Escriba un número adecuado entre"
" *0* y menor que *%s*" % len(TAREAS),
parse_mode=ParseMode.MARKDOWN,
reply_markup=markupc)
return OPCION
user_data['Tarea'] = TAREAS[int(i)]
user_data['Index'] = int(i)
update.message.reply_text(user_data['msj'],
parse_mode=ParseMode.MARKDOWN,
reply_markup=markupo)
return AVAN
def modAvan(bot, update, user_data):
text = update.message.text
act = user_data['Tarea'].p
if not text.isdigit() or int(text) < act or int(text) > 100:
update.message.reply_text("Escribe un número entero entre %s"
" y 100 no agregues espacios."
% str(act))
return AVAN
user_data['avance'] = int(text)
user = update.message.from_user.username
user_data['usuario'] = user
update.message.reply_text("SI desea cambiar cambiar completamente"
" la descripción larga escríbala a continuación"
" de lo contrario presione la "
" opción Siguiente.\n"
" La descripción actual es:\n %s"
% user_data['Tarea'].about,
parse_mode=ParseMode.MARKDOWN,
reply_markup=markupt)
return DONE
def modDone(bot, update, user_data):
text = update.message.text
if text == 'Siguiente':
user_data['Tarea'].edit(user_data['usuario'],
user_data['avance'])
else:
user_data['Tarea'].edit(user_data['usuario'],
user_data['avance'], about=text)
saveList(TAREAS)
update.message.reply_text("Se modifico la tarea %s"
% user_data['Tarea'].show(),
parse_mode=ParseMode.MARKDOWN,
reply_markup=markupp)
user_data.clear()
return ConversationHandler.END
def modWork(bot, update, user_data):
update.message.reply_text("Desea modificar una de las tareas"
" escriba el número de uno de ellas\n %s"
% showWorks(),
parse_mode=ParseMode.HTML,
reply_markup=markupc)
user_data['msj'] = "Escriba el nuevo avance que tiene la"\
" tarea recuerde que debe ser un numero entero" \
" mayor al avance actual y *100*"
return OPCION
def verTarea(bot, update, user_data):
update.message.reply_text("La tarea a la que se quiere *%s* es:\n"
" %s \n¿esta usted seguro? "
% (user_data['palabra'],
user_data['Tarea'].show()),
parse_mode=ParseMode.MARKDOWN,
reply_markup=markupo)
user_data['usuario'] = update.message.from_user.username
return DONE
def addUser(bot, update, user_data):
if user_data['Tarea'].add(user_data['usuario']):
saveList(TAREAS)
update.message.reply_text("Se realizo la tarea exitosamente %s"
% user_data['Tarea'].show(),
parse_mode=ParseMode.MARKDOWN,
reply_markup=markupp)
else:
update.message.reply_text("Usted ya es parte del equipo",
reply_markup=markupp)
user_data.clear()
return ConversationHandler.END
def addPerson(bot, update, user_data):
update.message.reply_text("Desea unirse a alguna de las tareas del"
" grupo, escriba el número correspondiente"
" a alguna de ellas \n %s" % showWorks(),
parse_mode=ParseMode.HTML,
reply_markup=markupc)
user_data['msj'] = "Desea continuar escriba Aceptar"\
" lo contrario /Cancelar"
user_data['palabra'] = "agregar"
return OPCION
def byeUser(bot, update, user_data):
if user_data['Tarea'].remove(user_data['usuario']):
saveList(TAREAS)
update.message.reply_text("Se realizo la tarea exitosamente %s"
% user_data['Tarea'].show(),
parse_mode=ParseMode.MARKDOWN,
reply_markup=markupp)
else:
update.message.reply_text("Usted no es parte del equipo o es"
" el coordinador",
reply_markup=markupp)
user_data.clear()
return ConversationHandler.END
def byePerson(bot, update, user_data):
update.message.reply_text("Desea retirarse de alguna de las tareas"
" del grupo, escriba el número correspondiente"
" a alguna de ellas \n %s" % showWorks(),
parse_mode=ParseMode.HTML,
reply_markup=markupc)
user_data['msj'] = "Desea continuar escriba Aceptar"\
"de lo contrario /Cancelar"
user_data['palabra'] = "retirar"
return OPCION
def showGroup(bot, update, user_data):
mem = user_data['Tarea'].group
msj = ""
for x, y in enumerate(mem):
msj = msj+'<b>'+str(x)+'</b>'+" - @"+y+"\n"
update.message.reply_text("Seleccione alguno de los miembros"
" del equipo enviando el número"
" correspondiente \n %s" % msj,
parse_mode=ParseMode.HTML,
reply_markup=markupc)
user_data['Group'] = msj
return CHOICE
def selectU(bot, update, user_data):
text = update.message.text
if not text.isdigit() or int(text) < 0 or \
int(text) >= len(user_data['Tarea'].group):
update.message.reply_text("Seleccione una opción valida /n"
" %s" % user_data['Group'],
parse_mode=ParseMode.HTML,
reply_markup=markupc)
return CHOICE
user_data['nuevo'] = user_data['Tarea'].group[int(text)]
update.message.reply_text("Se convertirá el usuario @%s en el"
" coordinador de la tarea %s"
% (user_data['nuevo'], user_data['Tarea'].title),
reply_markup=markupo)
return DONE
def coorUser(bot, update, user_data):
if user_data['Tarea'].delegate(update.message.from_user.username,
user_data['nuevo']):
saveList(TAREAS)
update.message.reply_text("Se realizo la tarea exitosamente %s"
% user_data['Tarea'].show(),
parse_mode=ParseMode.MARKDOWN,
reply_markup=markupp)
else:
update.message.reply_text("Usted no es el coordinador y solo"
" él puede hacer esta operación.",
reply_markup=markupp)
user_data.clear()
return ConversationHandler.END
def passCoor(bot, update, user_data):
update.message.reply_text("Desea delegar la coordinación a"
" otra persona,asegúrese que la persona haga"
" parte del equipo de trabajo de la tarea y"
" escriba el número correspondiente a la tarea"
" que desea\n %s" % showWorks(),
parse_mode=ParseMode.HTML,
reply_markup=markupc)
user_data['msj'] = "¿Desea continuar?"
return OPCION
def salir(bot, update, user_data):
update.message.reply_text("Gracias Adiós",
reply_markup=ReplyKeyboardHide())
print (str(user_data))
user_data.clear()
return ConversationHandler.END
def error(bot, update, error):
logger.warn('Update "%s" causo el error "%s"'
(update, error))
def main():
updater = Updater(token) # Heroku
dp = updater.dispatcher
conv_handler = ConversationHandler(
entry_points=[CommandHandler('start'+clave, start)],
states={
CHOOSING: [CommandHandler('verTareas',
listar),
ConversationHandler(
entry_points=[RegexHandler(
'^Agregar tarea$',
addWork,
pass_user_data=True)],
states={
TITLE: [MessageHandler(
Filters.text,
addTitle,
pass_user_data=True)],
DC: [MessageHandler(
Filters.text,
addDC,
pass_user_data=True)],
DL: [MessageHandler(
Filters.text,
addDL,
pass_user_data=True)],
AVAN: [MessageHandler(
Filters.text,
addAvan,
pass_user_data=True)],
COOR: [RegexHandler(
'^Aceptar$',
addCoor,
pass_user_data=True)],
},
fallbacks=[CommandHandler(
'Cancelar',
cancelO,
pass_user_data=True)]),
ConversationHandler(
entry_points=[RegexHandler(
'^Modificar avance$',
modWork,
pass_user_data=True)],
states={
OPCION: [MessageHandler(
Filters.text,
obtener,
pass_user_data=True)],
AVAN: [MessageHandler(
Filters.text,
modAvan,
pass_user_data=True)],
DONE: [MessageHandler(
Filters.text,
modDone,
pass_user_data=True)]
},
fallbacks=[CommandHandler(
'Cancelar',
cancelO,
pass_user_data=True)]
),
ConversationHandler(
entry_points=[RegexHandler(
'^Unirse$',
addPerson,
pass_user_data=True)],
states={
OPCION: [MessageHandler(
Filters.text,
obtener,
pass_user_data=True)],
AVAN: [MessageHandler(
Filters.text,
verTarea,
pass_user_data=True)],
DONE: [RegexHandler(
'^Aceptar$',
addUser,
pass_user_data=True)]
},
fallbacks=[CommandHandler(
'Cancelar',
cancelO,
pass_user_data=True)]
),
ConversationHandler(
entry_points=[RegexHandler(
'^Retirarse$',
byePerson,
pass_user_data=True)],
states={
OPCION: [MessageHandler(
Filters.text,
obtener,
pass_user_data=True)],
AVAN: [MessageHandler(
Filters.text,
verTarea,
pass_user_data=True)],
DONE:[RegexHandler(
'^Aceptar$',
byeUser,
pass_user_data=True)]
},
fallbacks=[CommandHandler(
'Cancelar',
cancelO,
pass_user_data=True)]
),
ConversationHandler(
entry_points=[RegexHandler(
'^Delegar$',
passCoor,
pass_user_data=True)],
states={
OPCION: [MessageHandler(
Filters.text,
obtener,
pass_user_data=True)],
AVAN: [RegexHandler(
'^Aceptar$',
showGroup,
pass_user_data=True)],
CHOICE: [MessageHandler(
Filters.text,
selectU,
pass_user_data=True)],
DONE: [RegexHandler(
'^Aceptar$',
coorUser,
pass_user_data=True)],
},
fallbacks=[CommandHandler(
'Cancelar',
cancelO,
pass_user_data=True)]
)
],
},
fallbacks=[RegexHandler('^salir$',
salir,
pass_user_data=True)]
)
dp.add_handler(conv_handler)
dp.add_error_handler(error)
updater.start_webhook(listen="0.0.0.0", # Heroku
port=port,
url_path=token)
updater.bot.setWebhook("https://{}.herokuapp.com/{}".format(appname, token)) # Heroku
updater.idle()
if __name__ == '__main__':
main()
"""
Local
if len(sys.argv) < 3:
print ("Error correct method: python Simtebot.py 'token' 'name'")
else:
main(sys.argv[1])
"""