Skip to content

Commit 795fafb

Browse files
feat: Add support for getting sponsored messages
Adds a new `get_sponsored_messages` method to the `Client` to fetch sponsored messages from a channel. This method uses the `messages.getSponsoredMessages` raw API function to retrieve the sponsored messages. A new `SponsoredMessage` type is also added to represent the sponsored message data in a user-friendly way.
1 parent 9a7dea0 commit 795fafb

File tree

5 files changed

+108
-1
lines changed

5 files changed

+108
-1
lines changed

pyrogram/methods/messages/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from .get_messages import GetMessages
4646
from .get_message_read_participants import GetMessageReadParticipants
4747
from .get_scheduled_messages import GetScheduledMessages
48+
from .get_sponsored_messages import GetSponsoredMessages
4849
from .read_chat_history import ReadChatHistory
4950
from .retract_vote import RetractVote
5051
from .search_global import SearchGlobal
@@ -97,6 +98,7 @@ class Messages(
9798
GetMessages,
9899
GetMessageReadParticipants,
99100
GetScheduledMessages,
101+
GetSponsoredMessages,
100102
SetTodoTasksCompletion,
101103
SendAudio,
102104
SendChatAction,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
from typing import Union, Optional, List
3+
4+
import pyrogram
5+
from pyrogram import raw, types
6+
7+
8+
class GetSponsoredMessages:
9+
async def get_sponsored_messages(
10+
self: "pyrogram.Client",
11+
chat_id: Union[int, str],
12+
) -> Optional[List["types.SponsoredMessage"]]:
13+
peer = await self.resolve_peer(chat_id)
14+
15+
if isinstance(peer, raw.types.InputPeerChannel):
16+
rpc = raw.functions.messages.GetSponsoredMessages(
17+
peer=peer
18+
)
19+
else:
20+
raise ValueError(f'The chat_id "{chat_id}" belongs to a user or chat')
21+
22+
result = await self.invoke(rpc, sleep_threshold=60)
23+
24+
sponsored_messages = []
25+
for message in result.messages:
26+
sponsored_messages.append(
27+
types.SponsoredMessage._parse(
28+
self,
29+
message,
30+
{user.id: user for user in result.users},
31+
{chat.id: chat for chat in result.chats}
32+
)
33+
)
34+
35+
return sponsored_messages

pyrogram/types/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030
from .user_and_chats import *
3131
from .payments import *
3232
from .pyromod import *
33+
from .sponsored_message import SponsoredMessage
3334

3435
__all__ = [
3536
"List",
3637
"Object",
37-
"Update"
38+
"Update",
39+
"SponsoredMessage"
3840
]
3941
__all__.extend(authorization.__all__)
4042
__all__.extend(bots_and_keyboards.__all__)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
from pyrogram import raw, types
3+
from pyrogram.types.object import Object
4+
from typing import List, Optional
5+
6+
class SponsoredMessage(Object):
7+
def __init__(
8+
self,
9+
*,
10+
url: str,
11+
title: str,
12+
message: str,
13+
entities: Optional[List["types.MessageEntity"]] = None,
14+
button_text: str
15+
):
16+
super().__init__()
17+
self.url = url
18+
self.title = title
19+
self.message = message
20+
self.entities = entities
21+
self.button_text = button_text
22+
23+
@staticmethod
24+
def _parse(client, sponsored_message: "raw.types.SponsoredMessage", users, chats) -> "SponsoredMessage":
25+
return SponsoredMessage(
26+
url=sponsored_message.url,
27+
title=sponsored_message.title,
28+
message=sponsored_message.message,
29+
entities=[types.MessageEntity._parse(client, entity, users) for entity in sponsored_message.entities] if sponsored_message.entities else None,
30+
button_text=sponsored_message.button_text
31+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
import pytest
3+
from unittest.mock import AsyncMock
4+
from pyrogram import Client
5+
from pyrogram import raw
6+
from pyrogram import types
7+
8+
@pytest.mark.asyncio
9+
async def test_get_sponsored_messages():
10+
client = Client(":memory:")
11+
client.resolve_peer = AsyncMock(
12+
return_value=raw.types.InputPeerChannel(channel_id=123, access_hash=456)
13+
)
14+
15+
mock_sponsored_message = raw.types.SponsoredMessage(
16+
random_id=b'123',
17+
url="https://example.com",
18+
title="Sponsored",
19+
message="This is a sponsored message",
20+
button_text="Click me",
21+
entities=[]
22+
)
23+
24+
mock_response = raw.types.messages.SponsoredMessages(
25+
messages=[mock_sponsored_message],
26+
chats=[],
27+
users=[]
28+
)
29+
30+
client.invoke = AsyncMock(return_value=mock_response)
31+
32+
sponsored_messages = await client.get_sponsored_messages(chat_id="test_channel")
33+
34+
assert len(sponsored_messages) == 1
35+
assert sponsored_messages[0].message == "This is a sponsored message"
36+
assert sponsored_messages[0].url == "https://example.com"
37+
assert sponsored_messages[0].title == "Sponsored"

0 commit comments

Comments
 (0)