forked from Taosky/telegram-search-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
132 lines (99 loc) · 3.11 KB
/
utils.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
import re
from threading import Thread
import functools
import gettext
import time
import json
import os
CONFIG_FILE = './config/.config.json'
USERBOT_CHAT_MEMBERS_FILE = '.userbot_chat_members'
USERBOT_ADMIN_FILE = '.userbot_admin'
def get_text_func():
# Init i18n func
gettext.bindtextdomain('bot', 'locale')
gettext.textdomain('bot')
_ = gettext.gettext
return _
def delay_delete(bot, chat_id, message_id):
time.sleep(60)
bot.delete_message(chat_id=chat_id, message_id=message_id)
def auto_delete(fn):
@functools.wraps(fn)
def wrapper(*args, **kw):
bot = args[1].bot
sent_message = fn(*args, **kw)
if sent_message:
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 get_bot_user_name(bot):
return bot.get_me().username
def get_bot_id(bot):
return bot.get_me().id
def read_config():
if not os.path.exists(CONFIG_FILE):
return None
f = open(CONFIG_FILE)
config_dict = json.load(f)
f.close()
return config_dict
def check_control_permission(from_user_id):
try:
config = read_config()
if config['enable']:
if from_user_id in config['group_admins']:
return True
else:
return False
else:
return None
except:
return None
def load_chat_members():
if not os.path.exists(USERBOT_CHAT_MEMBERS_FILE):
with open(USERBOT_CHAT_MEMBERS_FILE, 'w') as f:
json.dump({}, f)
with open(USERBOT_CHAT_MEMBERS_FILE, 'r') as f:
chat_members = json.load(f)
return chat_members
def write_chat_members(chat_members):
with open(USERBOT_CHAT_MEMBERS_FILE, 'w') as f:
json.dump(chat_members, f)
def get_filter_chats(user_id):
filter_chats = []
chat_members = load_chat_members()
for chat_id in chat_members:
if user_id in chat_members[chat_id]['members']:
filter_chats.append((int(chat_id),chat_members[chat_id]['title']))
return filter_chats
def is_userbot_mode():
env = os.getenv("USER_BOT")
if not env:
return False
return os.getenv("USER_BOT")=="1"
def update_userbot_admin_id(user_id):
current_id = -1
if os.path.exists(USERBOT_ADMIN_FILE):
with open(USERBOT_ADMIN_FILE) as f:
current_id = int(f.read().strip())
if current_id != user_id:
with open(USERBOT_ADMIN_FILE, 'w') as f:
f.write(str(user_id))
def read_userbot_admin_id():
with open(USERBOT_ADMIN_FILE) as f:
current_id = int(f.read().strip())
return current_id