'Telethon Telegram API - Resolving invite link WITHOUT invoking ResolveUsernameRequest
I am currently building a software suite which takes in a few thousand telegram channels via their links (i.e. https://t.me/telegram_channel) and scrapes messages from them in order to do sentiment analysis. Everything works fine with the Telegram API and Telethon, however there is one problem I keep running into. In order to resolve the channel ID and user hash for each of these telegram channels, I need to run:
client.get_entity("https://t.me/telegram_channel")
However, this VERY quickly triggers FloodWaitErrors from the Telegram API, presumably because it thinks I may be attempting to resolve Telegram user's usernames in order to spam them.
That's not the case though. I am ONLY resolving public channels, not users or chats. I've searched for hours through the Telegram docs, and have not found a way to resolve a channel link WITHOUT invoking ResolveUsernameRequest and thus triggering a FloodWaitError. Is this possible to do without causing this rate limit?
Thanks!
Solution 1:[1]
This is because your own created API_ID and API_HASH are not official. It will run into FloodWaitErrors if you perform too many requests.
The easy way to solve this problem is by using an Official API.
Solution 1: Using tdesktop's API:
- The official Telegram Desktop app is using this API below:
API_ID = 2040 API_HASH = "b18441a1ff607e10a989891a5462e627" - Use it with Telethon:
client = TelegramClient("session", 2040, "b18441a1ff607e10a989891a5462e627")
However, there is a catch, as you can see in the initConnection request of Telegram API, there are more than just API_ID and API_HASH, it also needs other parameters such as device_model or lang_pack. Telethon does provide you a way to set these parameters via TelegramClient.__init__, but it doesn't let you set the lang_pack parameters because it's for official apps only.
So that brings us to Solution 2.
Solution 2: Using a wrapper library of Telethon which provides official APIs.
- There is a library called opentele that provides a neat way to use official APIs, including the API of Desktop, Android, iOS, macOS,... that currently being used by official apps.
- First you need to install it from PyPi:
pip install opentele
- Then run the example code below:
from opentele.tl import TelegramClient from opentele.api import API import asyncio async def main(): api = API.TelegramDesktop client = TelegramClient("telethon.session", api=api) await client.connect() asyncio.run(main()) - After that, you can use the
clientjust like using it with Telethon.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | professor |
