forked from Taosky/telegram-search-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
64 lines (48 loc) · 1.66 KB
/
utils.py
File metadata and controls
64 lines (48 loc) · 1.66 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
import re
from threading import Thread
import functools
import config
import time
def delay_delete(bot, chat_id, message_id):
time.sleep(config.SENT_DELETE_DELAY)
bot.delete_message(chat_id=chat_id, message_id=message_id)
def auto_delete(fn):
@functools.wraps(fn)
def wrapper(*args, **kw):
bot = args[0]
sent_message = fn(*args, **kw)
if sent_message:
if config.AUTO_DELETE:
Thread(target=delay_delete, args=[bot, sent_message.chat_id, sent_message.message_id]).start()
return sent_message
return wrapper
def build_menu(buttons, n_cols, header_buttons=None, footer_buttons=None):
menu = [buttons[i:i + n_cols] for i in range(0, len(buttons), n_cols)]
if header_buttons:
menu.insert(0, header_buttons)
if footer_buttons:
menu.append(footer_buttons)
return menu
# calculate num of non-ascii characters
def len_non_ascii(data):
temp = re.findall('[^a-zA-Z0-9.]+', data)
count = 0
for i in temp:
count += len(i)
return count
def delete_prev_message(bot, update):
bot_id = get_bot_id(bot)
# 检查权限
if not bot.get_chat_member(config.GROUP_ID, bot_id).can_delete_messages:
return
if not update.message:
prev_msg_id = update.callback_query.message.message_id
prev_msg_chat_id = update.callback_query.message.chat.id
else:
prev_msg_id = update.message.message_id
prev_msg_chat_id = update.message.chat_id
bot.delete_message(chat_id=prev_msg_chat_id, message_id=prev_msg_id)
def get_bot_user_name(bot):
return bot.get_me().username
def get_bot_id(bot):
return bot.get_me().id