-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatiops_bot.py
More file actions
executable file
·354 lines (286 loc) · 14.5 KB
/
datiops_bot.py
File metadata and controls
executable file
·354 lines (286 loc) · 14.5 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
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
#!/usr/bin/env python3
import time
import telepot
from telepot.namedtuple import InlineKeyboardButton, InlineKeyboardMarkup
from telepot.loop import MessageLoop
import json
import syslog
import os, os.path
import sys
import argparse
import speech
import random
import hashlib
secretos = {}
esperaMensaje = False
to_superadmin = False
superadmin = "7404034"
aprobado = ""
moderacion = {}
def lee_secretos(configfile):
global secretos
with open(configfile, 'r') as f:
return json.load(f)
def guarda_secretos(configfile):
global secretos
with open(configfile, 'w') as f:
json.dump(secretos, f)
def escribeLog(texto):
syslog.syslog(texto)
print(texto)
# telegram.sendMessage(superadmin, texto, disable_notification=True)
def teclado_usuarios(callback_str):
botones = []
for item in secretos["authorized_ids"]:
botones.append(InlineKeyboardButton(text=telegram.getChat(item)["first_name"], callback_data=callback_str + item))
columnas = [botones[i:i+2] for i in range(0,len(botones),2)]
return InlineKeyboardMarkup(inline_keyboard=columnas)
def mensaje_para_admins(mensaje, keyboard=[]):
for admin in secretos["admin"]:
telegram.sendMessage(admin, mensaje, reply_markup=keyboard)
def estado_moderacion():
if secretos["moderation_mode"]:
return "La moderacion de mensajes esta ACTIVADA"
else:
return "La moderacion de mensajes esta DESACTIVADA"
def get_hash_texto(texto):
return hashlib.md5(texto.encode("utf-8")).hexdigest()
def get_text_from_hash(hash_texto):
for item in moderacion.keys():
for i in moderacion[item]:
if i[0] == hash_texto:
return i[1]
def hash_already_exists(chat_id, hash_texto):
# print(moderacion)
try:
if (hash_texto, get_text_from_hash(hash_texto)) in moderacion[chat_id]:
return True
else:
return False
except KeyError:
return False
def delete_text(chat_id, hash_texto):
moderacion[chat_id].remove((hash_texto, get_text_from_hash(hash_texto)))
def on_chat_message(msg):
global esperaMensaje
global to_superadmin
chat_id = str(msg['chat']['id'])
comando = msg['text']
nombre_usuario = msg['from']['first_name']
keyboard = InlineKeyboardMarkup(inline_keyboard=[])
# print("CHAT_ID: " + chat_id)
# print("NOMBRE_USUARIO: "+ nombre_usuario)
# print("COMANDO: " + comando)
if chat_id not in secretos["authorized_ids"]:
escribeLog("El usuario %s (%s) no esta autorizado" %(nombre_usuario, chat_id))
mensaje = "El usuario %s (%s) quiere usar @datiops_bot, para autorizarle pulsa el boton" %(nombre_usuario, chat_id)
keyboard = InlineKeyboardMarkup(inline_keyboard=[[InlineKeyboardButton(text='Autorizar', callback_data='authorize.' + chat_id)],])
mensaje_para_admins(mensaje, keyboard)
return
else:
if comando == "/help":
mensaje = """
Estos son los comandos disponibles:
- /text: el texto que quieras reproducir en la raspberry (después de ejecutar el comando pregunta el texto a reproducir)
- /random: para reproducir una frase aleatoria de los temas propuestos
- /mp3: para reproducir uno de los mp3 propuestos
- /admin: solo para administradores
"""
elif comando == "/start":
escribeLog("El usuario %s (%s) ha iniciado chat con datiops_bot" %(nombre_usuario, chat_id))
mensaje = 'Buenas %s!\nSoy el bot de Operaciones de DATIO. Ejecuta /help para saber los comandos que tienes disponibles. A disfrutarlos' %nombre_usuario
# elif comando.startswith("/text "):
# reproduce.play_message(comando.split("/text ")[1])
# mensaje = "Mensaje reproducido"
elif comando == "/text":
esperaMensaje = True
mensaje = "Por favor, dime qué quieres reproducir en la raspberry:"
elif comando == "/random":
temas = reproduce.get_topics()
temas.sort()
botones = []
for item in temas:
botones.append(InlineKeyboardButton(text=item, callback_data=item))
columnas = [botones[i:i+2] for i in range(0,len(botones),2)]
keyboard = InlineKeyboardMarkup(inline_keyboard=columnas)
mensaje = "Por favor, elige uno de los siguientes temas:"
elif comando == "/mp3":
mp3 = reproduce.get_mp3()
mp3.sort()
botones = []
for item in mp3:
botones.append([InlineKeyboardButton(text=item, callback_data=item)])
keyboard = InlineKeyboardMarkup(inline_keyboard=botones)
mensaje = "Por favor, elige uno de los siguientes mp3:"
elif comando == "/admin":
if chat_id not in secretos["admin"]:
mensaje = "Lo siento pero no eres un usuario administrador. Pase por caja para aumentar tus privilegios https://www.paypal.me/ohermosa"
escribeLog ("El usuario %s (%s) ha intentado ejecutar el comando '/admin'" %(nombre_usuario, chat_id))
else:
mensaje = "Elige qué quieres hacer:"
botones = [[InlineKeyboardButton(text="Ver autorizados", callback_data="authorized"), InlineKeyboardButton(text="Bannear", callback_data="ban")], [InlineKeyboardButton(text="Ver administradores", callback_data="ver_admin"), InlineKeyboardButton(text="Nuevo admin", callback_data="new_admin")], [InlineKeyboardButton(text="Moderation STATUS", callback_data="moderation_status"), InlineKeyboardButton(text="Moderation MODE", callback_data="moderation_mode")],[InlineKeyboardButton(text="Enviar mensaje", callback_data="send_message")]]
keyboard = InlineKeyboardMarkup(inline_keyboard=botones)
elif esperaMensaje:
esperaMensaje = False
texto = comando
hash_texto = get_hash_texto(texto)
if secretos["moderation_mode"]:
if chat_id not in moderacion.keys():
moderacion[chat_id] = [(hash_texto, texto)]
elif chat_id in moderacion.keys() and not hash_already_exists(chat_id, hash_texto):
moderacion[chat_id].append((hash_texto, texto))
elif chat_id in moderacion.keys() and hash_already_exists(chat_id, hash_texto):
print ("El usuario %s (%s) insiste en reproducir la misma frase" %(nombre_usuario, chat_id))
mensaje = "%s quiere enviar el siguiente mensaje: '%s'. Permitir?" %(nombre_usuario, texto)
botones = [[InlineKeyboardButton(text="SÍ", callback_data="authmsgyes_" + chat_id + "_" + hash_texto), InlineKeyboardButton(text="NO", callback_data="authmsgno_" + chat_id + "_" + hash_texto)]]
keyboard = InlineKeyboardMarkup(inline_keyboard=botones)
to_superadmin = True
else:
reproduce.play_message(texto)
escribeLog("El usuario %s (%s) ha enviado el mensaje '%s'" %(nombre_usuario, chat_id, texto))
mensaje = "Mensaje reproducido"
elif comando.startswith("MSG@"):
destinatario = comando.split("@")[1]
mensaje = comando.split("@")[2]
chat_id = destinatario
escribeLog("El usuario %s (%s) ha enviado como @datiops_bot el mensaje '%s' a %s" %(nombre_usuario, chat_id, mensaje, telegram.getChat(destinatario)["first_name"]))
else:
mensaje = "Ay %s, eres un lechón. Aprende a usar este bot ejecutando el comando /help anda" %nombre_usuario
if to_superadmin:
telegram.sendMessage(superadmin, mensaje, reply_markup=keyboard)
to_superadmin = False
else:
telegram.sendMessage(chat_id, mensaje, reply_markup=keyboard)
def on_callback_query(msg):
nombre_usuario = msg['from']['first_name']
chat_id = str(msg['from']['id'])
query_id, from_id, query_data = telepot.glance(msg, flavor='callback_query')
if query_data in reproduce.get_topics():
reproduce.play_random(query_data)
telegram.answerCallbackQuery(query_id, text='Mensaje reproducido')
escribeLog("El usuario %s (%s) ha reproducido un mensaje aleatorio de %s" %(nombre_usuario, chat_id, query_data))
elif query_data in reproduce.get_mp3():
reproduce.play_mp3(query_data)
telegram.answerCallbackQuery(query_id, text='MP3 reproducido')
escribeLog("El usuario %s (%s) ha reproducido el mp3 %s" %(nombre_usuario, chat_id, query_data))
elif "authorize." in query_data:
nuevo_usuario = query_data.split("authorize.")[1]
if nuevo_usuario in secretos["authorized_ids"]:
mensaje = "El usuario %s (%s) ya estaba autorizado" %(telegram.getChat(nuevo_usuario)["first_name"], nuevo_usuario)
else:
secretos["authorized_ids"].append(nuevo_usuario)
guarda_secretos(args["configfile"])
telegram.sendMessage(nuevo_usuario, "Has sido autorizado, ENHORABUENA!")
escribeLog("Se ha autorizado al usuario %s a usar el bot" %nuevo_usuario)
mensaje = "El usuario %s (%s) ha sido autorizado" %(telegram.getChat(nuevo_usuario)["first_name"], nuevo_usuario)
telegram.answerCallbackQuery(query_id, text=mensaje)
mensaje_para_admins(mensaje)
elif query_data == "authorized":
mensaje = "Ahora mismo hay estos usuarios autorizados:\n"
for usuario in secretos["authorized_ids"]:
mensaje = mensaje + telegram.getChat(usuario)["first_name"] + "\n"
telegram.answerCallbackQuery(query_id, text='Se ha mostrado la lista de usuarios autorizados')
telegram.sendMessage(chat_id, mensaje)
elif query_data == "ban":
keyboard = teclado_usuarios("ban.")
mensaje = "Elige que usuario quieres bannear:"
telegram.sendMessage(chat_id, mensaje, reply_markup=keyboard)
elif "ban." in query_data:
to_ban = query_data.split("ban.")[1]
if to_ban == superadmin or to_ban in secretos["authorized_ids"]:
mensaje = "No se puede banear a un administrador"
else:
if to_ban not in secretos["authorized_ids"]:
mensaje = "El usuario %s (%s) ya no estaba en la lista de usuarios autorizados" %(telegram.getChat(to_ban), to_ban)
else:
secretos["authorized_ids"].remove(to_ban)
if to_ban in secretos["admin"]:
secretos["admin"].remove(to_ban)
guarda_secretos(args["configfile"])
telegram.sendMessage(to_ban, "Has sido baneado del bot. Para cualquier reclamacion, llama al 091")
escribeLog ("El usuario %s (%s) ha baneado al usuario %s (%s)" %(nombre_usuario, chat_id, telegram.getChat(to_ban)["first_name"], to_ban))
telegram.answerCallbackQuery(query_id, mensaje)
mensaje_para_admins(mensaje)
elif query_data == "new_admin":
mensaje = "Elije cual de estos usuarios quieres convertirlo en administrador:"
keyboard = teclado_usuarios("newadmin.")
telegram.sendMessage (chat_id, mensaje, reply_markup=keyboard)
elif query_data == "ver_admin":
mensaje = "Ahora mismo hay estos usuarios administradores:\n"
for usuario in secretos["admin"]:
mensaje = mensaje + telegram.getChat(usuario)["first_name"] + "\n"
telegram.answerCallbackQuery(query_id, text='Se ha mostrado la lista de usuarios administradores')
telegram.sendMessage(chat_id, mensaje)
elif "newadmin." in query_data:
to_admin = query_data.split("newadmin.")[1]
if to_admin in secretos["admin"]:
mensaje = "El usuario %s (%s) ya es administrador" %(telegram.getChat(to_admin)["first_name"], to_admin)
else:
secretos["admin"].append(to_admin)
guarda_secretos(args["configfile"])
mensaje = "Se ha convertido en administrador al usuario %s (%s)" %(telegram.getChat(to_admin)["first_name"], to_admin)
escribeLog ("El usuario %s (%s) ha convertido en admin al usuario %s (%s)" %(nombre_usuario, chat_id, telegram.getChat(to_admin)["first_name"], to_admin))
telegram.sendMessage(to_admin,"Ya eres administrador del bot, recuerda: __'un gran poder conlleva una gran responsabilidad__'")
telegram.answerCallbackQuery(query_id, text=mensaje)
mensaje_para_admins(mensaje)
elif query_data == "moderation_status":
mensaje = estado_moderacion()
telegram.answerCallbackQuery(query_id, text=mensaje)
elif query_data == "moderation_mode":
secretos["moderation_mode"] = not secretos["moderation_mode"]
guarda_secretos(args["configfile"])
mensaje = estado_moderacion()
telegram.answerCallbackQuery(query_id, text=mensaje)
if chat_id != superadmin:
telegram.sendMessage(superadmin, mensaje)
escribeLog ("El usuario %s (%s) ha cambiado el modo moderación" %(nombre_usuario, chat_id))
elif "authmsgyes_" in query_data:
telegram.answerCallbackQuery(query_id, "Mensaje aceptado")
usuario = query_data.split("_")[1]
hash_texto = query_data.split("_")[2]
texto = get_text_from_hash(hash_texto)
if hash_already_exists(usuario, hash_texto):
reproduce.play_message(texto)
telegram.sendMessage(usuario, "Mensaje reproducido")
delete_text(usuario, hash_texto)
escribeLog("El usuario %s (%s) ha enviado el mensaje '%s' y ha sido aprobado por el superadmin" %(telegram.getChat(usuario)["first_name"], usuario, texto))
else:
telegram.sendMessage(superadmin, "Este mensaje ya ha sido procesado")
elif "authmsgno_" in query_data:
telegram.answerCallbackQuery(query_id, "Mensaje denegado")
usuario = query_data.split("_")[1]
hash_texto = query_data.split("_")[2]
if hash_already_exists(usuario, hash_texto):
telegram.sendMessage(usuario, "Dios no ha aprobado tu mensaje, anda y que te den por culo")
delete_text(usuario, hash_texto)
escribeLog("El usuario %s (%s) ha enviado el mensaje '%s' pero no ha sido aprobado por el superadmin" %(telegram.getChat(usuario)["first_name"], usuario, get_text_from_hash(hash_texto)))
else:
telegram.sendMessage(superadmin, "Este mensaje ya ha sido procesado")
elif query_data == "send_message":
keyboard = teclado_usuarios("message.")
mensaje = "Elige que usuario al que quieres enviar un mensaje:"
telegram.sendMessage(chat_id, mensaje, reply_markup=keyboard)
elif "message." in query_data:
to_message = query_data.split("message.")[1]
mensaje = "Envia 'MSG@" + to_message + "@tu mensaje' para enviarle un mensaje a " + telegram.getChat(to_message)["first_name"]
telegram.sendMessage(chat_id, mensaje)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--configfile", required=True, help="Define el fichero json de configuracion del script")
args = parser.parse_args()
args = vars(args)
if os.path.islink(os.path.abspath(sys.argv[0])):
mydir = os.path.dirname(os.readlink(sys.argv[0]))
else:
mydir = os.path.dirname(os.path.abspath(sys.argv[0]))
reproduce = speech.TeHablo(mydir)
secretos = lee_secretos(args["configfile"])
if superadmin not in secretos["admin"]:
secretos["admin"].append(superadmin)
guarda_secretos(args["configfile"])
# print (secretos)
telegram = telepot.Bot(secretos["token"])
MessageLoop(telegram, {'chat': on_chat_message, 'callback_query': on_callback_query}).run_as_thread()
while 1:
secretos = lee_secretos(args["configfile"])
time.sleep(10)