LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!

Home Page:https://docs.telethon.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Telegram generates non int api_id

YuriyTigiev opened this issue · comments

Code that causes the issue

clients[channel] = await create_telegram_client(channel, env_config['api_id'], env_config['api_hash'])
await clients[channel].connect()  <--  

async def create_telegram_client(session_name, api_id, api_hash):
    session_directory = 'sessions'
    session_file_path = os.path.join(session_directory, session_name)
    if not os.path.exists(session_directory):
        os.makedirs(session_directory)

    return TelegramClient(f"{session_file_path}", api_id=api_id, api_hash=api_hash)

Expected behavior

The code worked properly. I have just created a new bot and got an error.

You can use this token to access HTTP API:
6718325900:API_HASGH

For a description of the Bot API, see this page: https://core.telegram.org/bots/api

Actual behavior

I'm getting the error after the calling connection()

Traceback

2024-05-05 21:51:33 - ERROR - Request caused struct.error: 'i' format requires -2147483648 <= number <= 2147483647: InvokeWithLayerRequest(layer=173, query=InitConnectionRequest(api_id=6718325900, device_model='PC 64bit', system_version='11', app_version='1.34.0', system_lang_code='en', lang_pack='', lang_code='en', query=GetConfigRequest(), proxy=None, params=None))

Telethon version

1.34.0

Python version

3.12.3

Operating system (including distribution name and version)

windows 11

Other details

No response

Checklist

  • The error is in the library's code, and not in my own.
  • I have searched for this issue before posting it and there isn't an open duplicate.
  • I ran pip install -U https://github.com/LonamiWebs/Telethon/archive/v1.zip and triggered the bug in the latest version.

Looks like that telegram have started used int64 for api_id

I have installed 1.35.1 and gotten the same error.

The bot token is not the API ID. The two concepts are separate. To run a bot with Telethon, you need both BotFather's token and the developer's API ID from https://my.telegram.org.

I know what is bot father and how to use it. Everything have worked with short api_id. The same code doesn't work with new api_id = 6718325900, because it's greater then range of integer number. Try to generate a new bot and use generated api_id

I don't quite understand. The token returned by BotFather are the ID and the hash separated by a semicolon, right? So splitting those two components and adding them to the corresponding variables should work. Or do I need an entirely different ID from somewhere else?

The token returned by BotFather are the ID and the hash separated by a semicolon, right?

No. As I've stated before:

The bot token is not the API ID. The two concepts are separate. To run a bot with Telethon, you need both BotFather's token and the developer's API ID from https://my.telegram.org/.

This is also documented. I admit the "Signing In as a Bot Account" section might be misleading, but the v2 documentation for this corrects it:

.. note::

    The bot token obtained from `@BotFather <https://t.me/BotFather>`_ looks something like this::

        54321:hJrIQtVBab0M2Yqg4HL1K-EubfY_v2fEVR

    This is **not** the API ID and hash separated by a colon!
    All of it is the bot token.
    Using a bot with Telethon still requires a separate API ID and hash.

Gotcha! This is a lot clearer, thanks!