-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
118 lines (93 loc) · 4.45 KB
/
Copy pathutil.py
File metadata and controls
118 lines (93 loc) · 4.45 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Message, \
BotCommand, MenuButtonCommands, BotCommandScopeChat, MenuButtonDefault
from telegram import Update
from telegram.constants import ParseMode
from telegram.ext import ContextTypes
# serializes a User object into a string representation
def dialog_user_info_to_str(user_data: dict) -> str:
mapper = {
'language_from': 'Source language',
'language_to': 'Target language',
'text_to_translate': 'Text to translate'
}
return '\n'.join(
f"{mapper.get(key, key)}: {value}"
for key, value in user_data.items()
)
# sends a text message to the chat
async def send_text(
update: Update,
context: ContextTypes.DEFAULT_TYPE,
text: str
) -> Message:
# validate markdown formatting
if text.count('_') % 2 != 0:
message = (
f"The string '{text}' is not valid Markdown. "
f"Please use send_html() instead."
)
print(message)
return await update.message.reply_text(message)
# handle surrogate pairs correctly
text = text.encode('utf-16', errors='surrogatepass').decode('utf-16')
return await context.bot.send_message(
chat_id=update.effective_chat.id,
text=text,
parse_mode=ParseMode.MARKDOWN
)
# sends an HTML-formatted text message to the chat
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)
# sends a text message to the chat and attaches buttons
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)
# sends a photo to the chat
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)
# displays the command list and the main menu
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)
# clears previously registered commands for the current chat
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)
# loads a message from the /resources/messages/ directory
def load_message(name):
with open("resources/messages/" + name + ".txt", "r",
encoding="utf8") as file:
return file.read()
# loads a predefined prompt from the /resources/messages/ directory
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