This repository has been archived by the owner on May 30, 2021. It is now read-only.
forked from fossifer/Telegram-CAPTCHA-bot
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.py
474 lines (439 loc) · 18.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
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
# !/usr/bin/env python3
# -*- coding: UTF-8 -*-
import json
import logging
import threading
from configparser import ConfigParser
from pyrogram import (Client, filters)
from pyrogram.errors import ChatAdminRequired, ChannelPrivate, MessageNotModified
from pyrogram.types import (InlineKeyboardMarkup, InlineKeyboardButton, User, Message, ChatPermissions, CallbackQuery)
from Timer import Timer
from challenge import Challenge
_app: Client = None
# _challenge_scheduler = sched.scheduler(time, sleep)
_current_challenges = dict()
_cch_lock = threading.Lock()
_config = dict()
'''
读 只 读 配 置
'''
cf = ConfigParser() # 启用ConfigParser读取那些启动后即不会再被更改的数据,如BotToken等
cf.read("auth.ini")
_admin_user = cf.getint("bot", "admin")
_token = cf.get("bot", "token")
_api_id = cf.getint("bot", "api_id")
_api_hash = cf.get("bot", "api_hash")
_channel = cf.getint("bot", "channel")
logging.basicConfig(level=logging.INFO)
# 设置一下日志记录,能够在诸如 systemctl status captchabot 这样的地方获得详细输出。
def load_config():
global _config
with open("config.json", encoding="utf-8") as f:
_config = json.load(f)
def save_config():
with open("config.json", "w") as f:
json.dump(_config, f, indent=4)
def _update(app):
@app.on_message(filters.command("reload") & filters.private)
async def reload_cfg(client: Client, message: Message):
_me: User = await client.get_me()
logging.info(message.text)
if message.from_user.id == _admin_user:
save_config()
load_config()
await message.reply("配置已成功重载。")
else:
logging.info("Permission denied, admin user in config is:" + _admin_user)
pass
@app.on_message(filters.command("help") & filters.group)
async def helping_cmd(client: Client, message: Message):
_me: User = await client.get_me()
logging.info(message.text)
await message.reply(_config["*"]["msg_self_introduction"],
disable_web_page_preview=True)
@app.on_message(filters.command("ping") & filters.private)
async def ping_command(client: Client, message: Message):
await message.reply("poi~")
@app.on_message(filters.command("start") & filters.private)
async def start_command(client: Client, message: Message):
await message.reply(_start_message)
@app.on_message(filters.command("leave") & filters.private)
async def leave_command(client: Client, message: Message):
chat_id = message.text.split()[-1]
if message.from_user.id == _admin_user:
try:
await client.send_message(int(chat_id),
_config["msg_leave_msg"])
await client.leave_chat(int(chat_id), True)
except:
await message.reply("指令出错了!可能是bot不在参数所在群里。")
else:
await message.reply("已离开群组: `" + chat_id + "`",
parse_mode="Markdown")
_me: User = await client.get_me()
try:
await client.send_message(
int(_channel),
_config["msg_leave_group"].format(
botid=str(_me.id),
groupid=chat_id,
),
parse_mode="Markdown")
except Exception as e:
logging.error(str(e))
else:
pass
@app.on_message(filters.new_chat_members)
async def challenge_user(client: Client, message: Message):
target = message.new_chat_members[0]
if message.from_user.id != target.id:
if target.is_self:
group_config = _config.get(str(message.chat.id), _config["*"])
try:
await client.send_message(
message.chat.id, group_config["msg_self_introduction"])
_me: User = await client.get_me()
try:
await client.send_message(
int(_channel),
_config["msg_into_group"].format(
botid=str(_me.id),
groupid=str(message.chat.id),
grouptitle=str(message.chat.title),
),
parse_mode="Markdown",
)
except Exception as e:
logging.error(str(e))
except ChannelPrivate:
return
return
try:
await client.restrict_chat_member(
chat_id=message.chat.id,
user_id=target.id,
permissions=ChatPermissions(can_send_stickers=False,
can_send_messages=False,
can_send_media_messages=False,
can_add_web_page_previews=False,
can_send_polls=False,
can_send_animations=False))
except ChatAdminRequired:
return
group_config = _config.get(str(message.chat.id), _config["*"])
challenge = Challenge()
def generate_challenge_button(e):
choices = []
answers = []
for c in e.choices():
answers.append(
InlineKeyboardButton(str(c),
callback_data=bytes(
str(c), encoding="utf-8")))
choices.append(answers)
return choices + [[
InlineKeyboardButton(group_config["msg_approve_manually"],
callback_data=b"+"),
InlineKeyboardButton(group_config["msg_refuse_manually"],
callback_data=b"-"),
]]
timeout = group_config["challenge_timeout"]
reply_message = await client.send_message(
message.chat.id,
group_config["msg_challenge"].format(target=target.first_name,
target_id=target.id,
timeout=timeout,
challenge=challenge.qus()),
reply_to_message_id=message.message_id,
reply_markup=InlineKeyboardMarkup(
generate_challenge_button(challenge)),
)
_me: User = await client.get_me()
chat_id = message.chat.id
chat_title = message.chat.title
target = message.from_user.id
timeout_event = Timer(
challenge_timeout(client, message.chat.id, message.from_user.id,
reply_message.message_id),
timeout=group_config["challenge_timeout"],
)
_cch_lock.acquire()
_current_challenges["{chat}|{msg}".format(
chat=message.chat.id,
msg=reply_message.message_id)] = (challenge, message.from_user.id,
timeout_event)
_cch_lock.release()
@app.on_callback_query()
async def challenge_callback(client: Client,
callback_query: CallbackQuery):
query_data = str(callback_query.data)
query_id = callback_query.id
chat_id = callback_query.message.chat.id
user_id = callback_query.from_user.id
msg_id = callback_query.message.message_id
chat_title = callback_query.message.chat.title
user_name = callback_query.from_user.first_name
group_config = _config.get(str(chat_id), _config["*"])
if query_data in ["+", "-"]:
admins = await client.get_chat_members(chat_id,
filter="administrators")
if not any([
admin.user.id == user_id and
(admin.status == "creator" or admin.can_restrict_members)
for admin in admins
]):
await client.answer_callback_query(
query_id, group_config["msg_permission_denied"])
return
ch_id = "{chat}|{msg}".format(chat=chat_id, msg=msg_id)
_cch_lock.acquire()
# target: int = None
timeout_event: None
challenge, target, timeout_event = _current_challenges.get(
ch_id, (None, None, None))
if ch_id in _current_challenges:
# 预防异常
del _current_challenges[ch_id]
_cch_lock.release()
timeout_event.stop()
if query_data == "+":
try:
await client.restrict_chat_member(
chat_id,
target,
permissions=ChatPermissions(
can_send_stickers=True,
can_send_messages=True,
can_send_media_messages=True,
can_send_polls=True))
except ChatAdminRequired:
await client.answer_callback_query(
query_id, group_config["msg_bot_no_permission"])
return
await client.edit_message_text(
chat_id,
msg_id,
group_config["msg_approved"].format(user=user_name),
reply_markup=None,
)
_me: User = await client.get_me()
try:
await client.send_message(
int(_channel),
_config["msg_passed_admin"].format(
botid=str(_me.id),
targetuser=str(target),
groupid=str(chat_id),
grouptitle=str(chat_title),
),
parse_mode="Markdown",
)
except Exception as e:
logging.error(str(e))
else:
try:
await client.kick_chat_member(chat_id, target)
except ChatAdminRequired:
await client.answer_callback_query(
query_id, group_config["msg_bot_no_permission"])
return
await client.edit_message_text(
chat_id,
msg_id,
group_config["msg_refused"].format(user=user_name),
reply_markup=None,
)
_me: User = await client.get_me()
try:
await client.send_message(
int(_channel),
_config["msg_failed_admin"].format(
botid=str(_me.id),
targetuser=str(target),
groupid=str(chat_id),
grouptitle=str(chat_title),
),
parse_mode="Markdown",
)
except Exception as e:
logging.error(str(e))
await client.answer_callback_query(query_id)
return
ch_id = "{chat}|{msg}".format(chat=chat_id, msg=msg_id)
_cch_lock.acquire()
challenge, target, timeout_event = _current_challenges.get(
ch_id, (None, None, None))
_cch_lock.release()
if user_id != target:
await client.answer_callback_query(
query_id, group_config["msg_challenge_not_for_you"])
return None
timeout_event.stop()
try:
await client.restrict_chat_member(
chat_id,
target,
permissions=ChatPermissions(can_send_stickers=True,
can_send_messages=True,
can_send_media_messages=True,
can_send_polls=True))
except ChatAdminRequired:
pass
correct = str(challenge.ans()) == query_data
if correct:
try:
await client.edit_message_text(
chat_id,
msg_id,
group_config["msg_challenge_passed"],
reply_markup=None)
_me: User = await client.get_me()
except MessageNotModified as e:
await client.send_message(int(_channel),
'Bot 运行时发生异常: `' + str(e) + "`")
try:
await client.send_message(
int(_channel),
_config["msg_passed_answer"].format(
botid=str(_me.id),
targetuser=str(target),
groupid=str(chat_id),
grouptitle=str(chat_title),
),
parse_mode="Markdown",
)
except Exception as e:
logging.error(str(e))
else:
if not group_config["use_strict_mode"]:
await client.edit_message_text(
chat_id,
msg_id,
group_config["msg_challenge_mercy_passed"],
reply_markup=None,
)
_me: User = await client.get_me()
try:
await client.send_message(
int(_channel),
_config["msg_passed_mercy"].format(
botid=str(_me.id),
targetuser=str(target),
groupid=str(chat_id),
grouptitle=str(chat_title),
),
parse_mode="Markdown",
)
except Exception as e:
logging.error(str(e))
else:
try:
await client.edit_message_text(
chat_id,
msg_id,
group_config["msg_challenge_failed"],
reply_markup=None,
)
# await client.restrict_chat_member(chat_id, target)
_me: User = await client.get_me()
try:
await client.send_message(
int(_channel),
_config["msg_failed_answer"].format(
botid=str(_me.id),
targetuser=str(target),
groupid=str(chat_id),
grouptitle=str(chat_title),
),
parse_mode="Markdown",
)
except Exception as e:
logging.error(str(e))
except ChatAdminRequired:
return
if group_config["challenge_timeout_action"] == "ban":
await client.kick_chat_member(chat_id, user_id)
elif group_config["challenge_timeout_action"] == "kick":
await client.kick_chat_member(chat_id, user_id)
await client.unban_chat_member(chat_id, user_id)
elif group_config["challenge_timeout_action"] == "mute":
await client.restrict_chat_member(
chat_id,
user_id,
permissions=ChatPermissions(
can_send_other_messages=False,
can_send_messages=False,
can_send_media_messages=False,
can_add_web_page_previews=False,
can_send_polls=False))
else:
pass
if group_config["delete_failed_challenge"]:
Timer(
client.delete_messages(chat_id, msg_id),
group_config["delete_failed_challenge_interval"],
)
if group_config["delete_passed_challenge"]:
Timer(
client.delete_messages(chat_id, msg_id),
group_config["delete_passed_challenge_interval"],
)
async def challenge_timeout(client: Client, chat_id, from_id, reply_id):
global _current_challenges
_me: User = await client.get_me()
group_config = _config.get(str(chat_id), _config["*"])
_cch_lock.acquire()
del _current_challenges["{chat}|{msg}".format(chat=chat_id,
msg=reply_id)]
_cch_lock.release()
# TODO try catch
await client.edit_message_text(
chat_id=chat_id,
message_id=reply_id,
text=group_config["msg_challenge_failed"],
reply_markup=None,
)
await client.send_message(chat_id=_channel,
text=_config["msg_failed_timeout"].format(
botid=str(_me.id),
targetuser=str(from_id),
groupid=str(chat_id)))
if group_config["challenge_timeout_action"] == "ban":
await client.kick_chat_member(chat_id, from_id)
elif group_config["challenge_timeout_action"] == "kick":
await client.kick_chat_member(chat_id, from_id)
await client.unban_chat_member(chat_id, from_id)
else:
pass
if group_config["delete_failed_challenge"]:
Timer(
client.delete_messages(chat_id, reply_id),
group_config["delete_failed_challenge_interval"],
)
def _main():
global _app, _channel, _start_message, _config
load_config()
_start_message = _config["msg_start_message"]
_proxy_ip = _config["proxy_addr"].strip()
_proxy_port = _config["proxy_port"].strip()
if _proxy_ip and _proxy_port:
_app = Client("bot",
bot_token=_token,
api_id=_api_id,
api_hash=_api_hash,
proxy=dict(hostname=_proxy_ip, port=int(_proxy_port)))
else:
_app = Client("bot",
bot_token=_token,
api_id=_api_id,
api_hash=_api_hash)
try:
_update(_app)
_app.run()
except KeyboardInterrupt:
quit()
except Exception as e:
logging.error(e)
_main()
if __name__ == "__main__":
_main()