How A Userbot Supercharges Your Telegram Bot

How A Userbot Supercharges Your Telegram Bot


A userbot is a type of bot that doesn't use the Telegram Bot API, but rather takes over a regular Telegram user account and interacts in its name. Consequently, it has no dedicated "Start" functionality or preset commands to choose from. However, when added to a group or channel as an admin, a userbot has all the abilities that normal Telegram users have, like uploading big files or performing user lookups, but executed programmatically. Userbots are normal Telegram clients that speak the MTProto protocol, like the one you are currently using to view this Telegraph post (hopefully).

If you're missing features from the Bot API or feel restricted by its limitations, the implementation of a custom userbot backend can extend your regular bot's power to the maximum. You can use this backend to circumvent various problems like the ones described in this article, but the applications are endless.

MTProto Backend

*But not all. Which will allow us to do nasty stuff.

Usually, you'll want to use a regular bot as your "frontend", the interface that your users see, to be able to set commands, accept inlinequeries and to prevent scaring people off who feel threatened by interacting with a Telegram user that is actually a bot. However, what magic your bot does under the hood is completely up to you.

Start off by picking an appropriate Telegram client implementation that best suits your programming environment: Preferably, you will want a library that is written in the same language as your bot, so that you can instantiate its classes directly in your code base, without taking a detour over the HTTP protocol.

If you have reasons to rely on an implementation in another language, the MTProto client will have to run alongside your bot, requiring to implement for example an RPC client and server, or a simple REST endpoint in order to connect them.

The next step after installation and the means of connecting is figured out, is to build an abstraction around the functionality that is important to you. An object-oriented approach where one class holds the methods that your bot is going to use (including setup routines) ensures separation of concerns.

User lookup

The Bot API does not provide a method getAllChatMembers and similar utilities, making this a perfect opportunity to add your Userbot to a group and make it call the MTProto method that gives you the members of that chat. To keep with the spirit of a dedicated backend, let's pick a good architecture for these functions. Your backend is going to provide a method getAllChatMembers(chat_id) that can be used by the regular bot after initialization of the backend class. This method then makes an MTProto GetFullChatRequest or GetFullChannelRequest based of the supplied chat_id, which yields a ChatFull object, containing Users of that chat.

As you can see, MTProto is not always straightforward. But after some time you're going to get a feel for it and of course you can always ask questions in the respective Telegram groups of the library you're using.

In the same fashion, you can also perform global username lookups to check if one is available (which is just one method and fairly easy).

File size limitations

To pick an example, let's assume you're building a YouTube Downloader Bot. When a user selects a video to download, you're going to utilize some CLI to download the video to local storage, and probably ask whether the user wants to convert it to another format or change its resolution. When this is done, you're gonna need to check if the resulting file size is within the limit that the Bot API allows (max. 50MB for upload at the time of writing). If this is the case, you send the file using the sendVideo method - business as usual. But if it exceeds the limits, you can exploit the user API to circumvent this restriction:

An MTProto client can send files of up to 2GB, regardless whether logged in as a normal user or into a bot account. I assume that the Telegram team decided to handle the limits like this in order to take load off their Bot API servers, while allowing standalone clients to upload bigger files.

Creating Groups and Channels

If you're building a community, this can really help. The ideas is that a userbot can create transient groups with a short lifetime — temporary chat rooms for specific threads of a conversation. So if some people want to talk about an important topic uninterrupted by the spam in a public megagroup, they could request their own private chat which is created by the bot, adding everyone who types /join in the public supergroup. This would also be great for (customer) support.

How to get more user accounts

You need phone numbers where you have access to the SMS. I have seen people stumble upon lists on the internet (mainly russian sites), but I can't exactly tell you where to look.

Is this illegal?

Pavel Durov and his team are very open to things built by the community. Also, you can only utilize a userbot backend if you have full access to the respective group. There's nothing you couldn't do manually if you had enough time, userbots just automate many problems and enable scaling of your solution.

Interacting with other bots (the holy grail)

Congratulations! You made it here. I've got one more tidbit that will probably blow your mind, as it has the ones of many before you.

This is a bold claim, but it is grounded.
Compare how, in a Linux environment, the different programs and utilities can be piped together in the terminal, facilitating their respective strengths and achieving results that wouldn't be possible with a single piece of software on its own. The same level of "intelligence" can be reached when bots are able capable of talking to each other in a meaningful way (not like this).

Telegram is actually the best possible platform for this, because bots are held in an IRC-like interface, seldomly relying on natural language inputs, but instead offering very structured interfaces with buttons and /commands most of the time. Computers can work with that a lot better, at least for as long as NLP isn't capable of understanding whatever you throw at it.

Bots are usually operating on a very narrow spectrum of use-cases, like only getting you the weather, or overturning your parking ticket. From what we've learned since the bot revolution began, having a too broad range of services bites you in the tail, both from a standpoint of NLU complexity and user expectations.
However, intelligent agents in the sense of what Alexa, Siri, Assistant or Cortana do, can act as a bridge between the different services provided by bots, leveraging natural language understanding only to route the user between interactions with different bots. To carry it to the extremes, when your Youtube Download Bot is asked for the weather, why shouldn't it admit that it's stumped for an answer and ask Poncho instead?

I'm absolutely positive bot interconnectivity is going to be a thing eventually, as it's the holy grail of the bot era.

List of MTProto Client Libraries

Not all libraries provide convenience wrappers around the Telegram API functionality, some only expose an invocation mechanism where you need to fill the gaps to reach the right Telegram endpoint. Following are the most notable libraries and frameworks I know of:

  • Telegram-CLI
    Ceased further development. Many will tell you to avoid it, but basic things still work. Not complete.
  • TDLib (C++, Java, C#)
    This library can be considered as the "official" MTProto implementation, as the Bot API, Telegram X, and other clients are built upon it. If you are willing to work with C++, this should be your first choice, but it also has code bindings to other languages (notably Java using JNI and C# using C++/CLI and C++/CX). If your favorite language is capable of executing C functions, it will, in theory, work with TDLib.
  • Telethon (Python3)
    A steadily growing framework including a TL-parser that generates python classes for the MTProto types and methods, therefore mostly complete (missing calls). A new event handling system makes it easy to react to incoming updates.
  • Pyrogram (Python3)
    "The new kid on the block". Like Telethon, this library has a TL object generator and about the same feature variety (also no calls). Pyrogram aims to be as close to the Bot API as possible.
  • MadelineProto (PHP)
    An advanced library with a lot of sugar and even a web API. Under active development, complete (even phone calls, go and call Magnaluna).
  • Telegram JS (JavaScript)
  • TLSharp (C#)
  • GramJS (JavaScript)

Report Page