Skip to content

Commit 308dae3

Browse files
author
Pyrogram-Mod - Dev
committed
fix(communities): resolve community IDs via joined-communities lookup
Community IDs use a separate ID space (no -100 prefix) and are not cached by resolve_peer, causing COMMUNITY_ID_INVALID errors. Build the InputChannel from the joined-communities list instead. Document the ID format in the peer-id FAQ.
1 parent af22a80 commit 308dae3

2 files changed

Lines changed: 42 additions & 7 deletions

File tree

docs/source/faq/peer-id-invalid-error.rst

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,18 @@ This error could mean several things:
1111
About the last point: in order for you to meet a user and thus communicate with them, you should ask yourself how to
1212
contact people using official apps. The answer is the same for Pyrogram too and involves normal usages such as searching
1313
for usernames, meeting them in a common group, having their phone contacts saved, getting a message mentioning them
14-
or obtaining the dialogs list.
14+
or obtaining the dialogs list.
15+
16+
Community IDs
17+
-------------
18+
19+
Communities (introduced in layer 228) use a **different ID space** from regular channels and supergroups.
20+
Their IDs are raw integers (e.g. ``1095605184288``) and are **not** prefixed with ``-100``.
21+
You can obtain a community ID from:
22+
23+
- The ``linked_community_id`` field on :obj:`~pyrogram.types.Chat` — populated when a channel belongs to a community.
24+
- :meth:`~pyrogram.Client.get_joined_communities` — returns all communities you are a member of, each with its raw ID and ``access_hash``.
25+
26+
Community IDs cannot be passed directly to methods that call ``resolve_peer`` internally (such as
27+
:meth:`~pyrogram.Client.get_chat`). Methods in the ``communities.*`` namespace (e.g.
28+
:meth:`~pyrogram.Client.add_chat_to_community`) handle this automatically.

pyrogram/methods/communities/add_chat_to_community.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ async def add_chat_to_community(
3434
.. include:: /_includes/usable-by/users.rst
3535
3636
Parameters:
37-
community_id (``int`` | ``str``):
38-
The community channel to add the chat to.
37+
community_id (``int``):
38+
The raw community ID (as returned by ``linked_community_id`` on
39+
:obj:`~pyrogram.types.Chat` or by :meth:`~pyrogram.Client.get_joined_communities`).
40+
Note: this is **not** the ``-100``-prefixed channel ID.
3941
4042
chat_id (``int`` | ``str``):
41-
The chat to add.
43+
The chat or peer to add to the community.
4244
4345
hidden (``bool``, *optional*):
4446
Pass True to add the chat as hidden (not visible in the community sidebar).
@@ -50,14 +52,33 @@ async def add_chat_to_community(
5052
Example:
5153
.. code-block:: python
5254
55+
# Get the community ID from a linked chat
56+
chat = await app.get_chat(my_channel_id)
57+
community_id = chat.linked_community_id
58+
5359
# Add visible
54-
await app.add_chat_to_community(-1001966997491, chat_id)
60+
await app.add_chat_to_community(community_id, chat_id)
5561
5662
# Add hidden
57-
await app.add_chat_to_community(-1001966997491, chat_id, hidden=True)
63+
await app.add_chat_to_community(community_id, chat_id, hidden=True)
5864
"""
5965

60-
community = await self.resolve_peer(community_id)
66+
# Community objects have raw IDs (no -100 prefix) and are not in the
67+
# peer cache, so resolve_peer can't handle them. Fetch the access_hash
68+
# from the joined-communities list instead.
69+
if isinstance(community_id, int):
70+
r = await self.invoke(raw.functions.communities.GetJoinedCommunities())
71+
target_id = abs(community_id)
72+
match = next((c for c in r.chats if c.id == target_id), None)
73+
if match is None:
74+
raise ValueError(f"Community {community_id} not found in joined communities")
75+
community = raw.types.InputChannel(
76+
channel_id=match.id,
77+
access_hash=match.access_hash,
78+
)
79+
else:
80+
community = await self.resolve_peer(community_id)
81+
6182
peer = await self.resolve_peer(chat_id)
6283

6384
await self.invoke(

0 commit comments

Comments
 (0)