-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreplyBot.py
More file actions
61 lines (46 loc) · 1.7 KB
/
replyBot.py
File metadata and controls
61 lines (46 loc) · 1.7 KB
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
# coding=utf-8
from telegram.ext import Updater
from telegram.ext import CommandHandler
from telegram.ext import MessageHandler, Filters
import random
# Funciones.
def test(bot, update):
bot.sendMessage(chat_id=update.message.chat_id,
text="Bip bup bop... Funcionando!!!")
def reply(bot, update):
try:
# Obtengo el texto que mandaron
# Get the text the user sent
text = update.message.text
print (text)
if any(x in text for x in listaMenciones):
# Enviar respuesta
# Send back the result
bot.sendMessage(chat_id=update.message.chat_id,
text=random.choice (listaRespuestas))
except UnicodeEncodeError:
bot.sendMessage(chat_id=update.message.chat_id,
text="Error")
# Setiar el token del bot en el updater
# Set the bot's token
updater = Updater(token='TOKEN')
dispatcher = updater.dispatcher
# Respuestas que quiere que el bot de
# Answers List
listaRespuestas = ["RESPUESTAS"]
# Palabras a las que el bot va a reaccionar
# Words that will trigger the bot
menciones = ["Hi","No","Yes","Oui"]
# convertir a unicode - Convert to unicode
listaMenciones = [i.decode('UTF-8') if isinstance(i, basestring) else i for i in menciones]
# Con los CommandHandler se llaman los comandos con "/"
# CommandHanlder will react to words that begins with "/"
start_handler = CommandHandler('test', test)
# Con los MessageHandler recibe todo lo que se escribe.
# With MessageHandler the bot will receive all the text.
reply_handler = MessageHandler([Filters.text], reply)
# Agregar los handlers al dispatcher
# Add handlers to dispatcher
dispatcher.add_handler(test_handler)
dispatcher.add_handler(reply_handler)
updater.start_polling()