-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
SlashBot.py
386 lines (318 loc) · 13.4 KB
/
SlashBot.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
from __future__ import annotations
import os
import sys
import re
import html
import requests
import telegram
from loguru import logger as _logger
from typing import Optional, Union, Any, Callable
from telegram.ext import Updater, MessageHandler, filters, Dispatcher
from functools import partial
from threading import Thread
from time import sleep
from itertools import product as _product
from itertools import starmap
from random import Random, SystemRandom
from collections import deque, Counter
Filters = filters.Filters
parser = re.compile(
r'^(?P<slash>[\\/]_?\$?)'
r'(?P<predicate>([^\s\\]|\\.)*((?<=\S)\\)?)'
r'(\s+(?P<complement>.+))?$'
)
ouenParser = re.compile(
r'^('
r'\\+ .* /+'
r'|'
r'\+ .* /+'
r'|'
r'(\\.*/\s*){2,}'
r'|'
r'(\.*/\s*){2,}'
r'|'
r'\\{2,}/{2,}'
r'|'
r'\{2,}/{2,}'
r')$'
)
pinParser = re.compile(
r'^[\\/]_?pin$'
)
randomStickerParser = re.compile(
r'^([\\/]_?){2,}$'
)
convertEscapes = partial(re.compile(r'\\(\s)').sub, r'\1')
htmlEscape = lambda s: s.replace("&", "&").replace("<", "<").replace(">", ">")
mentionParser = re.compile(r'@([a-zA-Z]\w{4,})')
product = lambda a, b: tuple(map(','.join, _product(a, b)))
PUNCTUATION_TAIL = '.,?!;:~(' \
'。,?!;:~('
class RandomizerMeta(type):
def __init__(cls, name, bases, namespace):
super().__init__(name, bases, namespace)
cls._counter = Counter()
class Randomizer(metaclass=RandomizerMeta):
def __class_getitem__(cls, item: str) -> Any:
collection = getattr(cls, item)
if cls._counter[item] % len(collection) == 0:
random.shuffle(collection)
cls._counter[item] += 1
collection.rotate()
return collection[-1]
class Vegetable(Randomizer):
permission_denied = deque(
product(
{'我太菜了', '我好菜', '我好菜啊', '我菜死了'},
{'pin 不了这条消息', '学不会怎么 pin 这条消息', '连 pin 都不被允许'}
)
+
product(
{'我学不会怎么 pin 这条消息', '这么简单的消息我都 pin 不了', '好想 pin 这条消息啊,但我做不到'},
{'需要浇浇', '怎么会有我这么菜的 bot', '我只能混吃等死', '我怎么会菜成这样'}
)
+
product(
{'这可要我怎么 pin 呀', '怎么才能 pin 这条消息呀', 'pin 不动呀,这可怎么办'},
{'拿大头针钉上吗', '找把锤子敲到柱子上吗', '触及知识盲区了都'}
)
)
reject = deque(
product(
{'我累了', '我好懒,又懒又菜', '我的 bot 生只要像这样躺着混日子就已经很幸福了'},
{'根本不想 pin 这条消息', '才不要 pin 这条消息', '还是另请高明吧', '一点 pin 的动力都没有'}
)
)
class Stickers(Randomizer):
_cnstr = partial(telegram.Sticker,
width=512,
height=512,
is_animated=False,
is_video=False,
type=telegram.Sticker.REGULAR)
stickers = deque(
starmap(
_cnstr,
(
('CAACAgUAAxkBAAInuWOcpUaH6eiL7vc9bIw6GedK-DNzAALNAQAC3x9yGXsiKdzwYgnWLAQ', 'AgADzQEAAt8fchk'),
('CAACAgUAAxkBAAInu2OcpakDQaHnVWVo_80AAVgE7EcujAAC2gEAAt8fchkAAbkxR5lrlfUsBA', 'AgAD2gEAAt8fchk'),
('CAACAgUAAxkBAAInvWOcpcUcJDWODiIxUoSTs840QJaFAALbAQAC3x9yGcc6smv9nZmELAQ', 'AgAD2wEAAt8fchk'),
('CAACAgUAAxkBAAKNaWOcn1dZP5Ooe5wX8JrCKkK2qXGzAALnAQAC3x9yGfJozMIlJl_kLAQ', 'AgAD5wEAAt8fchk'),
('CAACAgUAAxkBAAInt2Ocor-N-gnaJTGR-RtyopIgI0l5AALrAQAC3x9yGWlN_RGM1AESLAQ', 'AgAD6wEAAt8fchk'),
('CAACAgUAAxkBAAInv2Ocph3PT5XiCArmoehOYzCn1sJRAALyAQAC3x9yGSLk1aHaA09kLAQ', 'AgAD8gEAAt8fchk'),
('CAACAgUAAxkBAAInwWOcplEELbRuIBkuy3Yyv6YCScxdAAISAgAC3x9yGaW3ftfZrg8eLAQ', 'AgADEgIAAt8fchk'),
('CAACAgUAAxkBAAInw2Ocpm8VgaPVTUhDPTGHWjnDjzsDAAIbAgAC3x9yGWCDLF4OLhaMLAQ', 'AgADGwIAAt8fchk'),
)
)
)
def __new__(cls) -> telegram.Sticker:
return cls.__class_getitem__('stickers')
_logger.remove()
_logger.add(sys.stderr,
format="<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green>"
"|<level>{level:^8}</level>"
"|<cyan>{extra[username]:^15}</cyan>"
"|<level>{message}</level>",
level="DEBUG")
def log(func: Callable):
def wrapper(update: telegram.Update, ctx: telegram.ext.CallbackContext):
logger = ctx.bot_data['logger']
logger.debug(str(update.to_dict()))
return func(update, ctx, logger)
return wrapper
try:
random = SystemRandom()
except NotImplementedError:
random = Random()
_logger.warning('SystemRandom is not available, using Random instead')
# Docker env
TOKENS = re.compile(r'[^a-zA-Z\-_\d:]+').split(os.environ.get('TOKEN', ''))
if not TOKENS:
raise ValueError('no any valid token found')
TELEGRAM_PROXY = os.environ.get('PROXY', '')
REQUEST_PROXIES = {'all': TELEGRAM_PROXY} if TELEGRAM_PROXY else None
_updaters: list[Updater] = []
class User:
def __init__(self, uid: Optional[int] = None, username: Optional[str] = None, name: Optional[str] = None):
if not (uid and name) and not username:
raise ValueError('invalid user')
self.name = name
self.uid = uid
self.username = username
if not self.name and self.username:
self.__get_user_by_username()
def __get_user_by_username(self):
r = requests.get(f'https://t.me/{self.username}', proxies=REQUEST_PROXIES)
og_t = re.search(r'(?<=<meta property="og:title" content=").*(?=")', r.text, re.IGNORECASE).group(0)
name = html.unescape(og_t) if og_t else None
page_title = re.search(r'(?<=<title>).*(?=</title>)', r.text, re.IGNORECASE).group(0)
if page_title == og_t: # user does not exist
self.name = None
elif name:
self.name = name
def mention(self, mention_self: bool = False, pure: bool = False) -> str:
if not self.name:
return f'@{self.username}'
mention_deep_link = (f'tg://resolve?domain={self.username}'
if (self.username and (not self.uid or self.uid < 0))
else f'tg://user?id={self.uid}')
name = self.name if not mention_self else "自己"
name = htmlEscape(name)
return f'<a href="{mention_deep_link}">{name}</a>' if not pure else name
def __eq__(self, other):
return (
type(self) == type(other)
and (
((self.uid or other.uid) and self.uid == other.uid) or
((self.username or other.username) and self.username == other.username)
)
)
def get_user(msg: telegram.Message) -> User:
user = msg.sender_chat or msg.from_user
return User(name=user.full_name or user.title, uid=user.id, username=user.username)
def get_users(msg: telegram.Message) -> tuple[User, User]:
msg_from = msg
msg_rpl = msg.reply_to_message or msg_from
from_user, rpl_user = get_user(msg_from), get_user(msg_rpl)
return from_user, rpl_user
def parse_command(ctx: telegram.ext.CallbackContext) -> Optional[dict[str, Union[str, bool]]]:
match = ctx.match
parsed = match.groupdict()
predicate = parsed['predicate']
complement = parsed['complement']
if not predicate and complement:
return None # invalid command
omit_le = predicate.endswith('\\')
predicate = predicate[:-1] if omit_le else predicate
predicate = convertEscapes(predicate)
predicate = ctx.bot_data['delUsername'](predicate)
result = {'predicate': htmlEscape(predicate),
'complement': htmlEscape(complement or ''),
'slash': parsed['slash'],
'swap': parsed['slash'] not in ('/', '/$'),
'omit_le': omit_le}
return result
def get_tail(tail_char: str) -> str:
if tail_char in PUNCTUATION_TAIL:
return ''
halfwidth_mark = tail_char.isascii()
return '!' if halfwidth_mark else '!'
def get_text(user_from: User, user_rpl: User, command: dict):
rpl_self = user_from == user_rpl
mention_from = user_from.mention()
mention_rpl = user_rpl.mention(mention_self=rpl_self)
slash, predicate, complement, omit_le = \
command['slash'], command['predicate'], command['complement'], command['omit_le']
if predicate == '':
ret = '!' if not command['swap'] else '¡'
elif predicate == 'me':
ret = f"{mention_from}{bool(complement) * ' '}{complement}"
ret += get_tail((complement or user_from.mention(pure=True))[-1])
elif predicate == 'you':
ret = f"{mention_rpl}{bool(complement) * ' '}{complement}"
ret += get_tail((complement or user_rpl.mention(mention_self=rpl_self, pure=True))[-1])
elif complement:
ret = f"{mention_from} {predicate} {mention_rpl} {complement}"
ret += get_tail(complement[-1])
else:
ret = f"{mention_from} {predicate} "
ret += '了 ' if not omit_le else ''
ret += mention_rpl
ret += get_tail(mention_rpl[-1])
return ret
def reply(update: telegram.Update, ctx: telegram.ext.CallbackContext):
command = parse_command(ctx)
if not command:
return
logger = ctx.bot_data['logger']
logger.debug(str(update.to_dict()))
msg = update.effective_message
from_user, rpl_user = get_users(msg)
if from_user == rpl_user:
mention_match = mentionParser.search(command['predicate'])
if mention_match:
mention = mentionParser.search(msg.text).group(1)
rpl_user = User(username=mention)
command['predicate'] = command['predicate'][:mention_match.start()]
else:
mention_match = mentionParser.search(command['complement'])
if mention_match:
mention = mentionParser.search(msg.text).group(1)
rpl_user = User(username=mention)
complement = command['complement']
complement = complement[:mention_match.start()] + complement[mention_match.end():]
command['complement'] = complement.strip()
if command['swap'] and (not from_user == rpl_user):
(from_user, rpl_user) = (rpl_user, from_user)
text = get_text(from_user, rpl_user, command)
logger.info(text)
update.effective_message.reply_text('\u200e' + text, parse_mode='HTML')
@log
def repeat(update: telegram.Update, ctx: telegram.ext.CallbackContext, logger: _logger):
chat = update.effective_chat
msg = update.effective_message
if chat.id < 0:
chat = ctx.bot.get_chat(chat.id)
logger.info(msg.text)
msg.copy(chat.id) if chat.has_protected_content else msg.forward(chat.id)
@log
def pin(update: telegram.Update, _ctx: telegram.ext.CallbackContext, logger: _logger):
msg = update.effective_message
msg_to_pin = msg.reply_to_message
if not msg_to_pin:
vegetable = f'{Vegetable["reject"]} (Reply to a message to use the command)'
msg.reply_text(vegetable)
logger.warning(vegetable)
return
try:
msg_to_pin.unpin()
msg_to_pin.pin(disable_notification=True)
logger.info(f'Pinned {msg_to_pin.text}')
except telegram.error.BadRequest as e:
vegetable = f'{Vegetable["permission_denied"]} ({e})'
msg_to_pin.reply_text(vegetable)
logger.warning(vegetable)
@log
def random_sticker(update: telegram.Update, _ctx: telegram.ext.CallbackContext, logger: _logger):
msg = update.effective_message
sticker = Stickers()
logger.info(sticker)
msg.reply_sticker(sticker)
def start(token: str):
updater = Updater(token=token, use_context=True, request_kwargs={'proxy_url': TELEGRAM_PROXY})
dp: Dispatcher = updater.dispatcher
dp.add_handler(MessageHandler(Filters.regex(ouenParser) & ~Filters.update.edited_message, repeat, run_async=True))
dp.add_handler(MessageHandler(Filters.regex(randomStickerParser) & ~Filters.update.edited_message, random_sticker,
run_async=True))
dp.add_handler(MessageHandler(Filters.regex(pinParser) & ~Filters.update.edited_message, pin, run_async=True))
dp.add_handler(MessageHandler(Filters.regex(parser) & ~Filters.update.edited_message, reply, run_async=True))
username = f'@{updater.bot.username}'
logger = _logger.bind(username=username)
dp.bot_data['delUsername'] = partial(re.compile(username, re.I).sub, '')
dp.bot_data['logger'] = logger
updater.start_polling()
logger.info('Started')
_updaters.append(updater)
# updater.idle()
def main():
threads: list[Thread] = []
for token in TOKENS:
thread = Thread(target=start, args=(token,), daemon=True)
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
try:
while True:
sleep(1)
except KeyboardInterrupt:
threads_and_logger: list[tuple[Thread, Any]] = []
for updater in _updaters:
thread = Thread(target=updater.stop, daemon=True)
threads_and_logger.append((thread, updater.dispatcher.bot_data['logger']))
thread.start()
for thread, logger in threads_and_logger:
thread.join()
logger.info('Stopped')
if __name__ == '__main__':
main()