Greetings, Habrovchane! Recently, I wrote an article about how to write the IDCO diary parser, and in the end I promised to write about integration with the Telegram bot, I am very sorry . Now the bot is ready and fully functioning. I want to tell you what I used and what difficulties I encountered in this work.
To create my bot (@mrkorobot), I decided to use the pyTelegramBotAPI library. It is easy to install, easy to use, and even a good book was written for it. In the bot, I use the following libraries:
To access the diary you need a login 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 the user accesses the bot, it was necessary to select a fast database. My choice fell on Redis , and to work with her 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 a turnkey database, whose name is simply the user id in Telegram. Here is a visual representation:
{ "id_": ["", "__MD5"] }
When using such a scheme, you can immediately check if a cell with such an id exists, then the user is authorized, if not - no.
The result is a bot that can show the schedule of lessons by day, homework, grade for subjects and even attached files, if any.
In principle, at the moment the implementation of homework viewing ends, but I wanted to go even further and make daily notifications of grades. Thinking, I managed to write a simple script that I added to cron, generating an expression here . The logic is extremely simple: first, we retrieve 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 yes - send estimates in this format:
The diary check has never been easier!
I also wanted to implement the function of viewing personal messages. "Why not?" - I thought and wrote a method that allows you to view incoming messages. Surprisingly, there was nothing difficult here. It was just not necessary to contact 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:
By the way, there were problems with hosting: from the server on the Google Cloud Platform the domain mrko.mos.ru was unavailable for some reason I did not understand , so I had to place a bot with a friend for a while.
It is very important for me that you try Diary. Bot
(@mrkorobot) in the work and gave their views. All ideas are considered very carefully. Happy programming!
Source: https://habr.com/ru/post/326582/
All Articles