-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegramwrapper.py
89 lines (75 loc) · 2.82 KB
/
telegramwrapper.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
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, RegexHandler
import logging
logger = logging.getLogger(__name__)
class Command(object):
def __init__(self, name, function, pass_args):
self._name = name
self._function = function
self._pass_args = pass_args
@property
def name(self):
return self._name
@property
def function(self):
return self._function
@property
def pass_args(self):
return self._pass_args
class App(object):
def __init__(self, token, access_checker, bootstrap_retries=-1):
self._token = token
self._access_checker = access_checker
self._bootstrap_retries = bootstrap_retries
self._commands = []
def _secure_function(self, f):
def inner(*args, **kwargs):
bot = args[0]
update = args[1]
if not self._access_checker.is_allowed(update.message.from_user):
logger.warn(
'Unauthorized user access {}'
.format(update.message.from_user)
)
bot.sendMessage(
update.message.chat_id,
text="You are not allowed to use this bot"
)
else:
try:
f(*args, **kwargs)
except BaseException as error:
logger.exception(
'Update "%s" caused error "%s"' % (update, error))
bot.sendMessage(
update.message.chat_id,
text="An error occurred: {}".format(str(error))
)
return inner
def _unknown_command(self, bot, update):
bot.sendMessage(update.message.chat_id, text="Invalid command")
def _message(self, bot, update):
bot.sendMessage(update.message.chat_id, text="Please send a command")
def _error(self, bot, update, error):
logger.warn('Update "%s" caused error "%s"' % (update, error))
def add_command(self, command):
self._commands.append(command)
def run(self):
updater = Updater(self._token)
dispatcher = updater.dispatcher
for command in self._commands:
dispatcher.add_handler(CommandHandler(
command.name,
self._secure_function(command.function),
pass_args=command.pass_args
))
dispatcher.add_handler(MessageHandler(
[Filters.text],
self._secure_function(self._message)
))
dispatcher.add_handler(RegexHandler(
'/.*',
self._secure_function(self._unknown_command)
))
dispatcher.add_error_handler(self._error)
updater.start_polling(bootstrap_retries=self._bootstrap_retries)
updater.idle()