-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
100 lines (77 loc) · 4.48 KB
/
Copy pathutil.py
File metadata and controls
100 lines (77 loc) · 4.48 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
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
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Message, \
BotCommand, MenuButtonCommands, BotCommandScopeChat, MenuButtonDefault
from telegram import Update
from telegram.constants import ParseMode
from telegram.ext import ContextTypes
# конвертирует объект user в строку
def dialog_user_info_to_str(user_data) -> str:
mapper = {'language_from': 'Язык оригинала', 'language_to': 'Язык перевода',
'text_to_translate': 'Текст для перевода'}
return '\n'.join(map(lambda k, v: (mapper[k], v), user_data.items()))
# посылает в чат текстовое сообщение
async def send_text(update: Update, context: ContextTypes.DEFAULT_TYPE,
text: str) -> Message:
if text.count('_') % 2 != 0:
message = f"Строка '{text}' является невалидной с точки зрения markdown. Воспользуйтесь методом send_html()"
print(message)
return await update.message.reply_text(message)
text = text.encode('utf16', errors='surrogatepass').decode('utf16')
return await context.bot.send_message(chat_id=update.effective_chat.id,
text=text,
parse_mode=ParseMode.MARKDOWN)
# посылает в чат html сообщение
async def send_html(update: Update, context: ContextTypes.DEFAULT_TYPE,
text: str) -> Message:
text = text.encode('utf16', errors='surrogatepass').decode('utf16')
return await context.bot.send_message(chat_id=update.effective_chat.id,
text=text, parse_mode=ParseMode.HTML)
# посылает в чат текстовое сообщение, и добавляет к нему кнопки
async def send_text_buttons(update: Update, context: ContextTypes.DEFAULT_TYPE,
text: str, buttons: dict) -> Message:
text = text.encode('utf16', errors='surrogatepass').decode('utf16')
keyboard = []
for key, value in buttons.items():
button = InlineKeyboardButton(str(value), callback_data=str(key))
keyboard.append([button])
reply_markup = InlineKeyboardMarkup(keyboard)
return await context.bot.send_message(
update.effective_message.chat_id,
text=text, reply_markup=reply_markup,
message_thread_id=update.effective_message.message_thread_id)
# посылает в чат фото
async def send_image(update: Update, context: ContextTypes.DEFAULT_TYPE,
name: str) -> Message:
with open(f'resources/images/{name}.jpg', 'rb') as image:
return await context.bot.send_photo(chat_id=update.effective_chat.id,
photo=image)
# отображает команду и главное меню
async def show_main_menu(update: Update, context: ContextTypes.DEFAULT_TYPE,
commands: dict):
command_list = [BotCommand(key, value) for key, value in commands.items()]
await context.bot.set_my_commands(command_list, scope=BotCommandScopeChat(
chat_id=update.effective_chat.id))
await context.bot.set_chat_menu_button(menu_button=MenuButtonCommands(),
chat_id=update.effective_chat.id)
# Удаляем команды для конкретного чата
async def hide_main_menu(update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.delete_my_commands(
scope=BotCommandScopeChat(chat_id=update.effective_chat.id))
await context.bot.set_chat_menu_button(menu_button=MenuButtonDefault(),
chat_id=update.effective_chat.id)
# загружает сообщение из папки /resources/messages/
def load_message(name):
with open("resources/messages/" + name + ".txt", "r",
encoding="utf8") as file:
return file.read()
# загружает промпт из папки /resources/messages/
def load_prompt(name):
with open("resources/prompts/" + name + ".txt", "r",
encoding="utf8") as file:
return file.read()
async def default_callback_handler(update: Update,
context: ContextTypes.DEFAULT_TYPE):
await update.callback_query.answer()
query = update.callback_query.data
await send_html(update, context, f'You have pressed button with {query} callback')
class Dialog:
pass