Telethon is more than just another viable alternative when developing botsfor Telegram. If you haven’t decided which wrapper library for bots to useyet, using Telethon from the beginning may save you some headaches later.
Contents
- HTTP Bot API vs MTProto
- Migrating from Bot API to Telethon
The Telegram Bot API, also known as HTTP Bot API and from now on referredto as simply “Bot API” is Telegram’s official way for developers to controltheir own Telegram bots. Quoting their main page: Tap forms 5 organizer secure database 5 1 1.
The Bot API is an HTTP-based interface created for developers keen onbuilding bots for Telegram.
To learn how to create and set up a bot, please consult ourIntroduction to Bots and Bot FAQ.
Bot API is simply an HTTP endpoint which translates your requests to it intoMTProto calls through tdlib, their bot backend.
MTProto is Telegram’s own protocol to communicate with their API when youconnect to their servers.
The purpose of this site is to provide free MtProto proxy services. Links in first and last columns can be used only if you have Telegram client installed. Telegram MTProto Proxy (Alpine) The Telegram Messenger MTProto proxy is a zero-configuration container that automatically sets up a proxy server that speaks Telegram's native MTProto. Quick reference. To start the proxy all you need to do is docker run -d -p443:443 -name=mtproxy -restart=always -v./config:/data alexdoesh/mtproxy:latest. As of version 4.6, major Telegram clients are using MTProto 2.0. MTProto v.1.0 is deprecated and is currently being phased out. This article describes the basic layer of the MTProto protocol version 2.0 (Cloud chats, server-client encryption). The principal differences from version 1.0 (described here for reference) are as follows.
Telethon is an alternative MTProto-based backend written entirely in Pythonand much easier to setup and use. Everweb 2 6 – drag and drop website builder reviews.
Both official applications and third-party clients (like your ownapplications) logged in as either user or bots can use MTProto tocommunicate directly with Telegram’s API (which is not the HTTP bot API).
When we talk about MTProto, we often mean “MTProto-based clients”.
MTProto clients (like Telethon) connect directly to Telegram’s servers,which means there is no HTTP connection, no “polling” or “web hooks”. Thismeans less overhead, since the protocol used between you and the serveris much more compact than HTTP requests with responses in wasteful JSON.
Since there is a direct connection to Telegram’s servers, even if theirBot API endpoint is down, you can still have connection to Telegram directly.
Using a MTProto client, you are also not limited to the public API thatthey expose, and instead, you have full control of what your bot can do.Telethon offers you all the power with often much easier usage than anyof the available Python Bot API wrappers.
If your application ever needs user features because bots cannot do certainthings, you will be able to easily login as a user and even keep your botwithout having to learn a new library.
If less overhead and full control didn’t convince you to use Telethon yet,check out the wiki page MTProto vs HTTP Bot API with a more exhaustiveand up-to-date list of differences.
Mtproto Free
It doesn’t matter if you wrote your bot with requests and you weremaking API requests manually, or if you used a wrapper library likepython-telegram-bot or pyTelegramBotAPI. It’s never too late tomigrate to Telethon!
If you were using an asynchronous library like aiohttp or a wrapper likeaiogram or dumbot, it will be even easier, because Telethon is also anasynchronous library.
Next, we will see some examples from the most popular libraries.
Let’s take their echobot2.py example and shorten it a bit:
After using Telethon:
Telegram Mtproto List
Key differences:
- The recommended way to do it imports less things.
- All handlers trigger by default, so we need
events.StopPropagation
. - Adding handlers, responding and running is a lot less verbose.
- Telethon needs
asyncdef
andawait
. - The
bot
isn’t hidden away byUpdater
orDispatcher
.
Let’s show another echobot from their README:
Now we rewrite it to use Telethon:
Key differences:
- Instead of doing
bot.reply_to(message)
, we can doevent.reply
.Note that theevent
behaves just like theirmessage
. - Telethon also supports
func=lambdam:True
, but it’s not necessary.
From their GitHub:
After rewrite:
Key differences: Fetch 518 idaho schedule listings 2011.
- Telethon offers convenience methods to avoid retyping
bot.send_photo(message.chat.id,..)
all the time,and instead let you typeevent.reply
. - Sending files is a lot easier. The methods for sendingphotos, documents, audios, etc. are all the same!
Showcasing their subclassing example:
After rewriting:
Key differences:
Mtproto Telegram Obits
- Telethon method names are
snake_case
. - dumbot does not offer friendly methods like
update.reply
. - Telethon does not have an implicit
on_update
handler, sowe need to manually register one.