Created MTProto vs Bot API (markdown)

Lonami 2020-02-11 16:21:38 +01:00
parent fbda74114b
commit a3a64c66ba

177
MTProto-vs-Bot-API.md Normal file

@ -0,0 +1,177 @@
HTTP Bot API vs MTProto comparison
==================================
This page aims to show that
[MTProto](https://core.telegram.org/mtproto) is superior to the [HTTP
Bot API](https://core.telegram.org/bots/api) (from now on referred to as
"botapi") when developing bots for [Telegram](https://telegram.org/).
> This page is in its early stages and needs a lot more work before
> being complete, so it's subject to major refactoring. If you have more
> information, please edit the page!
MTProto Advantages
==================
Multiple Instances
------------------
**Status**: Needs testing.
When you try to run multiple instances of a botapi bot, the old one will
no longer receive updates from the server. MTProto lets you have
multiple sessions active at the same time.
Get Messages by ID
------------------
**Status**: Tested manually.
The botapi doesn't let you get any message by its ID. Instead, this is
handled for you so that things like reply messages can always be
provided.
In MTProto, bots can call
[messages.getMessages](https://lonamiwebs.github.io/Telethon/methods/messages/get_messages.html)
or
[channels.getMessages](https://lonamiwebs.github.io/Telethon/methods/channels/get_messages.html)
to get any message by its ID.
Full Control of Entity Access Hashes
------------------------------------
**Status**: Needs testing.
Usually in Telegram, you need an entity ID and an access hash to be able
to do something with that entity (like send a message to a user).
The botapi stores the access hashes for a limited amount of time, after
which it can no longer implicitly provide the access hash to you, so
calls such as [getChat](https://core.telegram.org/bots/api#getchat) will
fail after your bot no longer has contact with the user for a while.
With MTProto you have full control over the access hashes, which allows
you to perform tasks with the entity without relying on the server to
store your access hashes.
Resolve anything by username
----------------------------
**Status**: Tested manually.
The botapi has implicit resolving of *channel* usernames when making
requests like
[sendMessage](https://core.telegram.org/bots/api#sendmessage).
In MTProto bots can use
[contacts.resolveUsername](https://lonamiwebs.github.io/Telethon/methods/contacts/resolve_username.html)
to resolve *anything* by username.
Bulk Delete and Forward
-----------------------
**Status**: Tested manually.
The botapi lets you delete a single message with
[deleteMessage](https://core.telegram.org/bots/api#deletemessage) or
[forwardMessage](https://core.telegram.org/bots/api#forwardmessage).
MTProto lets you bulk delete or forward up to 100 messages at a time
([messages.deleteMessages](https://lonamiwebs.github.io/Telethon/methods/messages/delete_messages.html)/[channels.deleteMessages](https://lonamiwebs.github.io/Telethon/methods/channels/delete_messages.html)
and
[messages.forwardMessages](https://lonamiwebs.github.io/Telethon/methods/messages/forward_messages.html)).
Get participants of group
-------------------------
**Status**: Tested manually.
The botapi only allows getting a subset of the participants in a group,
with
[getChatAdministrators](https://core.telegram.org/bots/api#getchatadministrators)
to get administrators.
With MTProto, bots can use
[channels.getParticipants](https://lonamiwebs.github.io/Telethon/methods/channels/get_participants.html)
with plenty of different filters, which means you can fetch not only
administrators but also banned people, kicked members, etc.
Byte Types
----------
**Status**: Tested manually.
The botapi uses JSON to serialize data. JSON does not have a byte type,
only strings. This means that fields like `callback_data` in a
[InlineKeyboardButton](https://core.telegram.org/bots/api#inlinekeyboardbutton)
can't accept arbitrary bytes like the description would have you
believe.
In MTProto you can use arbitrary bytes in this field:
[KeyboardButtonCallback](https://lonamiwebs.github.io/Telethon/constructors/keyboard_button_callback.html).
Full Control over Text Entities
-------------------------------
**Status**: Tested manually.
The botapi only lets you use either HTML or Markup for adding text
entities to your messages (eg: bold, links, etc).
With MTProto, entities are an entirely optional field in calls like
[messages.SendMessageRequest](https://lonamiwebs.github.io/Telethon/methods/messages/send_message.htmL).
This allows you to easily do things like [making the entire message
monospace](https://github.com/udf/uniborg/blob/8290974235182dccab384f8fc5be653987c1b8c5/stdplugins/info.py#L15-L20)
without having to worry about escaping the text.
See More Fields
---------------
**Status**: Needs testing.
An example of this is when a user sends a message using an inline bot,
the botapi does not provide the via\_bot\_id field of the message
object, so you cannot tell which bot was used to send the message.
Faster Updates
--------------
**Status**: Needs testing.
The botapi will inevitably need more work to be up and running after an
update, while updating a MTProto based library can be done as soon as a
layer change is published. Even if the botapi gets this change first,
MTProto libraries can grab their layer. This is especially important in
some cases:
> For all the adminbot devs:
>
> Bots can not see messages with animated stickers but clients already
> show them or show the "This message is not supported...." message.
> Which means groups can be spammed.
Anecdotal Advantages
====================
Things that MTProto might do better than the botapi, but which we have
no proof for.
Less Overhead
-------------
Since the botapi servers use MTProto under the hood (through
[TDLib](https://core.telegram.org/tdlib)), using MTProto directly would
give you less overhead.
This doesn't really mean much unless the botapi server goes down or
starts being slow. In that case, calls like
[answerCallbackQuery](https://core.telegram.org/bots/api#answercallbackquery)
can fail because you have a limited time to respond to it.
Update Comparison
=================
The botapi does not send certain updates to your bot, for example
messages being deleted.
(TODO: Add table comparing the updates from MTProto and botapi)