LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
10.01k stars 1.41k forks source link

Issue with channels.getForumTopics API Method for Bot Accounts #4160

Closed osqenawi closed 1 year ago

osqenawi commented 1 year ago

Code that causes the issue

from telethon.sync import TelegramClient
from telethon.sessions import StringSession
from telethon.tl.types import PeerChannel
from telethon.tl.functions.channels import GetForumTopicsRequest
from datetime import datetime

api_id = 'API ID'
api_hash = 'API HASH'
bot_session = 'Bot Session'

bot_client = TelegramClient(StringSession(bot_session), api_id, api_hash)
with bot_client:
    # It Works (The input_entity of my Forum Group).
    input_peer_channel = bot_client.get_input_entity(PeerChannel(1892928277))

    #------------ Code That Produces The Error ------------# 
    forum_topics = bot_client(GetForumTopicsRequest(channel=input_peer_channel,
                                                    offset_date=datetime(2023, 7, 30),
                                                    offset_id=-1, offset_topic=-1, limit=5))

Expected behavior

The channels.getForumTopics method should work for bot accounts with the appropriate permissions, as stated in the documentation.

Actual behavior

The method returns an error when used with a bot account.

Traceback

Traceback (most recent call last):
  File "/Users/osamagamal/Desktop/test_bot_forums.py", line 17, in <module>
    forum_topics = bot_client(GetForumTopicsRequest(channel=input_peer_channel,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/telethon/sync.py", line 39, in syncified
    return loop.run_until_complete(coro)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/telethon/client/users.py", line 30, in __call__
    return await self._call(self._sender, request, ordered=ordered)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/telethon/client/users.py", line 87, in _call
    result = await future
telethon.errors.rpcerrorlist.BotMethodInvalidError: The API access for bot users is restricted. The method you tried to invoke cannot be executed as a bot (caused by GetForumTopicsRequest)

Telethon version

1.29.2

Python version

3.9.7

Operating system (including distribution name and version)

macOS 13.4.1

Other details

When using the channels.getForumTopics API method with a bot account, I encountered the following error: telethon.errors.rpcerrorlist.BotMethodInvalidError: The API access for bot users is restricted. The method you tried to invoke cannot be executed as a bot (caused by GetForumTopicsRequest)

The official Telethon documentation claims that the channels.getForumTopics method is accessible to both users and bots. However, in my testing, I found that the method works as expected for user accounts but fails with the aforementioned error when used with bot accounts.

Checklist

Lonami commented 1 year ago

The documentation says:

Both users and bots may be able to use this request

Not:

Both users and bots are able to use this request

This line in the raw API page is manually regenerated by me every now and then, and I haven't done so for newer requests yet. So the default is "maybe it works, maybe not".

osqenawi commented 1 year ago

I wonder why the official Telegram Docs says:

Bots can use this method

https://core.telegram.org/method/channels.getForumTopics

Lonami commented 1 year ago

You are using raw API, which means the results you are getting come directly from Telegram, and Telethon is not changing them in any way (other than adapting some input parameters where needed, all while respecting the intended values). Unfortunately this means it's something Telethon cannot "fix" and it's just the way Telegram's API works.

Their documentation could have a mistake.