forked from Diego0075/pugby_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
279 lines (257 loc) · 10.8 KB
/
main.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import telegram
from telegram.ext import *
from functools import partial
import json
from random import choice
from utils.utils import texter, get_time, args_time
import sys, os
with open("conf/settings.json") as settings:
SETTINGS = json.load(settings)
# Import the duos and squads manager
# corresponding to the configured database engine
if SETTINGS["USE_DATABASE"]:
if SETTINGS["DATABASE_ENGINE"] == "postgres":
pass
elif SETTINGS["DATABASE_ENGINE"] == "sqlite3":
pass
else:
from utils.squaddic import SquadDic
SquadManager = SquadDic()
jobs = {}
# Miscellaneous functions-----------------------------------------------
def send_create_group(bot,cid,message,user,query=False,group=False):
s_list = "\n"
if not group:
group = SquadManager.get_group(user)
users = SquadManager.get_group_users(group)
data = " %s %s" % (group,user)
sym1 = texter("symbol01",SETTINGS["LENGUAGE"])
sym2 = texter("symbol02",SETTINGS["LENGUAGE"])
sym3 = texter("symbol03",SETTINGS["LENGUAGE"])
sym4 = texter("symbol04",SETTINGS["LENGUAGE"])
count = 0
for user in users:
if user[0] is not None:
count += 1
if user[2]:
s = sym2+" "+user[0]+"\n"
else:
s = sym3+" "+user[0]+"\n"
else:
s = sym1+"\n"
s_list += s
message += "\n"+sym4 % (count,len(users))
message += s_list
a = [
[telegram.InlineKeyboardButton(text=texter("btn_group_01",
SETTINGS["LENGUAGE"]),callback_data="JG"+data)],
[telegram.InlineKeyboardButton(text=texter("btn_group_02",
SETTINGS["LENGUAGE"]),callback_data="LG"+data)],
[telegram.InlineKeyboardButton(text=texter("btn_group_03",
SETTINGS["LENGUAGE"]),callback_data="UG"+data)]]
keyboard = telegram.InlineKeyboardMarkup(a)
if query:
sent = bot.edit_message_text(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
text=message,
parse_mode=telegram.ParseMode.HTML,
reply_markup=keyboard)
return sent
else:
sent = bot.sendMessage(chat_id=cid,text=message,
parse_mode=telegram.ParseMode.HTML,
reply_markup=keyboard)
SquadManager.add_group_message_id(cid,sent.message_id,group)
return sent
def expire_group(*args):
SquadManager.expire_group()
return True
def expire_message(bot,job):
mid,cid = SquadManager.expire_message()
bot.delete_message(chat_id=cid,message_id=mid)
return True
# General commands -----------------------------------------------------
def command_help(bot, update, pass_chat_data=True):
cid = update.message.chat_id
bot.sendMessage(chat_id=cid,
text=texter("help",SETTINGS["LENGUAGE"]),
parse_mode=telegram.ParseMode.HTML)
def command_about(bot, update, pass_chat_data=True):
cid = update.message.chat_id
bot.sendMessage(chat_id=cid,
text=texter("about",SETTINGS["LENGUAGE"]),
parse_mode=telegram.ParseMode.HTML)
def command_create_duo(bot,update,args=[],chat_data=True,job_queue=True):
try:
cid = update.message.chat_id
uid = update.message.from_user.id
user = "@"+str(update.message.from_user.username)
a = args_time(args)
if user == "@None":
bot.sendMessage(chat_id=cid,
text=texter("warning02",SETTINGS["LENGUAGE"]),
parse_mode=telegram.ParseMode.HTML)
elif a:
info = SquadManager.create_duo(user,cid)
if info == "try_again":
message = texter("duo02",SETTINGS["LENGUAGE"]) % (user)
bot.sendMessage(chat_id=cid,text=message,
parse_mode=telegram.ParseMode.HTML)
else:
a = (a[0],a[1],user)
message = texter("duo01",SETTINGS["LENGUAGE"]) % a
sent = send_create_group(bot,cid,message,user)
job = job_queue.run_once(expire_group,172800,context=cid)
SquadManager.to_register_job(cid,sent.message_id,job,info)
else:
message = texter("duo03",SETTINGS["LENGUAGE"])
sent = bot.sendMessage(chat_id=cid,text=message,
parse_mode=telegram.ParseMode.HTML)
SquadManager.add_message_id(sent.message_id, cid)
job_queue.run_once(expire_message,45,context=cid)
except Exception as e:
print(e)
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
def command_create_squad(bot,update,args=[],chat_data=True,job_queue=True):
try:
cid = update.message.chat_id
uid = update.message.from_user.id
user = "@"+str(update.message.from_user.username)
a = args_time(args)
if user == "@None":
bot.sendMessage(chat_id=cid,
text=texter("warning02",SETTINGS["LENGUAGE"]),
parse_mode=telegram.ParseMode.HTML)
elif a:
info = SquadManager.create_squad(user,cid)
if info == "try_again":
message = texter("squad02",SETTINGS["LENGUAGE"]) % (user)
bot.sendMessage(chat_id=cid,text=message,
parse_mode=telegram.ParseMode.HTML)
else:
a = (a[0],a[1],user)
message = texter("squad01",SETTINGS["LENGUAGE"]) % a
sent = send_create_group(bot,cid,message,user)
job = job_queue.run_once(expire_group,172800,context=cid)
SquadManager.to_register_job(cid,sent.message_id,job,info)
else:
message = texter("squad03",SETTINGS["LENGUAGE"])
sent = bot.sendMessage(chat_id=cid,text=message,
parse_mode=telegram.ParseMode.HTML)
SquadManager.add_message_id(sent.message_id, cid)
job_queue.run_once(expire_message,45,context=cid)
except Exception as e:
print(e)
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
def command_refloat(bot, update, chat_data=True):
try:
cid = update.message.chat_id
mid = update.message.reply_to_message.message_id
text = update.message.reply_to_message.text
a = text.split("\n")[0].split(" ")
a = "<b>%s</b> %s %s <b>%s %s</b>" % tuple(a)
u = text.split("\n")[1]
message = a+"\n"+u
user = text.split("\n")[1].split(" ")[2]
group = SquadManager.get_group_message_id(cid,mid)
if group:
bot.delete_message(chat_id=cid,message_id=mid)
sent = send_create_group(bot,cid,message,user,False,group)
SquadManager.repalace_job_id(cid,sent.message_id,cid,mid)
else:
bot.sendMessage(chat_id=cid,
text=texter("warning01",SETTINGS["LENGUAGE"]),
parse_mode=telegram.ParseMode.HTML)
except Exception as e:
print(e)
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
def command_eliminate(bot, update, chat_data=True):
cid = update.message.chat_id
mid = update.message.reply_to_message.message_id
user = "@"+str(update.message.from_user.username)
if user in SETTINGS["SUPERUSERS"]:
SquadManager.delete_group_job(cid,mid)
bot.sendMessage(chat_id=cid,
text=texter("warning03",SETTINGS["LENGUAGE"]),
parse_mode=telegram.ParseMode.HTML)
def command_humor(bot, update, pass_chat_data=True):
cid = update.message.chat_id
a = ("001","002","003","004","005",
"006","007","008","009","010",
"011","012","013")
pic = "images/humor/humor_"+choice(a)+".jpg"
bot.sendPhoto(chat_id=cid,
photo=open(pic,"rb"))
# General --------------------------------------------------------------
def callback_handler(bot, update):
try:
query = update.callback_query
uid = update.callback_query.from_user.id
cid = query.message.chat_id
user = "@"+str(query.from_user.username)
data = query.data.split(" ")
a = query.message.text.split("\n")[0].split(" ")
a = "<b>%s</b> %s %s <b>%s %s</b>" % tuple(a)
message = a+"\n"+query.message.text.split("\n")[1]
expired = False
if user == "@None":
bot.sendMessage(chat_id=cid,
text=texter("warning02",SETTINGS["LENGUAGE"]),
parse_mode=telegram.ParseMode.HTML)
elif data[0] == "JG": # Joint group
info = SquadManager.join(user,uid,data[1],True)
elif data[0] == "LG": # Leave group
info = SquadManager.leave(user,data[1])
elif data[0] == "UG": # Undecided group
info = SquadManager.join(user,uid,data[1],False)
if not info and user != "@None":
bot.edit_message_text(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
text=texter("warning01",SETTINGS["LENGUAGE"]),
parse_mode=telegram.ParseMode.HTML)
elif user != "@None":
send_create_group(bot,cid,message,data[2],query,data[1])
except Exception as e:
print(e)
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
def listener(bot, update):
cid = update.message.chat_id
message = update.message.text
print("ID: %s \n Message: %s" % (cid, message))
def main():
#bot = telegram.Bot(token=SETTINGS["TEST_BOT_TOKEN"])
bot = telegram.Bot(token=SETTINGS["BOT_TOKEN"])
botupdater = Updater(bot.token)
# Add handlers
dp = botupdater.dispatcher
#dp.add_handler(MessageHandler(Filters.text, listener))
dp.add_handler(CallbackQueryHandler(callback_handler))
# Commands handlers
dp.add_handler(CommandHandler('help',command_help))
dp.add_handler(CommandHandler('about',command_about))
dp.add_handler(CommandHandler('crearDuo',command_create_duo,
pass_args=True,pass_chat_data=True,pass_job_queue=True))
dp.add_handler(CommandHandler('crearSquad',command_create_squad,
pass_args=True,pass_chat_data=True,pass_job_queue=True))
dp.add_handler(CommandHandler('refloat',command_refloat,
pass_chat_data=True))
dp.add_handler(CommandHandler('eliminar',command_eliminate,
pass_chat_data=True))
dp.add_handler(CommandHandler('humor',command_humor))
# Initialize the bot
botupdater.start_polling()
botupdater.idle()
if __name__ == "__main__":
main()