Skip to content

Commit 3a1d25c

Browse files
committed
Added caption extraction when send a msg with a file (#4499)
Closes #4499
1 parent 3f589b2 commit 3a1d25c

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

telethon/client/messages.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,8 @@ async def callback(event):
826826
await client.send_message(chat, 'Hi, future!', schedule=timedelta(minutes=5))
827827
"""
828828
if file is not None:
829+
if isinstance(message, types.Message):
830+
message = message.message
829831
return await self.send_file(
830832
entity, file, caption=message, reply_to=reply_to,
831833
attributes=attributes, parse_mode=parse_mode,

tests/telethon/client/test_messages.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import inspect
2+
from unittest import mock
3+
from unittest.mock import MagicMock
24

35
import pytest
46

57
from telethon import TelegramClient
8+
from telethon.client import MessageMethods
9+
from telethon.tl.types import PeerChat, MessageMediaDocument, Message
610

711

812
@pytest.mark.asyncio
@@ -38,3 +42,36 @@ async def send_file(self, entity, file, **kwargs):
3842

3943
client = MockedClient()
4044
assert (await client.send_message('a', file='b', **arguments)) == sentinel
45+
46+
47+
class TestMessageMethods:
48+
@pytest.mark.asyncio
49+
async def test_send_msg_and_file(self):
50+
async def async_func(result): # AsyncMock was added only in 3.8
51+
return result
52+
msg_methods = MessageMethods()
53+
expected_result = Message(
54+
id=0, peer_id=PeerChat(chat_id=0), message='', date=None,
55+
)
56+
entity = 'test_entity'
57+
message = Message(
58+
id=1, peer_id=PeerChat(chat_id=0), message='expected_caption', date=None,
59+
)
60+
media_file = MessageMediaDocument()
61+
62+
with mock.patch.object(
63+
target=MessageMethods, attribute='send_file',
64+
new=MagicMock(return_value=async_func(expected_result)), create=True,
65+
) as mock_obj:
66+
result = await msg_methods.send_message(
67+
entity=entity, message=message, file=media_file,
68+
)
69+
mock_obj.assert_called_once_with(
70+
entity, media_file, caption=message.message,
71+
reply_to=None, attributes=None, parse_mode=(),
72+
force_document=False, thumb=None, buttons=None,
73+
clear_draft=False, silent=None, schedule=None,
74+
supports_streaming=False, formatting_entities=None,
75+
comment_to=None, background=None, nosound_video=None,
76+
)
77+
assert result == expected_result

0 commit comments

Comments
 (0)