Skip to content

Commit

Permalink
feat(bot): add template image
Browse files Browse the repository at this point in the history
  • Loading branch information
exmanka committed Jan 23, 2024
1 parent c8b7bf7 commit 68e1c0a
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 23 deletions.
16 changes: 13 additions & 3 deletions src/handlers/other.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from aiogram.dispatcher.filters import Text
from src.keyboards import user_authorized_kb, user_unauthorized_kb, other_kb
from src.database import postgres_dbms
from src.services import gpt4free, localization as loc
from src.services import gpt4free, internal_functions, localization as loc
from bot_init import bot


Expand All @@ -16,7 +16,12 @@ async def command_help(message: Message):

async def show_project_info(message: Message):
"""Send message with information about project."""
await bot.send_photo(message.from_user.id, loc.other.tfids['project_info'], loc.other.msgs['project_info'], 'HTML', reply_markup=other_kb.faq_inline)
# use safe sending in case new bot tries to send photo using ksiVPN's bot file_id
await internal_functions.send_photo_safely(message.from_user.id,
telegram_file_id=loc.other.tfids['project_info'],
caption=loc.other.msgs['project_info'],
parse_mode='HTML',
reply_markup=other_kb.faq_inline)
# if client is registered
if await postgres_dbms.is_user_registered(message.from_user.id):
await message.answer(markdown.hide_link('https://github.com/exmanka/ksiVPN-telegram-bot') + loc.other.msgs['github'], parse_mode='HTML')
Expand Down Expand Up @@ -50,7 +55,12 @@ async def answer_unrecognized_messages(message: Message):

async def command_start(message: Message, state: FSMContext = None):
"""Send message when user press /start."""
await bot.send_photo(message.from_user.id, loc.other.tfids['hello_message'], loc.other.msgs['hello_message'], 'HTML')
# use safe sending in case new bot tries to send photo using ksiVPN's bot file_id
await internal_functions.send_photo_safely(message.from_user.id,
telegram_file_id=loc.other.tfids['hello_message'],
caption=loc.other.msgs['hello_message'],
parse_mode='HTML',
reply_markup=other_kb.faq_inline)

# if user isn't in db
if not await postgres_dbms.is_user_registered(message.from_user.id):
Expand Down
33 changes: 17 additions & 16 deletions src/handlers/user_authorized.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,13 @@ async def account_configurations_fsm_start(message: Message, state: FSMContext):
async def account_ref_program_fsm_start(message: Message, state: FSMContext):
"""Start FSM for account referral program menu and show account referral program menu keyboard."""
await state.set_state(user_authorized_fsm.AccountMenu.ref_program)
await bot.send_photo(message.from_user.id, loc.auth.tfids['ref_program_info'], loc.auth.msgs['ref_program_info'], parse_mode='HTML', reply_markup=user_authorized_kb.ref_program)

# use safe sending in case new bot tries to send photo using ksiVPN's bot file_id
await internal_functions.send_photo_safely(message.from_user.id,
telegram_file_id=loc.auth.tfids['ref_program_info'],
caption=loc.auth.msgs['ref_program_info'],
parse_mode='HTML',
reply_markup=user_authorized_kb.ref_program)


@user_mw.authorized_only()
Expand Down Expand Up @@ -583,27 +589,22 @@ async def configuration_instruction(call: CallbackQuery):
instruction_text = loc.auth.msgs['instructions'][configuration_protocol_name.lower()][configuration_os.lower()]
instruction_images_list = loc.auth.tfids['instructions'][configuration_protocol_name.lower()][configuration_os.lower()]

# use media group builder in aiogram 3.x.x
# create media group with multiple photos and caption as instruction
media_group = MediaGroup()

# add first image to media group with caption and parse_mode to display caption in instruction
media_group.attach_photo(instruction_images_list[0], caption=instruction_text, parse_mode='HTML')

# add all other photos to media group
if len(instruction_images_list) > 1:
for telegram_file_id in instruction_images_list[1:]:
media_group.attach_photo(telegram_file_id)

# send media group to client
await call.message.reply_media_group(media_group)
# use safe sending in case new bot tries to send photos using ksiVPN's bot files_ids
await internal_functions.reply_media_group_safely(call.message,
telegram_files_ids_list=instruction_images_list,
caption=instruction_text,
parse_mode='HTML')
await call.answer()


