Skip to content

Commit

Permalink
feat - sending just image when is just one
Browse files Browse the repository at this point in the history
  • Loading branch information
jjpaulo2 committed Oct 4, 2024
1 parent 34fc9e5 commit 4d06bc0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
10 changes: 9 additions & 1 deletion function/telegram/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def __init__(self, message: Message):

@cached_property
def _album_images(self) -> list[str]:
return self.message.images[1 : TELEGRAM_MAX_ALBUM_QUANTITY + 1]
if len(self.message.images) > 1:
return self.message.images[1 : TELEGRAM_MAX_ALBUM_QUANTITY + 1]
return []

@cached_property
def _album_caption(self) -> str:
Expand Down Expand Up @@ -83,6 +85,12 @@ def _button(self, text: str, link: str) -> list[InlineKeyboardButton]:
def chat_id(self) -> str:
return self.message.destiny

@cached_property
def image(self) -> str | None:
if len(self.message.images) == 1:
return self.message.images[0]
return None

@cached_property
def album(self) -> list[InputMediaPhoto]:
return [
Expand Down
29 changes: 25 additions & 4 deletions function/telegram/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pyrogram import Client, utils
from pyrogram.enums import ParseMode
from pyrogram.errors import FloodWait
from pyrogram.errors import FloodWait, BadRequest
from aws_lambda_powertools import Logger

from function.settings import TELEGRAM_API_HASH, TELEGRAM_API_ID
Expand Down Expand Up @@ -67,6 +67,23 @@ async def _send_message(
reply_markup=message.buttons,
)

async def _send_image(self, client: Client, message: TelegramMessage):
try:
await client.send_photo(
chat_id=message.chat_id,
photo=message.image, # type: ignore
caption=message.content,
parse_mode=ParseMode.MARKDOWN,
reply_markup=message.buttons,
)

except FloodWait as exc:
raise exc

except BadRequest as exc:
self.logger.warning(str(exc))
await self._send_message(client, message)

async def _send_album(self, client: Client, message: TelegramMessage) -> int:
sent_messages = await client.send_media_group(
chat_id=message.chat_id,
Expand All @@ -82,9 +99,13 @@ async def send(self, message: TelegramMessage):
if message.album:
reply_to = await self._token_rotation(self._send_album, message=message)

await self._token_rotation(
self._send_message, message=message, reply_to=reply_to
)
if message.image:
await self._token_rotation(self._send_image, message=message)

else:
await self._token_rotation(
self._send_message, message=message, reply_to=reply_to
)

except FloodWait as exc:
raise exc
Expand Down

0 comments on commit 4d06bc0

Please sign in to comment.