Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: added caption extraction when send a msg with a file #4500

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions telethon/client/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,9 @@ async def callback(event):
await client.send_message(chat, 'Hi, future!', schedule=timedelta(minutes=5))
"""
if file is not None:
if isinstance(message, types.Message):
formatting_entities = formatting_entities or message.entities
message = message.message
return await self.send_file(
entity, file, caption=message, reply_to=reply_to,
attributes=attributes, parse_mode=parse_mode,
Expand Down
43 changes: 43 additions & 0 deletions tests/telethon/client/test_messages.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import inspect
from unittest import mock
from unittest.mock import MagicMock

import pytest

from telethon import TelegramClient
from telethon.client import MessageMethods
from telethon.tl.types import PeerChat, MessageMediaDocument, Message, MessageEntityBold


@pytest.mark.asyncio
Expand Down Expand Up @@ -38,3 +42,42 @@ async def send_file(self, entity, file, **kwargs):

client = MockedClient()
assert (await client.send_message('a', file='b', **arguments)) == sentinel


class TestMessageMethods:
@pytest.mark.asyncio
@pytest.mark.parametrize(
'formatting_entities',
([MessageEntityBold(offset=0, length=0)], None)
)
async def test_send_msg_and_file(self, formatting_entities):
async def async_func(result): # AsyncMock was added only in 3.8
return result
msg_methods = MessageMethods()
expected_result = Message(
id=0, peer_id=PeerChat(chat_id=0), message='', date=None,
)
entity = 'test_entity'
message = Message(
id=1, peer_id=PeerChat(chat_id=0), message='expected_caption', date=None,
entities=[MessageEntityBold(offset=9, length=9)],
)
media_file = MessageMediaDocument()

with mock.patch.object(
target=MessageMethods, attribute='send_file',
new=MagicMock(return_value=async_func(expected_result)), create=True,
) as mock_obj:
result = await msg_methods.send_message(
entity=entity, message=message, file=media_file,
formatting_entities=formatting_entities,
)
mock_obj.assert_called_once_with(
entity, media_file, caption=message.message,
formatting_entities=formatting_entities or message.entities,
reply_to=None, silent=None, attributes=None, parse_mode=(),
force_document=False, thumb=None, buttons=None,
clear_draft=False, schedule=None, supports_streaming=False,
comment_to=None, background=None, nosound_video=None,
)
assert result == expected_result