-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·154 lines (124 loc) · 4.6 KB
/
main.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
#!/usr/bin/env python
import inspect
import logging
import os
import re
import sys
from functools import partial
from typing import (
List,
Optional,
Tuple,
Union,
cast,
)
import telebot
import yandexcloud
from telebot.types import (
BotCommand,
CallbackQuery,
InlineKeyboardMarkup,
Message,
Update,
)
from commands import ReplyFunc, commands
# todo: validate env
# todo: .env file
BOT_TOKEN = os.getenv('BOT_TOKEN')
CLOUD_TOKEN = os.getenv('CLOUD_TOKEN')
FOLDER = os.getenv('FOLDER')
DEBUG_LIB = os.getenv('DEBUG_LIB')
USERS_WHITELIST = os.getenv('TG_USERS_WHITELIST', '').split(';')
telebot.apihelper.ENABLE_MIDDLEWARE = True
bot = telebot.TeleBot(BOT_TOKEN, parse_mode='markdown') # some escaping problem. MarkdownV2 same
logger = logging.getLogger()
logger.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
if DEBUG_LIB:
logging.getLogger('TeleBot').setLevel(logging.DEBUG)
command_regex = re.compile('/(?P<cmd>[^ ]+)(?P<args>.*)')
class UsersWhiteList(telebot.custom_filters.SimpleCustomFilter):
key = 'whitelist'
def check(self, event: Union[Message, CallbackQuery]):
if isinstance(event, CallbackQuery):
message = event.message
else:
message = event
sender = event.from_user.username
if sender not in USERS_WHITELIST:
logger.warning('rejected message from user %r: %r', sender, message.text)
return False
return True
def _reply(
chat_id: int,
message_id: Optional[int],
text: str,
markup: Optional[InlineKeyboardMarkup] = None,
*,
edit: bool,
) -> None:
if message_id and edit:
# throws error if content (text, keyboard) is not changed
bot.edit_message_text(text, chat_id, message_id)
if markup:
bot.edit_message_reply_markup(chat_id, message_id, reply_markup=markup)
else:
bot.send_message(chat_id, text, reply_markup=markup)
def ss_entry(event: dict, _context):
update = Update.de_json(event['body'])
logger.debug('%r', update)
bot.process_new_updates([update])
return {
'statusCode': 200,
}
@bot.middleware_handler(update_types=['message', 'callback_query'])
def log_messages(_bot_instance: telebot.TeleBot, event: Union[Message, CallbackQuery]):
if isinstance(event, CallbackQuery):
logger.info('[%s] callback from user %r: %r',
event.id, event.from_user.username, event.data)
else:
logger.info('[%s] message from user %r: %r', event.id, event.from_user.username, event.text)
@bot.message_handler(whitelist=True, commands=['start', 'help'])
def handle_help(message: Message):
docs = [command.build_help() for command in commands.values()]
doc = '**available commands**:\n\n/help - show this help\n\n' + '\n\n'.join(docs)
bot.send_message(message.chat.id, doc)
@bot.message_handler(whitelist=True, commands=list(commands.keys()))
def handle_commands(message: Message):
reply = cast(ReplyFunc, partial(_reply, message.chat.id, None))
process_command(reply, message.text, str(message.id))
@bot.callback_query_handler(whitelist=True, func=lambda call: True)
def handle_callback_query(call: CallbackQuery):
reply = cast(ReplyFunc, partial(_reply, call.message.chat.id, call.message.id))
process_command(reply, call.data, str(call.id))
bot.answer_callback_query(call.id)
def process_command(reply: ReplyFunc, command_text: str, request_id: str) -> None:
command_name, args = parse_command_args(command_text)
logger.info('[%s] command %r, args %s', request_id, command_name, args)
command_class = commands[command_name]
sdk = yandexcloud.SDK(token=CLOUD_TOKEN)
repo_class = inspect.signature(command_class.__init__).parameters['repo'].annotation
client_stub = inspect.signature(repo_class.__init__).parameters['client'].annotation
client = sdk.client(client_stub)
repo = repo_class(FOLDER, client)
command = command_class(args, request_id, reply, repo=repo)
command.run()
def parse_command_args(text: str) -> Tuple[str, List[str]]:
match = command_regex.match(text).groupdict()
cmd = match['cmd']
args = match.get('args', '').split()
return cmd, args
bot.add_custom_filter(UsersWhiteList())
bot.set_my_commands(
[
BotCommand('help', 'show help'),
] + [
BotCommand(command.name, command.short_doc())
for command in commands.values()
]
)
if __name__ == '__main__':
bot.polling()