The story of how the parser turned into a full-fledged MRCO bot for Telegram

    Greetings, Khabrovsk! Recently I wrote an article about how to write a parser for an MRCO diary, and in the end I promised to write about integration with the Telegram bot,what I really regret. Now the bot is ready and fully functional. I want to tell you what I used and what difficulties I encountered in this work.


    Python and Telegram


    To create my bot (@mrkorobot), I decided to use the pyTelegramBotAPI library . It is easy to install, convenient to use, and they even wrote a good book for it . In the bot, I use the following libraries:



    To access the diary, you need a username and password. The first step is to request them from the user and put them in the database. Once the username and password are needed every time a user accesses the bot, it was necessary to choose a fast database. My choice fell on Redis , and for working with it in Python it was decided to use the library of the same name . Having previously encrypted the password in md5 using the hashlib library built into Python , I put them in the database under the key, the name of which is just the user id in the Telegram. Here is a visual representation:


    {
      "id_пользователя": ["логин", "пароль_в_MD5"]
    }

    When using such a scheme, you can immediately check if there is a cell with such an id, then the user is authorized, if not, no.


    The result was a bot that can show class schedules by day, homework, grades on subjects and even attached files, if any.


    Daily notifications


    In principle, at the moment, the implementation of viewing homework is ending, but I wanted to go even further and make daily notifications about the grades. After thinking, I managed to write a simple script that I added to cron, generating an expression here . The logic is painfully simple: first we get a list of all authorized users (who provided the bot with a username and password) and check if the user has turned on daily notifications, if so, we send ratings in this format:


    Diary Bot


    Checking your diary has never been easier!


    Private messages


    I also wanted to implement the function of viewing private messages. "Why not?" - I thought and wrote a method that allows you to view incoming messages. Nothing complicated, surprisingly, was not here. It was just necessary to contact not mrko.mos.ru/dnevnik/services/dnevnik.php?r=1&first=1 , but to mrko.mos.ru/dnevnik/services/mail.php?r=0 . Then the usual parsing and now everything is ready:


    Diary Bot


    Diary Bot


    By the way, there were problems with hosting: the server mrko.mos.ru was unavailable from the server on the Google Cloud Platform for some reason I did not , so I had to place the bot with a friend for a while.


    It is very important to me that you have tried Dnevnik.Bota
    (@mrkorobot) in the work and given their views. All ideas are considered very carefully. Happy programming!


    Also popular now: