📜 ⬆️ ⬇️

Stepic.org in Telegram: how we developed the bot and what came of it

In the winter of 2016, we in Stepic.org had the idea to make a personal assistant for our students, so we called Konstantin Charkin, a student of SPbAU RAS, for an internship, which resulted in a Telegram bot. Then it all went out of control and a set of stickers and channels for courses appeared ... But first things first!



Stepic bot


Language and library selection

Python was chosen as the programming language, since Stepic’s backend is written in Django and the easiest way to integrate the bot into the overall infrastructure is to write Django Application.
Of the many wrapper libraries over the Telegram API, the eternnoir / pyTelegramBotAPI was chosen. Among the options were three more: datamachine / twx.botapi , nickoala / telepot , sourcesimian / txTelegramBot . The choice was due to the fact that, firstly, it was actively supported, and secondly, according to it there is a good guide in Russian for the smallest. By the way, the author of this textbook has created a group in the VC, where he answers any questions related to Telegram bots, completely free of charge and fairly quickly.

Linking account

In order for the bot to perform some useful actions, it is necessary to link the account on Stepic with the account in the Telegram. For this, a mechanism called Deep Linking is used. You can read about it either in the Telegram API documentation , or here (the second link is clearer :).
')
In our case, the mechanism is as follows:

It is also worth considering that people take courses mainly from a PC, and use Telegram on mobile devices. Therefore, asking them if they found a bot not through the site Stepic.org, to go there and click on this link is a bad idea, because it is likely that they are not logged on this device, and the password has long been forgotten. Therefore, there is a second option linking an account - via email. A bot, if an unfamiliar user wrote to him, asks to enter his email, which was used during registration, and if such email is found, then a unique link is sent there, the user enters his email client, clicks on this link and everything is ready.

Useful actions

After we have learned how to link an account, you need to decide what the bot itself will be able to do. There were many ideas about this, but, for a start, it was decided to dwell on the functions:

Design

The simplest way to model the behavior of chat bots is a finite state machine .
In this case, it looks like this:



Red states are states in which a bot expects some input from the user.
I is the initial state in which the bot expects one of the following commands:

II - is the choice of registration method (via website or email);
III - waiting for the user to enter email;
IV is the state in which the bot expects one of the following commands:

V is a search, the bot is waiting for an input request.

Actually, the meaning of all commands is quite obvious, then a few examples of use.

The communication between students is implemented in the simplest way - a chat is created for the course and, at the user's request, it receives a list of chat courses that are divided into 2 categories depending on whether the user is enrolled in the course.

image

Here, by the way, a new feature from botAPI 2.0 was used, namely the built-in keyboard and message editing. It would be possible to display all the chats at once, but there can be quite a lot of chats on courses for which the user is not recorded, and if the user wants to see several screens, then let it do it consciously.

Search courses:

image

Account deleting:

image

Choosing a registration method:

image

An example of the output of the / deadlines command:



Stepic.org in Telegram:


Bots can be found at: telegram.me/stepicbot .

Or you can immediately go to linking your account on Stepic.org with your Telegram account like this: stepic.org/telegram .

We will be happy to answer questions, constructive criticism is also welcome!

And for a sweet set of Stepic-stickers: telegram.me/addstickers/Stepic

Source: https://habr.com/ru/post/303478/


All Articles