forked from boterostg/botBasicoGlitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
643 lines (367 loc) · 19.4 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
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
import telebot
from telebot import *
from os import environ
from random import randrange
import json
import requests
from configBan import *
bot = telebot.TeleBot(environ['TELEGRAM_TOKEN']) #Token del bot
# Variable que se utilizará en el comando /start para enviarlo como el texto del mensaje del bot
bot_text = '''
Howdy, how are you doing?
Source code on https://glitch.com/~{}
'''.format(environ['PROJECT_NAME'])
menuKeyboard = types.InlineKeyboardMarkup()
menuKeyboard.add(types.InlineKeyboardButton('Ayuda', callback_data='ayuda'),
types.InlineKeyboardButton('Creador', callback_data='creador'))
yoNuncaKeyboard = types.InlineKeyboardMarkup()
yoNuncaKeyboard.add(types.InlineKeyboardButton('Me apunto', callback_data='yo'))
propuestaInlineKeyboard = types.InlineKeyboardMarkup()
propuestaInlineKeyboard.add(types.InlineKeyboardButton('Me apunto', callback_data='inlineApuntado'))
@bot.message_handler(commands=['menu'])
def menu(m):
cid = m.chat.id
bot.send_message(cid, "Menu", reply_markup=menuKeyboard, parse_mode="Markdown")
@bot.callback_query_handler(func=lambda call: call.data in ['ayuda', 'creador'])
def callback_handlerMenu(call):
cid = call.message.chat.id
mid = call.message.message_id
info = call.data
#bot.send_message(cid, "Has pulsado " + str(info))
if info == "ayuda":
#bot.send_message(cid, "Entramos en " + str(info))
bot.edit_message_text("Has pulsado ayuda", cid, mid, reply_markup=menuKeyboard, parse_mode="Markdown")
elif info == "creador":
#bot.send_message(cid, "Entramos en " + str(info))
bot.edit_message_text("Mi creador es : @batichico" , cid, mid, reply_markup=menuKeyboard , parse_mode="Markdown")
@bot.inline_handler(lambda query: query.query.startswith('propuesta') and len(query.query.split()) > 1)
def function_propuesta(q):
cid = q.from_user.id
txt = q.query.split(None, 1)[1]
keyboard = types.InlineKeyboardMarkup()
keyboard.add(types.InlineKeyboardButton("Me apunto", callback_data="propuesta {}".format(txt)))
article = types.InlineQueryResultArticle(1, "Enviar propuesta", types.InputTextMessageContent(txt), reply_markup=keyboard)
bot.answer_inline_query(q.id, [article], cache_time=1)
@bot.callback_query_handler(func=lambda call: call.data.startswith('propuesta'))
def function_button(call):
mensaje = call.data.split(None, 1)[1]
idUsuario = call.from_user.id
nombreUsuario = call.from_user.username
nombreUsuario = "@"+nombreUsuario
todoCall = call
print(str(todoCall))
mid = call.inline_message_id
contenido = mensaje + "\n\n"
apuntados = mensaje.split("\n\n")[1:]
#bot.answer_callback_query(call.id , str(mensaje) , show_alert=True)
if nombreUsuario not in mensaje:
mensaje = contenido + nombreUsuario
bot.answer_callback_query(call.id , "Vamos a apuntarte, apuntados: " + str(apuntados) , show_alert=True)
bot.edit_message_text(mensaje, inline_message_id=call.inline_message_id , reply_markup=propuestaInlineKeyboard, parse_mode="Markdown")
apuntados = mensaje.split("\n\n")[1:]
else :
apuntadosS = ""
apuntados = mensaje.split("\n\n")[1:]
contenido = mensaje.split("\n\n")[0]
contenido = contenido + "\n\n"
apuntados.remove(str(nombreUsuario))
bot.answer_callback_query(call.id , str(apuntados) , show_alert=True)
'''
mid = call.message.message_id
mensaje = call.message.text
contenido = mensaje + "\n\n"
apuntados = mensaje.split("\n\n")[1:]
else :
apuntadosS = ""
apuntados = mensaje.split("\n\n")[1:]
contenido = mensaje.split("\n\n")[0]
contenido = contenido + "\n\n"
apuntados.remove(str(nombreUsuario))
if len(apuntados) == 0 :
bot.edit_message_text(contenido, cid, mid, reply_markup=yoNuncaKeyboard, parse_mode="Markdown")
elif len(apuntados) == 1 :
mensaje = contenido + apuntados[0]
bot.edit_message_text(mensaje, cid, mid, reply_markup=yoNuncaKeyboard, parse_mode="Markdown")
elif len(apuntados) >= 1 :
for apuntado in apuntados:
apuntadosS = apuntadosS + apuntado + "\n\n"
mensaje = contenido + apuntadosS
bot.edit_message_text(mensaje, cid, mid, reply_markup=yoNuncaKeyboard, parse_mode="Markdown")
else :
apuntadosS = ""
apuntados = mensaje.split("\n\n")[1:]
contenido = mensaje.split("\n\n")[0]
contenido = contenido + "\n\n"
apuntados.remove(str(nombreUsuario))
if len(apuntados) == 0 :
bot.edit_message_text(contenido, cid, mid, reply_markup=yoNuncaKeyboard, parse_mode="Markdown")
elif len(apuntados) == 1 :
mensaje = contenido + apuntados[0]
bot.edit_message_text(mensaje, cid, mid, reply_markup=yoNuncaKeyboard, parse_mode="Markdown")
elif len(apuntados) >= 1 :
for apuntado in apuntados:
apuntadosS = apuntadosS + apuntado + "\n\n"
mensaje = contenido + apuntadosS
bot.edit_message_text(mensaje, cid, mid, reply_markup=yoNuncaKeyboard, parse_mode="Markdown")
'''
@bot.callback_query_handler(func=lambda call: call.data in ['yosi'])
def callback_handlerYoNunca(call):
cid = call.message.chat.id
idUsuario = call.from_user.id
nombreUsuario = call.from_user.first_name
mid = call.message.message_id
mensaje = call.message.text
arrayApuntados = arrayUsuarios
nombreUsuarioClick = call.message.from_user.first_name
if idUsuario not in arrayApuntados or len(arrayApuntados) == 0 :
añadirUser = {mid:{idUsuario:nombreUsuario}}
arrayApuntados.append(añadirUser)
nombreNuevoUser = arrayApuntados[-1][mid][idUsuario]
mensaje = mensaje + "\n\n" + nombreNuevoUser
bot.edit_message_text(mensaje, cid, mid, reply_markup=yoNuncaKeyboard, parse_mode="Markdown")
bot.send_message(cid, str(arrayApuntados))
elif idUsuario in arrayApuntados :
arrayApuntados.pop(idUsuario)
@bot.callback_query_handler(func=lambda call: call.data in ['inlineApuntado'])
def callback_handlerPropuesta(call):
print("Entramos en handler")
print("CALL: " + str(call))
print(str(call.id))
#cid = call.message.chat.id
idUsuario = call.from_user.id
nombreUsuario = call.from_user.username
nombreUsuario = "@"+nombreUsuario
print(str(nombreUsuario))
mid = call.inline_message_id
print(str(mid))
#mensaje = call.message.text
#mensaje = call.data.split(None, 1)[1]
mensaje = call.data
contenido = mensaje + "Contenido\n\n"
#contenido = mensaje + "\n\n"
apuntados = mensaje.split("\n\n")[1:]
print("Apuntados: " + str(apuntados))
#bot.answer_callback_query(mid , str(apuntados) , show_alert=True)
if nombreUsuario not in mensaje:
print ("entramos en if" + nombreUsuario + "\n" + mensaje)
mensaje = contenido + nombreUsuario
apuntados = mensaje.split("\n\n")[1:]
print("Mensaje metido: " + mensaje)
print("Apuntados: " + str(apuntados))
#bot.answer_callback_query(call.id , "Vamos a apuntarte, apuntados: " + str(apuntados) , show_alert=True)
bot.edit_message_text(mensaje, inline_message_id=call.inline_message_id , reply_markup=propuestaInlineKeyboard, parse_mode="Markdown")
print("Mensaje despues metido: " + mensaje)
#bot.edit_message_text(mensaje, inline_message_id=mid, reply_markup=propuestaInlineKeyboard, parse_mode="Markdown")
#bot.edit_message_text(mensaje, cid, mid, reply_markup=propuestaInlineKeyboard, parse_mode="Markdown")
else :
#print ("entramos en else")
apuntadosS = ""
apuntados = mensaje.split("\n\n")[1:]
#print (apuntados)
contenido = mensaje.split("\n\n")[0]
contenido = contenido + "\n\n"
apuntados.remove(str(nombreUsuario))
if len(apuntados) == 0 :
bot.edit_message_text(contenido, cid, mid, reply_markup=propuestaInlineKeyboard, parse_mode="Markdown")
elif len(apuntados) == 1 :
mensaje = contenido + apuntados[0]
bot.edit_message_text(mensaje, cid, mid, reply_markup=propuestaInlineKeyboard, parse_mode="Markdown")
elif len(apuntados) >= 1 :
for apuntado in apuntados:
apuntadosS = apuntadosS + apuntado + "\n\n"
mensaje = contenido + apuntadosS
bot.edit_message_text(mensaje, cid, mid, reply_markup=propuestaInlineKeyboard, parse_mode="Markdown")
@bot.callback_query_handler(func=lambda call: call.data in ['yo'])
def callback_handlerYoNunca(call):
cid = call.message.chat.id
idUsuario = call.from_user.id
if (call.from_user.username is None):
bot.answer_callback_query(call.id, "Tienes que tener nickname, @ lo que sea", show_alert=True)
else:
nombreUsuario = call.from_user.username
nombreUsuario = "@"+nombreUsuario
mid = call.message.message_id
mensaje = call.message.text
contenido = mensaje + "\n\n"
apuntados = mensaje.split("\n\n")[1:]
if nombreUsuario == "@elraro":
#print(str(call.id) + " " + nombreUsuario)
bot.answer_callback_query(call.id, "HDP", show_alert=True)
else:
if nombreUsuario not in mensaje:
mensaje = contenido + nombreUsuario
bot.edit_message_text(mensaje, cid, mid, reply_markup=yoNuncaKeyboard, parse_mode="Markdown")
apuntados = mensaje.split("\n\n")[1:]
else :
apuntadosS = ""
apuntados = mensaje.split("\n\n")[1:]
contenido = mensaje.split("\n\n")[0]
contenido = contenido + "\n\n"
apuntados.remove(str(nombreUsuario))
if len(apuntados) == 0 :
bot.edit_message_text(contenido, cid, mid, reply_markup=yoNuncaKeyboard, parse_mode="Markdown")
elif len(apuntados) == 1 :
mensaje = contenido + apuntados[0]
bot.edit_message_text(mensaje, cid, mid, reply_markup=yoNuncaKeyboard, parse_mode="Markdown")
elif len(apuntados) >= 1 :
for apuntado in apuntados:
apuntadosS = apuntadosS + apuntado + "\n\n"
mensaje = contenido + apuntadosS
bot.edit_message_text(mensaje, cid, mid, reply_markup=yoNuncaKeyboard, parse_mode="Markdown")
# Inicio charla 1- 19/07/2018
@bot.message_handler(commands=['start', 'help']) # Comando /start o /help . Cuando un usuario escriba cualquiera de los comandos
def send_welcome(message):
bot.reply_to(message, bot_text) # El bot responde con el contenido de la variable bot_text al mensaje.
@bot.message_handler(commands=['ayuda']) # Ejemplo de comando /ayuda que en vez de pasarle una variable, directamente escribimos
def ayuda(message):
bot.reply_to(message, 'ahora mismo te ayudo') # El bot responde 'ahora mismo te ayudo' al mensaje
#Inicio charla 2 - 31/07/2018
@bot.message_handler(commands=['ruleta'])
def ruleta(message):
cid = message.chat.id
nombreUsuario = message.from_user.username
idUsuario = message.from_user.id
rnd = randrange(0, int(5))
if rnd == 4:
bot.send_message(cid, "Pummmm tas muelto @" + nombreUsuario )
bot.kick_chat_member(cid,idUsuario)
bot.unban_chat_member(cid, idUsuario)
else:
bot.send_message(cid, "Te has salvado amijo @" + nombreUsuario)
#Fin charla 2 - 31/07/2018
@bot.message_handler(func=lambda message: True) # Respuestas del bot
def echo_message(message):
cid = message.chat.id # Guardamos en la variale cid el id de el mensaje recibido
idUsu = message.from_user.id
if message.text.lower() == "bot vete" :
if idUsu== 239822769:
bot.send_message(cid, 'vale :C, adios mi gente!')
bot.leave_chat(cid)
if "ban" in message.text.lower().split() :
bot.send_message(cid, 'estas ban')
if message.text.lower() == "holi": # Comparamos el texto el mensaje si es igual a 'holi'
id = message.from_user.id # Guardamos en una variable el id del usuario que envió el mensaje
nombre = message.from_user.first_name # Guardamos en una variable el nombre del usuario que envió el mensaje
bot.send_message(cid, 'holi ' + nombre ) # El bot responde 'holi' y después el nombre del usuario que guardamos en la variable da arriba
if id == 239822769: # Comparamos el id guardado del usuario con un id que le hemos pasado
bot.send_message( cid, 'Hola mi creador 😙') # Si es igual responde el mensaje que hemos introducido al chat indicado
elif id == 270803389 : # En este caso el id lo comparamos con otra id diferente
bot.send_message( cid, 'Holi manuel') # Aquí envia al chat que le hemos hablado 'Holi manuel'
bot.send_message( 115659666, 'Holi manuel') # Aquí probamos a pasarle en vez de cid, un id de un usuario y le enviará el mensaje por mensaje
#privado
else: # Si no se da ninguno de los resultados de arriba hará lo siguiente:
bot.send_message( cid, 'No estás registrado') # Enviará por el chat el mensaje 'No estás registrado'
elif message.text.lower() == "mensaje": # Aquí probamos un if comparandolo con un String(una cadena de carácteres)
bot.send_message( cid, 'mensaje') # Si es así contestará 'mensaje'
#Fin charla 1 - 19/07/218
if message.text.lower().startswith('/'):
idMensaje = message.message_id
idUsuario = call.from_user.id
bot.delete_message(cid,idMensaje)
dt = datetime.datetime(2018, 8, 21, 16, 00)
unixTime = time.mktime(dt.timetuple())
bot.restrict_chat_member(self, cid, idUsuario, until_date=None, can_send_messages=False,
can_send_media_messages=False, can_send_other_messages=False,
can_add_web_page_previews=False)
#Inicio charla 3 - 08/08/2018
if message.text.lower().startswith('participo con el numero'):
cid = message.chat.id
mensaje=message.text
rnd = randrange(0, int(9))
respuesta = ' '.join(mensaje.split(" ")[4:])
#bot.send_message(cid, "la respuesta es: " + respuesta)
#bot.send_message(cid, str(rnd))
if int(respuesta) == rnd:
bot.send_message(cid, "Has acertado")
else:
bot.send_message(cid, "Has fallado, el número era " + str(rnd))
#Fin charla 3 - 08/08/2018
if message.text.lower().startswith("propuesta") and len(message.text)>1:
cid = message.chat.id
nombre = message.from_user.first_name
mensaje = message.text
respuesta = ' '.join(mensaje.split(" ")[1:])
bot.send_message(cid, respuesta, reply_markup=yoNuncaKeyboard, parse_mode="Markdown")
#Inicio charla 2 - 31/07/2018
if "boti di" in message.text.lower(): # Comparamos el texto el mensaje si es igual a "boti di"
cid = message.chat.id #Id del chat
mensaje=message.text #Mensaje completo
respuesta = ' '.join(mensaje.split(" ")[2:]) #Respuesta
bot.send_message(cid, respuesta) #El bot envía la respuesta
#Respuesta a la entrada de un usuario al grupo
@bot.message_handler(func=lambda message: True, content_types=['new_chat_members'])
def command_bienvenida(m):
cid = m.chat.id #Id del chat
cname = m.chat.title #Nombre del grupo
bienvenida = "" #Variable dónde guardaremos el mensaje de bienvenida
infoMesaje = str(m)
nuevoUsuarioId = m.new_chat_member.id
nuevoUsuarioName = m.new_chat_member.username
añadidoPorId = m.from_user.id
añadidoPor = m.from_user.username
#bot.send_message(cid, infoMesaje)
if nuevoUsuarioName == "Botinutilbot":
if cid != -1001318959349:
bot.send_message(cid, añadidoPor + " , para que hostias me metes, este no es mi grupo joderrr!")
bot.leave_chat(cid)
if añadidoPorId == nuevoUsuarioId:
bot.send_message(cid, 'Entrado por enlace de invitación')
#bot.send_message(cid, infoMesaje)
else:
bot.send_message(cid, 'añadido por: ' + añadidoPor)
bot.send_message(cid, str(cid))
if (m.new_chat_member.username is None): #Si el usuario no tiene nickname
nun = m.new_chat_member.first_name #Guardamos el primer nombre del usuario
if (m.new_chat_member.last_name is not None): #Si el usuario tiene apellido
nun += " "
nun += m.new_chat_member.last_name #Se guarda el apellido
else: #Si no tiene apellido
bienvenida = "Bienvenido al grupo" #Se guarda "Bienvenido al grupo"
bienvenida += str(cname) #Se le añade el nombre del chat detras de "Bienvenido al grupo"
bienvenida += " "
else: #Si el usuario tiene nickname
nun = m.new_chat_member.username #Se guarda en nun el nickname del usuario
bienvenida = "Bienvenido al grupo " #Se guarda "Bienvenido al grupo"
bienvenida += str(cname) #Se le añade el nombre del chat detras de "Bienvenido al grupo"
bienvenida += " @"
bot.send_message(cid, str(bienvenida) + str(nun)) #Se envía el mensaje de bienvenida concatenando el nombre/nickname del usuario
#Respuesta a la salida de un usuario del grupo
@bot.message_handler(func=lambda message: True, content_types=['left_chat_member'])
def command_bye(m):
cid = m.chat.id #Id del grupo
despedida = "" #Variable dónde guardaremos el mensaje de despedida
if (m.left_chat_member.username is None): #Si el usuario no tiene nickname
nun = m.left_chat_member.first_name #Guardamos el primer nombre del usuario
if (m.left_chat_member.last_name is not None): #Si el usuario tiene apellido
nun += " "
nun += m.left_chat_member.last_name #Se guarda el apellido
else: #Si el usuario tiene nickname
nun = m.left_chat_member.username #Se guarda en nun el nickname del usuario
despedida = "Hasta luego " #Se guarda "Hasta luego"
despedida += " @"
bot.send_message(cid, str(despedida) + str(nun)) #Se envía el mensaje de despedida concatenando el nombre/nickname del usuario
#Fin charla 2 - 31/07/2018
'''
APUNTESSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSs
import random
from itertools import izip_longest
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# Taken from itertools recipes:
# https://docs.python.org/2/library/itertools.html#recipes
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)
apuntados = ["peter","alice","andy","patxi","richard","ana"]
parejas = ""
print(str(apuntados))
random.shuffle(apuntados)
print(len(apuntados))
if len(apuntados) >1:
for primero, segundo in grouper(apuntados, 2):
parejas = parejas + str(primero) + " y " + str(segundo) +"\n"
else:
bot.send_message(cid,"solo hay uno")
print(parejas)
'''
bot.set_webhook("https://{}.glitch.me/{}".format(environ['PROJECT_NAME'], environ['TELEGRAM_TOKEN']))