DEV Community

Andrey
Andrey

Posted on

Telegram + node.js - bot

You can easily find tons of articles about creating bot for Telegram, including ones based on node.js. We at Tonfotos faced completely different task - we needed to build a Telegram client into our application.

Why Telegram client, not a bot?

Tonfotos is an application that takes care of personal or family photo archive. And as such, it has to take care not only about storing, indexing and viewing photos and videos, it also has to simplify photos sharing. And these days people do not share their photos on USB sticks any more. Majority of photos got shared using messengers, and Telegram is one of them. There is wide spread belief among users that Telegram does not over-compress photos, unlike other messenger app, such as Viber for example.

As a result, we have implemented Telegram connector feature into our application. Once user connects Tonfotos to his or her Telegram account, all images that are sent to user by friends got saved into image archive, fully automated.

Choosing the library

There are several requirements that pretty much narrow down a list of possible Telegram libraries:

  • Tonfotos is a cross-platform application using Electron that supports Windows, Linux and macOS. The library should work on all of them.
  • Application is not open-source, so no GPL.

Those requirements ruled out many implementations, including ones based on official TDLib library. So we ended up using telegram-mtproto library with MIT license. This is JS-native implementation of MT protocol. Thanks to that there is no native code dependencies, and impact on distribution size is negligible.

Downside is though, this library is not supported anymore and repository is not accepting PR's with fixes. So all bug fixes we had to keep as patches. This includes update of MTproto schema to up-to-date one, which is basically a replacement of schema file.

Architecture

The disadvantage of the chosen library is that it cannot deinitialize and free up all the used memory. Therefore we had to run it always in a separate process that we would spawn using child_process.fork, and killing this process when it is not needed anymore.

Top comments (0)