-
Notifications
You must be signed in to change notification settings - Fork 6
/
bot.py
324 lines (278 loc) · 10.8 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
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
# -*- coding: utf-8 -*-
#hola - Saluda
#start - Hello
#ayuda - Responde
#help - Response
#chiste - Cuenta un chiste
#animame - Envia una imagen de animo
#cheer_up - Send a cheer up picture
#comida - Envia una imagen de una buena comida
#food - Send an image of a tasty food
#animal - Envia una imagen de un animalillo
#animal - Send a picture of a cute animal
#especial - Envia una imagen especial
#special - Send a special picture
#sticker - Envia un sticker
#sticker - Send a sticker
#sorprendeme - Te sorprenderá
#surprise - It will surprise you
#elimina_stickers - Elimina los stickers guardados
#remove_stickers - Remove all the stickers
#es_mi_cumple - Felicidades!!
#its_my_birthday - Congratulations!!
#examen - Ohh, a ver si te animo
#exam - Ohh, let my cheer you up
import telebot # Librería de la API del bot.
#from telebot import types # Tipos para la API del bot.
import requests
import os
import random
import shutil
from random import randint
import pathlib
from classes.chistes import Chistes
from classes.imagenes import Imagenes
from classes.texto_a_array import Texto_a_array
from classes.myaes import MyAES
#https://github.com/eternnoir/pyTelegramBotAPI#writing-your-first-bot
#time.sleep(10)
bot = telebot.TeleBot(os.getenv('TELEGRAM_BOT_TOKEN')) # Creamos el objeto de nuestro bot.
chats = dict()
chistes = Chistes()
chistes.load_chistes()
absPath = pathlib.Path(__file__).parent.absolute()
print(absPath)
texts_folder = os.path.join(absPath, "texts/")
birthday_path = os.path.join(texts_folder, "birthday.txt")
if (os.path.isfile(birthday_path)):
birthday_array = Texto_a_array(birthday_path)
birthday_array.load_textArray()
examen_path = os.path.join(texts_folder, "examen.txt")
if (os.path.isfile(examen_path)):
examen_array = Texto_a_array(examen_path)
examen_array.load_textArray()
imagenes_folder = os.path.join(absPath, "images/")
imagenes_animo = Imagenes(["cheer up", "happiness", "super happy", "happy"], imagenes_folder+"animo.jpg")
imagenes_comida = Imagenes(["dessert", "cake", "hamburguer", "pizza", "ribs", "milkshakes", "cocktail"], imagenes_folder+"comida.jpg")
imagenes_animales = Imagenes(["cute animals", "cute cat", "cute dog", "baby bear", "cute rabbit", "cute koala", "cute penguin"], imagenes_folder+"animal.jpg")
imagenes_especiales = Imagenes(["minion", "sponge bob"], imagenes_folder+"special.jpg")
imagenes_sexy = Imagenes(["sexy", "bikini", "lingerie"], imagenes_folder+"sexy.jpg")
imagenes_gif = Imagenes(["funny gifs"], imagenes_folder+"temp.gif", True)
imagenes_animo.load_imagesUrls()
imagenes_animo.get_imagesUrl()
imagenes_comida.load_imagesUrls()
imagenes_comida.get_imagesUrl()
imagenes_animales.load_imagesUrls()
imagenes_animales.get_imagesUrl()
imagenes_especiales.load_imagesUrls()
imagenes_especiales.get_imagesUrl()
imagenes_sexy.load_imagesUrls()
imagenes_sexy.get_imagesUrl()
#imagenes_gif.load_imagesUrls()
print(absPath)
stickers_folder = os.path.join(absPath, "stickers/")
def create_folders():
if not os.path.exists(stickers_folder):
print(f"Creating {stickers_folder}")
os.makedirs(stickers_folder)
if not os.path.exists(imagenes_folder):
print(f"Creating {imagenes_folder}")
os.makedirs(imagenes_folder)
if not os.path.exists(texts_folder):
print(f"Creating {texts_folder}")
os.makedirs(texts_folder)
def always(message):
pass
"""print(message.chat.first_name)
#print(message.chat.id)
#chats[message.chat.first_name] = message.chat #Add new chat
#bot.forward_message(mId, message.chat.id, message.message_id) #FW
"""
def send_image(message, path):
always(message)
if not os.path.exists(path):
bot.send_message(message.chat.id, "Cargando imagen...\nPrueba en unos segundos")
else:
image = open(path, 'rb')
bot.send_photo(message.chat.id, image)
#bot.send_photo(message.chat.id, "FILEID")
def send_gif(message, url):
img_data = requests.get(url).content
with open('temp.gif', 'wb') as image:
image.write(img_data)
image = open('temp.gif', 'rb')
bot.send_document(message.chat.id, image)
bot.send_document(message.chat.id, "FILEID")
##COMMANDS
# Handles all text messages that contains the commands '/start' or '/hola'
@bot.message_handler(commands=['start', 'hola'])
def send_welcome(message):
always(message)
bot.reply_to(message, "Hola, que tál?")
@bot.message_handler(commands=['help','ayuda'])
def send_help(message):
always(message)
bot.reply_to(message, "Todos necesitamos ayuda alguna vez, cuéntame")
@bot.message_handler(commands=['chiste'])
def send_chiste(message):
always(message)
bot.send_message(message.chat.id, chistes.get_chiste())
@bot.message_handler(commands=['animame', 'cheer_up'])
def send_imageCheerUp(message):
path = imagenes_animo.get_path()
send_image(message,path)
@bot.message_handler(commands=['comida', 'food'])
def send_imageFood(message):
path = imagenes_comida.get_path()
send_image(message,path)
@bot.message_handler(commands=['animal'])
def send_imageAnimal(message):
path = imagenes_animales.get_path()
send_image(message,path)
@bot.message_handler(commands=['sexy'])
def send_imageSexy(message):
path = imagenes_sexy.get_path()
send_image(message,path)
@bot.message_handler(commands=['especial', 'special'])
def send_imageSpecial(message):
path = imagenes_especiales.get_path()
send_image(message,path)
@bot.message_handler(commands=['gif'])
def send_gifCommand(message):
#url = imagenes_gif.get_imagesUrl()
#send_gif(message,url)
pass
@bot.message_handler(commands=['bonita'])
def send_textBonita(message):
ma = MyAES()
bot.send_message(message.chat.id, ma.get_random_decrypt())
@bot.message_handler(commands=['sticker'])
def send_stickerCommand(message):
always(message)
for (dirpath, dirnames, filenames) in os.walk(stickers_folder):
if filenames:
sticker = stickers_folder + random.choice(filenames)
while not(".webp" in sticker):
sticker = stickers_folder + random.choice(filenames)
sti = open(sticker, 'rb')
bot.send_sticker(message.chat.id, sti)
bot.send_sticker(message.chat.id, "FILEID")
break
@bot.message_handler(commands=['remove_stickers','elimina_stickers'])
def remove_stickers(message):
always(message)
shutil.rmtree(stickers_folder)
os.makedirs(stickers_folder)
@bot.message_handler(commands=['sorprendeme', "surprise"])
def send_surprise(message):
commands = ['chiste', 'animame', 'comida', 'animal', 'especial', 'sticker']
index = randint(0,len(commands)-1)
if index == 0:
send_chiste(message)
elif index == 1:
send_imageCheerUp(message)
elif index == 2:
send_imageFood(message)
elif index == 3:
send_imageAnimal(message)
elif index == 4:
send_imageSpecial(message)
else:
send_stickerCommand(message)
@bot.message_handler(commands=['es_mi_cumple','its_my_birthday'])
def birthday(message):
always(message)
comment = birthday_array.get_comment()
bot.send_message(message.chat.id, comment)
@bot.message_handler(commands=['examen','exam'])
def exam(message):
always(message)
comment = examen_array.get_comment()
bot.send_message(message.chat.id, comment)
@bot.message_handler(commands=['enviar_mensaje_a','send_message_to'])
def send_message_to(message):
always(message)
if(message.text):
text_split = message.text.split(' ', 2)
if ((chats[text_split[1]] is not None) and text_split[2]):
bot.send_message(chats[text_split[1]], text_split[2])
@bot.message_handler(commands=['enviar_sorpresa','send_surprise']) #/enviar_sorpresa QUIEN QUÉ NO FUNCIONA
def programar(message):
always(message)
if(message.text):
text_split = message.text.split()
if ((chats[text_split[1]]is not None) and text_split[2]):
message.chat = chats[text_split[1]]
#message["from"] = chats[text_split[1][1]]
message.text = text_split[2]
echo_all(message)
##END COMMANDS
# Handles all sent sticker
# Guarda el sticker en una carpeta
@bot.message_handler(content_types=['sticker'])
def handle_sticker(message):
file_path = bot.get_file(message.sticker.file_id).file_path
url = 'https://api.telegram.org/file/bot{0}/{1}'.format(os.getenv('TELEGRAM_BOT_TOKEN'), file_path)
print("Descargando sticker: ")+file_path
sticker = requests.get(url)
with open(file_path, 'wb') as new_sticker: #Lo guarda en la carpeta stickers/ID.webp
new_sticker.write(sticker.content)
# Handles all sent documents and audio files
@bot.message_handler(content_types=['document', 'audio'])
def handle_docs_audio(message):
bot.reply_to(message, "Qué interesante!")
#Text
@bot.message_handler(func=lambda m: True)
def echo_all(message):
if "hola" in message.text:
send_welcome(message)
elif "ayuda" == message.text:
send_help(message)
elif "chiste" == message.text:
send_chiste(message)
elif "animame" == message.text:
send_imageCheerUp(message)
elif "comida" == message.text:
send_imageFood(message)
elif "animal" == message.text:
send_imageAnimal(message)
elif "especial" == message.text:
send_imageSpecial(message)
elif "gif" == message.text:
send_gifCommand(message)
elif "sticker" == message.text:
send_stickerCommand(message)
elif "elimina_stickers" == message.text:
remove_stickers(message)
elif "sorprendeme" == message.text:
send_surprise(message)
elif "examen" == message.text:
exam(message)
elif "es mi cumple" == message.text:
birthday(message)
else:
#bot.send_message(message.chat.id, message.text) Repite lo dicho
bot.send_message(message.chat.id, "cuentame más")
always(message)
#def listener(messages): # Con esto, estamos definiendo una función llamada 'listener', que recibe como parámetro un dato llamado 'messages'.
# for m in messages: # Por cada dato 'm' en el dato 'messages'
# if m.content_type == 'text': # Filtramos mensajes que sean tipo texto.
# cid = m.chat.id # Almacenaremos el ID de la conversación.
# bot.send_message("hola", cid)
# print "[" + str(cid) + "]: " + m.text # Y haremos que imprima algo parecido a esto -> [52033876]: /start
#bot.set_update_listener(listener) # Así, le decimos al bot que utilice como función escuchadora nuestra función 'listener' declarada arriba.
def start_listening():
try:
bot.polling(none_stop=True) # Con esto, le decimos al bot$
except Exception as e:
print("Error in bot polling: "+str(e))
try:
bot.polling(none_stop=True)
except Exception as e:
print("Second error: "+str(e))
def main():
create_folders()
print("Listening...")
start_listening()
if __name__ == "__main__":
main()