Skip to content

Commit

Permalink
Fix telegram send WebpageCurlFailedError
Browse files Browse the repository at this point in the history
  • Loading branch information
AlberLC committed Aug 6, 2022
1 parent bf961f2 commit 0944d12
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions multibot/bots/telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,28 @@ async def _get_text(self, original_message: constants.TELEGRAM_EVENT | constants

@staticmethod
@return_if_first_empty
async def _prepare_media_to_send(media: Media, force_bytes=False) -> str | io.BytesIO | None:
if media.url and not force_bytes:
def _prepare_media_to_send(media: Media, prefer_bytes=False) -> str | io.BytesIO | None:
def url_file() -> str | None:
if not media.url:
return

if not pathlib.Path(media.url).is_file() and media.source is Source.INSTAGRAM and (not (path_suffix := pathlib.Path(media.url).suffix) or len(path_suffix) > constants.MAX_FILE_EXTENSION_LENGHT):
file = f'{media.url}.{media.type_.extension}'
return f'{media.url}.{media.type_.extension}'
else:
file = media.url
elif media.bytes_:
file = io.BytesIO(media.bytes_)
file.name = f'bot_media.{media.type_.extension}'
return media.url

def bytes_file() -> io.BytesIO | None:
if not media.url:
return

file_ = io.BytesIO(media.bytes_)
file_.name = f'bot_media.{media.type_.extension}'
return file_

if prefer_bytes:
file = bytes_file() or url_file()
else:
return
file = url_file() or bytes_file()

return file

Expand Down Expand Up @@ -382,7 +393,7 @@ async def send(
send_as_file: bool = None,
edit=False,
) -> Message | None:
file = await self._prepare_media_to_send(media)
file = self._prepare_media_to_send(media)
telegram_buttons = None

if buttons:
Expand All @@ -408,10 +419,7 @@ async def send(
if media:
if 'inline_media' not in message.contents:
message.contents['inline_media'] = []
if media.type_ is MediaType.IMAGE:
message.contents['inline_media'].append(message.original_event.builder.photo(file))
else:
message.contents['inline_media'].append(message.original_event.builder.document(file, title=media.type_.name.title(), type=media.type_.name.lower()))
message.contents['inline_media'].append(media)
return
elif edit:
if buttons is not None:
Expand Down Expand Up @@ -452,7 +460,7 @@ async def send(
original_message = await self.client.send_message(chat.original_object, text, buttons=telegram_buttons, reply_to=reply_to, silent=silent, **kwargs)
except telethon.errors.rpcerrorlist.WebpageCurlFailedError:
if media.bytes_:
kwargs['file'] = await self._prepare_media_to_send(media, force_bytes=True)
kwargs['file'] = self._prepare_media_to_send(media, prefer_bytes=True)
original_message = await self.client.send_message(chat.original_object, text, buttons=telegram_buttons, reply_to=reply_to, silent=silent, **kwargs)
else:
raise
Expand All @@ -463,8 +471,18 @@ async def send(

@inline
async def send_inline_results(self, message: Message):
def create_result(media: Media, prefer_bytes=False) -> telethon.types.InputBotInlineResultPhoto | telethon.types.InputBotInlineResultDocument:
file = self._prepare_media_to_send(media, prefer_bytes)
if media.type_ is MediaType.IMAGE:
return message.original_event.builder.photo(file)
else:
return message.original_event.builder.document(file, title=media.type_.name.title(), type=media.type_.name.lower())

try:
await message.original_event.answer(message.contents['inline_media'])
try:
await message.original_event.answer([create_result(media) for media in message.contents['inline_media']])
except telethon.errors.rpcerrorlist.WebpageCurlFailedError:
await message.original_event.answer([create_result(media, prefer_bytes=True) for media in message.contents['inline_media']])
except (KeyError, telethon.errors.rpcerrorlist.QueryIdInvalidError):
pass

Expand Down

0 comments on commit 0944d12

Please sign in to comment.