This repository was archived by the owner on Mar 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.py
364 lines (287 loc) · 11.5 KB
/
api.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
from bases import JSONAPI
from telegram import ParseMode, TelegramError
from telegram.ext import CommandHandler, MessageHandler, Filters
import utils
import pickle
import logging
logger = logging.getLogger(__name__)
class TelegramAPI(object):
"""Class which represents Telegram API between module and wrapper
Attributes:
bot_id(int): bot ID
Args:
updater(:class:`telegram.ext.Updater`): updater instance
storage(:class:`api.StorageAPI`): data for ``main.py``
"""
_commands = []
@utils.log(logger, print_ret=False)
def __init__(self, updater, storage):
self._bot = updater.bot
self._dispatcher = updater.dispatcher
self._storage = storage
@utils.log(logger, print_ret=False)
def _register_command(self, command):
if command in self._commands:
raise ValueError('Command "{0}" is already registered'.format(command))
self._commands.append(command)
@utils.log(logger, print_ret=False)
def register_command(self, commands, callback, allow_edited=False):
"""Registers commands handler
Args:
commands(list|tuple): list of commands to register
callback(function): callable object to execute
allow_edited(Optional[bool]): pass edited messages
Raises:
ValueError: if one of commands in ``commands`` was already registered
"""
for command in commands:
self._register_command(command)
@utils.log(logger, print_ret=False)
def process_update(bot, update):
lang = utils.get_lang(self._storage, update.effective_user)
callback(update.effective_message,
update.effective_message.text.split(' ')[1:], lang)
self._dispatcher.add_handler(CommandHandler(commands, process_update,
allow_edited=allow_edited))
@utils.log(logger, print_ret=False)
def register_text_handler(self, callback, allow_edited=False):
"""Registers text message handler
Args:
callback(function): callable object to execute
allow_edited(Optional[bool]): pass edited messages
"""
@utils.log(logger, print_ret=False)
def process_update(bot, update):
lang = utils.get_lang(self._storage, update.effective_user)
callback(update.effective_message, lang)
self._dispatcher.add_handler(MessageHandler(Filters.text, process_update,
edited_updates=allow_edited))
@utils.log(logger)
def send_text_message(self, chat, text, markdown=False, html=False, reply_to=None,
force_reply_to=False, **kwargs):
"""Sends message
Notes:
For now, this method supports only sending message with markdown or HTML parsing
Args:
chat(int|str): chat ID or '@channel_name'
text(str): text to send
markdown(Optional[bool]): parse text as markdown
html(Optional[bool]): parse text as html
reply_to(Optional[int]): ID of message to reply to
force_reply_to(Optional[bool]): Replies to message even in private chats
Returns:
int|bool: message_id if message was sent, ``False`` otherwise
Raises:
ValueError: if ``markdown`` and ``html`` are both ``True``
"""
if markdown and html:
raise ValueError('`markdown` and `html` are self-exclusive')
if markdown:
parse_mode = ParseMode.MARKDOWN
elif html:
parse_mode = ParseMode.HTML
else:
parse_mode = None
if reply_to and self.is_private_chat(chat) and not force_reply_to:
reply_to = None
try:
msg = self._bot.send_message(chat, text, parse_mode=parse_mode,
reply_to_message_id=reply_to, **kwargs)
except TelegramError as e:
logger.exception('Exception was raised while sending message', exc_info=e)
return False
return msg.message_id
@utils.log(logger)
def delete_message(self, chat=None, message_id=None, message=None):
"""Deletes message
Args:
chat(Optional[int|str]): chat ID or '@channel_name'
message_id(Optional[int]): ID of message to be deleted
message(Optional[:class:`telegram.Message`]): message to be deleted
Returns:
bool: ``True`` on success, ``False`` otherwise
Raises:
ValueError: if ``chat``, ``message_id`` and ``message`` are ``None``
"""
if (chat is None or message_id is None) and message is None:
raise ValueError('Either `chat` and `message_id` or `message` must be given')
if message is not None:
chat = message.chat_id
message_id = message.message_id
try:
return self._bot.delete_message(chat, message_id)
except TelegramError as e:
logger.exception('Exception was raised while deleting message', exc_info=e)
return False
@utils.log(logger)
def ban_member(self, chat, user_id=None, user=None):
"""Bans chat member
Args:
chat(int|str): chat ID or '@channel_name'
user_id(Optional[int]): user ID to be banned
user(Optional[:class:`telegram.User`]): user to be banned
Returns:
bool: ``True`` on success, ``False`` otherwise
Raises:
ValueError: if both ``user_id`` and ``user`` were (not) given
"""
if (user_id is None and user is None) or (user_id is not None and user is not None):
raise ValueError('Either `user_id` or `user` must be given')
if user is not None:
user_id = user.id
try:
self._bot.kick_chat_member(chat, user_id)
except TelegramError as e:
logger.exception('Exception was raised while kicking member', exc_info=e)
return False
return True
@utils.log(logger)
def unban_member(self, chat, user_id=None, user=None):
"""Unbans chat member
Args:
chat(int|str): chat ID or '@channel_name'
user_id(Optional[int]): user ID to be unbanned
user(Optional[:class:`telegram.User`]): user to be unbanned
Returns:
bool: ``True`` on success, ``False`` otherwise
Raises:
ValueError: if both ``user_id`` and ``user`` were (not) given
"""
if (user_id is None and user is None) or (user_id is not None and user is not None):
raise ValueError('Either `user_id` or `user` must be given')
if user is not None:
user_id = user.id
try:
self._bot.unban_chat_member(chat, user_id)
except TelegramError as e:
logger.exception('Exception was raised while unbanning member', exc_info=e)
return False
return True
@utils.log(logger)
def kick_member(self, chat, user_id=None, user=None):
"""Kicks chat member
Args:
chat(int|str): chat ID or '@channel_name'
user_id(Optional[int]): user ID to be unbanned
user(Optional[:class:`telegram.User`]): user to be unbanned
Returns:
bool: ``True`` on success, ``False`` otherwise
Raises:
ValueError: if both ``user_id`` and ``user`` were (not) given
"""
return self.ban_member(chat, user_id, user) and self.unban_member(chat, user_id, user)
@utils.log(logger)
def get_admins(self, chat, use_ids=False):
"""Get chat administrators and return them
Args:
chat(int|str): chat ID or '@channel_name'
use_ids(Optional[bool]): if ``True``, returns list of IDs, otherwise returns list of
``telegram.ChatMember``
Returns:
list: list of admins
"""
admins = self._bot.get_chat_administrators(chat)
if use_ids:
return [admin.user.id for admin in admins]
else:
return list(admins)
@staticmethod
@utils.log(logger)
def is_private_chat(chat):
"""Checks whether ``chat`` is private chat
Notes:
If ``chat`` is str, it is considered as channel
Args:
chat(int|str): chat ID or '@channel_name' to check
"""
return isinstance(chat, int) and chat > 0
@property
def bot_id(self):
return self._bot.id
class ConfigAPI(JSONAPI):
"""This class gives easy access to config files used by module
Args:
name(str): module name
"""
@utils.log(logger, print_ret=False)
def __init__(self, name):
super(ConfigAPI, self).__init__('config', name)
class LangAPI(JSONAPI):
"""This class gives easy access to language files (translations)
Short usage:
>>> tr = LangAPI('foo')
>>> print(tr('en_US', 'bar'))
If desired string cannot be found in specified language, it will fallback to 'en'.
If desired string cannot be found in 'en', ``None`` is returned
Args:
name(str): module name
"""
# TODO: use built-in python translation library
@utils.log(logger, print_ret=False)
def __init__(self, name):
super(LangAPI, self).__init__('lang', name)
@utils.log(logger)
def __getitem__(self, item):
if item is None:
return None
lang = item.split('-')[0]
return super(LangAPI, self).__getitem__(lang)
@utils.log(logger)
def __call__(self, lang, string):
tr = self[lang]
if tr is not None:
tr = tr.get(string.lower(), None)
if tr is None:
return self['en'].get(string.lower(), None)
return tr
class StorageAPI(object):
@utils.log(logger, print_ret=False)
def __init__(self, name, load=True, autosave=True):
from os import path, mkdir
if not path.exists('./data'):
logger.debug('Creating data directory...')
mkdir('./data')
self._file = './data/{0}.pkl'.format(name)
self._autosave = autosave
self._data = None
if load:
self.load()
@utils.log(logger)
def __getattr__(self, item):
return self._data.get(item)
@utils.log(logger, print_args=True)
def __setattr__(self, key, value):
if key[0] != '_':
logger.debug('Setting data key')
self.__dict__['_data'][key] = value
if self._autosave:
self.save()
else:
logger.debug('Setting class property')
super(StorageAPI, self).__setattr__(key, value)
@utils.log(logger, print_ret=False)
def __delattr__(self, item):
del self._data[item]
@property
def raw_data(self):
return self._data
@utils.log(logger)
def load(self):
try:
with open(self._file, 'rb') as file:
self._data = pickle.load(file)
except FileNotFoundError:
self._data = {}
except Exception as e:
logger.exception('Exception in `StorageAPI` was raised', exc_info=e)
return False
return True
@utils.log(logger)
def save(self):
try:
with open(self._file, 'wb') as file:
pickle.dump(self._data, file, pickle.HIGHEST_PROTOCOL)
except Exception as e:
logger.exception('Exception in `StorageAPI` was raised', exc_info=e)
return False
return True