-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDicVenBot.py
212 lines (151 loc) · 7.11 KB
/
DicVenBot.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# DicVenBot - Diccionario Venezolano
#
##########################################################################
"""DicVenBot - Diccionario Venezolano
La intención de este bot de Telegram es proveer términos en el vocabulario del venezolano.
Es una fase inicial y he extraído la data desde diferentes fuentes de internet.
Es probable que muchos términos no se encuentren por lo que se puede ir agregando.
Cuando no consigue un término, responde de manera muy venezolana :-)
Para buscar: /quevainaes {término a buscar}
ejemplo: /quevainaes **vaina**
NOTA: El autor no se hace responsable por los términos, ya que han sido recopilados de diferentes fuentes de internet.
Asdrúbal R. Velásquez Lagrave
Twitter: @Visionario
"""
from telegram import ParseMode
from telegram.ext import (Updater, CommandHandler, MessageHandler,
Filters, RegexHandler, ConversationHandler)
from functools import wraps
import logging, logging.handlers
import os, time, sys, random, json
#Import constants
from constants import LOGFILE, TOKEN, ADMINS
from constants import DATAFILE, RESPUESTAS_NEGATIVAS_NEUTRAL
from constants import MSG_START, MSG_AYUDA
# ABOUT DATA
__author__ = 'Asdrubal R. Velasquez Lagrave <[email protected]>'
__twitter__ = '@Visionario'
__copyright__ = 'CopyLeft'
__credits__ = ['']
__license__ = 'GPLv3.0'
__version__ = '0.1'
__maintainer__ = 'Asdrubal R. Velasquez Lagrave'
__email__ = '[email protected]'
__status__ = 'BETA - UNSTABLE'
# Enable logging / Create the Logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# Create the Handler for logging data to a file
logger_handler = logging.handlers.WatchedFileHandler(LOGFILE)
logger_handler.setLevel(logging.INFO)
# Create a Formatter for formatting the log messages
logger_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# Add the Formatter to the Handler
logger_handler.setFormatter(logger_formatter)
# Add the Handler to the Logger
logger.addHandler(logger_handler)
# SCREEN LOG
logger_handler_screen = logging.StreamHandler()
logger_handler_screen.setLevel(logging.DEBUG)
logger.addHandler(logger_handler_screen)
# DATAFILE es el archivo de datos que se encuentra en json
with open(DATAFILE) as data_file:
DATA = json.load(data_file)
# https://github.com/python-telegram-bot/python-telegram-bot/wiki/Code-snippets#restrict-access-to-a-handler-decorator
def restricted(func):
@wraps(func)
def wrapped(bot, update, *args, **kwargs):
user_id = update.effective_user.id
if user_id not in ADMINS:
print("Unauthorized access denied for {}.".format(user_id))
return
return func(bot, update, *args, **kwargs)
return wrapped
# Comando Start.
def start(bot, update):
chat_id = update.message.chat_id
user = update.message.from_user
update.message.reply_text(MSG_START % user.first_name, parse_mode=ParseMode.MARKDOWN)
logger.info("START: chat_id:%s user_id:%s Usuario:%s Username:@%s data:%s" % (chat_id, user.id, user.first_name, user.username, update.message.text))
# Ayuda
def ayuda(bot, update):
chat_id = update.message.chat_id #Chat ID
user_id = update.message.from_user.id #User ID
user = update.message.from_user #user
# Haga un Log de esto
logger.info("Texto: chat_id:%s user_id:%s Usuario:%s Username:@%s data:%s" % (chat_id, user_id, user.first_name, user.username, update.message.text))
update.message.reply_text(MSG_AYUDA, parse_mode=ParseMode.MARKDOWN)
# Se recibe el parámetro a buscar
def quevainaes(bot, update):
chat_id = update.message.chat_id #Chat ID
user_id = update.message.from_user.id #User ID
user = update.message.from_user #user
# WHOAMI? (bot)
ME = bot.getMe()
text = update.message.text
# Verifique si el comando viene con sufijo (/[email protected]) y prepare
if (ME['username'] in text):
text = text.split(ME['username'])[1][1:]
else:
text = text[len(text.split()[0])+1:]
if not text: # Se recibió un comando sin texto
return # ignore
# Haga un Log de esto
logger.info("Texto: chat_id:%s user_id:%s Usuario:%s Username:@%s data:%s" % (chat_id, user_id, user.first_name, user.username, update.message.text))
try: # Intenta buscar
data = DATA[text.lower()]
# Envíe el resultado (si lo encontró)
header = "👇***" + text.capitalize() + "***👇\n"
update.message.reply_text(header + data, parse_mode=ParseMode.MARKDOWN)
except:
# Respuesta aleatoria, elegida desde el archivo de constantes
# Se intenta hacerlo un poco jocoso dada la temática de este bot
# Existen tres conatantes que se pueden usar
# RESPUESTAS_NEGATIVAS_NEUTRAL
# TODO: RESPUESTAS_NEGATIVAS_MASCULINO
# TODO: RESPUESTAS_NEGATIVAS_FEMENINO
# Solo en una futura versión se le puede preguntar al usuario si es
# hombre o mujer para afinar las respuestas del bot adecuadamente
r = RESPUESTAS_NEGATIVAS_NEUTRAL[random.randrange(0,len(RESPUESTAS_NEGATIVAS_NEUTRAL))]
update.message.reply_text(r, parse_mode=ParseMode.MARKDOWN)
logger.info("NO EXISTE: chat_id:%s user_id:%s Usuario:%s Username:@%s data:%s" % (chat_id, user_id, user.first_name, user.username, update.message.text))
# Restart
# Restringido solo para ADMIN (Constante ADMINS)
@restricted
def restart(bot, update ):
chat_id = update.message.chat_id #Chat ID
user_id = update.message.from_user.id #User ID
user = update.message.from_user #user
bot.send_message(update.message.chat_id, "Bot is restarting...")
logger.info("RESTART FROM: chat_id:%s user_id:%s Usuario:%s Username:@%s data:%s" % (chat_id, user_id, user.first_name, user.username, update.message.text))
time.sleep(0.5)
os.execl(sys.executable, sys.executable, *sys.argv)
def error(bot, update, error):
logger.warning('Update "%s" caused error "%s"' % (update, error))
# Main function
def main():
# Create the EventHandler and pass it your bot's token.
updater = Updater(TOKEN)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("restart", restart))
dp.add_handler(CommandHandler("ayuda", ayuda))
dp.add_handler(CommandHandler(["q","quevainaes","quevergaes","buscar","define"], quevainaes))
# log all errors
dp.add_error_handler(error)
# Start the Bot
updater.start_polling()
# NOTIFY ADMINS BOT IS ONLINE
for admin in ADMINS:
updater.bot.send_message(admin, "@" + updater.bot.getMe().username + " En línea!!")
logger.info("BOT " + "@" + updater.bot.getMe().username + " INICIADO ")
# 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.
updater.idle()
if __name__ == '__main__':
main()