-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
handlers.py
56 lines (38 loc) · 1.93 KB
/
handlers.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
from telegram import Update
from telegram.ext import CommandHandler, RegexHandler, MessageHandler
import tg_bot.modules.sql.blacklistusers_sql as sql
from tg_bot import ALLOW_EXCL
if ALLOW_EXCL:
CMD_STARTERS = ('/', '!')
else:
CMD_STARTERS = ('/',)
class CustomCommandHandler(CommandHandler):
def __init__(self, command, callback, **kwargs):
if "admin_ok" in kwargs:
del kwargs["admin_ok"]
super().__init__(command, callback, **kwargs)
def check_update(self, update):
if isinstance(update, Update) and (update.message or update.edited_message and self.allow_edited):
message = update.message or update.edited_message
if sql.is_user_blacklisted(update.effective_user.id):
return False
if message.text and len(message.text) > 1:
fst_word = message.text_html.split(None, 1)[0]
if len(fst_word) > 1 and any(fst_word.startswith(start) for start in CMD_STARTERS):
command = fst_word[1:].split('@')
command.append(message.bot.username) # in case the command was sent without a username
if self.filters is None:
res = True
elif isinstance(self.filters, list):
res = any(func(message) for func in self.filters)
else:
res = self.filters(message)
return res and (command[0].lower() in self.command
and command[1].lower() == message.bot.username.lower())
return False
class CustomRegexHandler(RegexHandler):
def __init__(self, pattern, callback, friendly="", **kwargs):
super().__init__(pattern, callback, **kwargs)
class CustomMessageHandler(MessageHandler):
def __init__(self, filters, callback, friendly="", **kwargs):
super().__init__(filters, callback, **kwargs)