@user_mw.authorized_only()
async def show_project_rules(message: Message):
"""Send message with information about project rules."""
await message.answer_photo(loc.auth.tfids['rules'], caption=loc.auth.msgs['rules'], parse_mode='HTML')
# use safe sending in case new bot tries to send photo using ksiVPN's bot file_id
await internal_functions.send_photo_safely(message.from_user.id,
telegram_file_id=loc.auth.tfids['rules'],
caption=loc.auth.msgs['rules'],
parse_mode='HTML')


@user_mw.authorized_only()
Expand Down
2 changes: 1 addition & 1 deletion src/localizations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@
},

"telegram_file_ids": {

"template_image_url": "https://github.com/exmanka/ksiVPN-telegram-bot/assets/74555362/38b4bf5b-bfa8-4904-b8e8-4c492c4eb872"
}
},

Expand Down
2 changes: 1 addition & 1 deletion src/localizations/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@
},

"telegram_file_ids": {

"template_image_url": "https://github.com/exmanka/ksiVPN-telegram-bot/assets/74555362/38b4bf5b-bfa8-4904-b8e8-4c492c4eb872"
}
},

Expand Down
49 changes: 47 additions & 2 deletions src/services/internal_functions.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import asyncio
from aiogram.types import Message
import logging
from aiogram.types import Message, MediaGroup, ReplyKeyboardMarkup, InlineKeyboardMarkup
from aiogram.dispatcher import FSMContext
from aiogram.utils.exceptions import MessageToDeleteNotFound
from aiogram.utils.exceptions import MessageToDeleteNotFound, WrongFileIdentifier
from src.keyboards import user_authorized_kb, admin_kb
from src.states import user_authorized_fsm
from src.database import postgres_dbms
from src.services import aiomoney, localization as loc
from bot_init import bot, ADMIN_ID, YOOMONEY_TOKEN


logger = logging.getLogger(__name__)


async def format_none_string(string: str | None, prefix: str = ' ', postfix: str = '') -> str:
"""Return empty string '' if specified object is None, else return specified specified string with prefix and postfix.
Expand All @@ -22,6 +26,47 @@ async def format_none_string(string: str | None, prefix: str = ' ', postfix: str
return '' if string is None else prefix + string + postfix


async def send_photo_safely(telegram_user_id: int,
telegram_file_id: str,
caption: str | None,
parse_mode: str | None = None,
reply_markup: ReplyKeyboardMarkup | InlineKeyboardMarkup | None = None):
"""Send photo by specified telegram_file_id if telegram_file_id is valid and available for bot. Else send template image by URL."""
try:
await bot.send_photo(telegram_user_id, telegram_file_id, caption, parse_mode, reply_markup=reply_markup)

except WrongFileIdentifier as wrong_file_id:
logger.warning(f"Can't send photo by specified telegram_file_id: {wrong_file_id}. Perhaps you try to send image by file_id from ksiVPN bot. File_id is unique for each individual bot!")
await bot.send_photo(telegram_user_id, loc.internal.tfids['template_image_url'], caption, parse_mode, reply_markup=reply_markup)


async def reply_media_group_safely(message: Message,
telegram_files_ids_list: list[str],
caption: str | None,
parse_mode: str | None = None):
"""Reply message with media group with specified telegram_files_ids if they are valid and available for bot. Else send template media group with image by URL."""
# use media group builder in aiogram 3.x.x
try:
# create media group
media_group = MediaGroup()

# add first image to media group with caption and parse_mode to display caption in instruction
media_group.attach_photo(telegram_files_ids_list[0], caption, parse_mode)

# add all other photos to media group
if len(telegram_files_ids_list) > 1:
for telegram_file_id in telegram_files_ids_list[1:]:
media_group.attach_photo(telegram_file_id)

await message.reply_media_group(media_group)

except WrongFileIdentifier as wrong_file_id:
logger.warning(f"Can't send media group by specified telegram_files_ids: {wrong_file_id}. Perhaps you try to send image by file_id from ksiVPN bot. File_id is unique for each individual bot!")
media_group = MediaGroup()
media_group.attach_photo(loc.internal.tfids['template_image_url'], caption, parse_mode)
await message.reply_media_group(media_group)


async def send_message_by_telegram_id(telegram_id: int, message: Message):
"""Send specified message by provided telegram_id.
Expand Down

0 comments on commit 68e1c0a

Please sign in to comment.