-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.py
245 lines (213 loc) · 8.76 KB
/
bot.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# importing Telegram libs and classes
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from functools import wraps
import logging, os, json, random, time, sys
token = os.environ['TELEGRAM_TOKEN']
LIST_OF_ADMINS = [2740760]
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
"""
This decorator allows you to restrict the access of a handler to only the
user_ids specified in LIST_OF_ADMINS.
Add a @restricted decorator on top of your handler declaration.
"""
def restricted(func):
@wraps(func)
def wrapped(bot, update, *args, **kwargs):
user_id = update.effective_user.id
if user_id not in LIST_OF_ADMINS:
bot.send_message(chat_id=update.message.chat_id,
text="Yo solo recibo ordendes de mi patron")
print("Unauthorized access denied for {}.".format(user_id))
return
return func(bot, update, *args, **kwargs)
return wrapped
# This search for everything except madrazos
def knowledgeG(t):
# open the json file with custom messages
with open('knowledge/data.json') as data_file:
data = json.load(data_file)
# check how many data on specific category
# we set -1 because we need this number
# to get data from array, and positions start
# from 0, not 1
count = len(data[t]) - 1
# select a random number to later select a message
r = random.randint(0, count)
# return the random message from json
return data[t][r]
# This search in json file for madrazos
def knowledgeM(t, who):
# open the json file with custom messages
with open('knowledge/data.json') as data_file:
data = json.load(data_file)
# check how many data on specific category
# we set -1 because we need this number
# to get data from array, and positions start
# from 0, not 1
count = len(data[t][who]) - 1
# select a random number to later select a message
r = random.randint(0, count)
# return the random message from json
return data[t][who][r]
"""
Define a few command handlers. These usually take the two arguments bot and
update. Error handlers also receive the raised TelegramError object in error.
"""
@restricted
def restart(bot, update):
bot.send_message(update.message.chat_id, "Bot is restarting...")
time.sleep(0.2)
os.execl(sys.executable, sys.executable, *sys.argv)
# Let "El Socio" know who you want him to give a "madrazo"
def madrear(bot, update, args):
try:
# let's keep the name for the message
real_name = args[0]
# check which madrazo we need to send
person = args[0]
if person == 'oscar' or person == 'Oscar' or person == 'ozkar':
person = 'oscar'
elif person == 'reina' or person == 'Reina' or person == 'edwin':
person = 'reina'
elif person == 'eddy' or person == 'Eddy':
person = 'eddy'
elif person == 'diego' or person == 'Diego' or person == 'Dieguito' or person == 'dieguito':
person = 'diego'
elif person == 'juan david' or person == 'Juan David' or person == 'ojitos':
person = 'juand'
else:
person = 'general'
# Request a madrazo for someone
kn = knowledgeM('madrazos', person)
# Send random madrazo
bot.send_message(chat_id=update.message.chat_id,
text=kn.format(user_name=real_name))
except (IndexError, ValueError):
# When you set nothing with /madrear
update.message.reply_text('Uso: /madrear <nombre del pirobo>')
# "El Socio" will give you a map to his house
def mihouse(bot, update):
try:
bot.send_message(chat_id=update.message.chat_id, text="Hogar de dioses")
# Caney, of course ;)
bot.send_location(chat_id=update.message.chat_id,
latitude=3.29057260799024,
longitude=-75.0142164941721)
except (IndexError, ValueError):
# In case something went wrong
update.message.reply_text('Uso: /micasa')
# "El socio" will give you a photo or gif about The finger expression
def pistola(bot, update):
try:
# Request a 'The Finger expression image or gif' from json
kn = knowledgeG('pistolas')
if (kn == 'gatito'):
# open the image with The Finger expression
image = open('knowledge/pistola/images/gatito.jpg','rb')
#send a image
bot.sendPhoto(chat_id=update.message.chat_id,
photo=image)
else:
#send a gif
bot.sendVideo(chat_id=update.message.chat_id,
video=kn)
except(IndexError, ValueError):
# In case something went wrong
update.message.reply_text('Uso: /pistola')
# "El Socio" will give you a suggestion for a plan
def planear(bot, update):
try:
# Request a 'plan' from json
kn = knowledgeG('planes')
# Send random 'plan'
bot.send_message(chat_id=update.message.chat_id,
text=kn)
except (IndexError, ValueError):
# When you get an error
update.message.reply_text('Uso: /planear')
# "El Socio" will give you a suggestion for a 'excusa'
def excusas(bot, update):
try:
# Request a 'excusa' from json
kn = knowledgeG('excusas')
# Send random 'excusa'
bot.send_message(chat_id=update.message.chat_id,
text=kn)
except (IndexError, ValueError):
# When you get an error /excusas
update.message.reply_text('Uso: /excusas')
# Tell "El Socio" what you want to pin
def pinear(bot, update):
try:
# Send pin message
bot.pin_chat_message(chat_id=update.message.chat_id,
message_id=update.message.message_id)
#print (update.message.text)
except (IndexError, ValueError):
# When you set nothing with /pinear
update.message.reply_text('Uso: /pinear <mensaje>')
# Let "El Socio" know who you want him to give an "agradecimiento"
def agradecer(bot, update, args):
try:
# let's keep the name for the message
real_name = args[0]
# Request an 'agradecimiento' for someone
kn = knowledgeG('agradecimientos')
# Send random 'agradecimiento'
bot.send_message(chat_id=update.message.chat_id,
text=kn.format(user_name=real_name))
except (IndexError, ValueError):
# When you set nothing with /agradecer
update.message.reply_text('Uso: /agradecer <nombre del pirobo>')
def ayuda(bot, update):
# A message when you don't know how to use it
msg = "Hola {user_name}! Yo soy {bot_name}. \n\n"
msg += "Esta joda es sencilla, los comandos son: \n\n"
msg += "- Madrear a alguien: /madrear <nombre-del-pirobo> \n"
msg += "- Saber la ubicacion de mi casa: /mihouse \n"
msg += "- Le propongo un plan: /planear \n"
msg += "- Agradecerle parcero: /agradecer <nombre-a-bendecir> \n"
msg += "- Le roto una excusa: /excusas \n"
msg += "- Le hago pistola: /pistola \n"
msg += "- Explicarle de nuevo porque no entiende: /ayuda \n\n"
msg += "Si le quedó claro papi? \n"
# Send the help message
bot.send_message(chat_id=update.message.chat_id,
text=msg.format(
user_name=update.message.from_user.first_name,
bot_name=bot.name))
# for error logging
def error(bot, update, error):
logger.warn('Update "%s" caused error "%s"' % (update, error))
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("madrear", madrear, pass_args=True))
dp.add_handler(CommandHandler("mihouse", mihouse))
dp.add_handler(CommandHandler("planear", planear))
dp.add_handler(CommandHandler("pistola", pistola))
dp.add_handler(CommandHandler("agradecer", agradecer, pass_args=True))
dp.add_handler(CommandHandler("excusas", excusas))
dp.add_handler(CommandHandler("pinear", pinear))
dp.add_handler(CommandHandler("ayuda", ayuda))
# Commands for admin porpuses
dp.add_handler(CommandHandler("restart", restart))
# log all errors
dp.add_error_handler(error)
# Start the Bot
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.
updater.idle()
if __name__ == '__main__':
main()