As I wrote the bot for school d / z and changed the database

Hello, Khabrovsk citizens!

Today I will try to tell you how a student can write a bot for storing homework for VK.

Caution
I’ll immediately draw attention to the fact that for serious projects it’s impossible to build infrastructure like this , this whole project was created only for entertainment and time killing, and for presentations at all kinds of school project conferences.



Comment
11/17/2019 15:55

They wrote to me in private messages that throughout the article it is not very clear what kind of homework they are talking about.

I’ll clarify right away that all the tasks that I talk about in the article are filled in as in a regular diary, and the old tasks “disappear”.

This is how the user receives those tasks that he will need to do.





The bot was written in PHP, and for the sake of experiment I decided to receive updates not using the CallBack API webhooks, but using LongPoll.



Due to the fact that I never wrote bots using this method of receiving updates, I was tormented by vague doubts about whether I could write this bot in PHP, but in the end everything worked out. Now I’ll move on to how my ideas arose and how I implemented them.



The first one, in general, was the idea of ​​writing a bot in order to store homework in it. Well, how to store. Something like notes, but inside VK, since I rarely go into notes, and they are not very convenient for me.



In order to implement a banal bot with a homework, I created one table in the database, in which the name of the item, the homework itself and the number of all this happiness were set. This is not very interesting to describe, so I will omit this point, but I will give a powerful ER-diagram of how the database looked at the very beginning.



image



Then I thought, why not give other people the opportunity to use the bot. Well, he gave people this opportunity. I changed the database a little, but the table around which all this revolves has remained almost unchanged.



image



From the ER-chart, you can understand that each user belongs to a group. Entrance to an existing group is carried out by token, and for each new one a unique token is generated. And the tasks themselves also belong to one or another group, so that users do not see chaos from their and other people's tasks.



I added the code, there is nothing interesting here either, in principle, no. I just made it so that the old tasks would not appear, so as not to delete the whole thing manually, but, I think, writing a SELECT query in SQL is quite simple.



And now it's time for fun: I started adding functions that would slow down the overall message queue, so I decided to put them in separate scripts, since I can’t multithread at the moment, but I chose LongPoll. (And is there any such use of PHP?)



And these functions: sending messages to all members of the group and generating QR codes in which the key to join the group is encrypted.



To do this, I just created another table in the database, in which I enter the mailing text and to whom it is intended. The base eventually took on this form.



image



And the generation of QR codes is generally made in a separate process, which also receives updates from VK about outgoing messages, and looks at which message contains the key to the group.



So far, everything looks more or less adequate, but then I went to break the relational model by putting JSON into the database.



What kind of JSON can I put in the database? I enter the attachments parameter, which I receive from VK, and then process them when sending messages, so as not to load the main process.



As a result, the whole thing looks like this.



image



About the same method, then I added file attachment to tasks in the bot. The task is added immediately, and the files are gradually attached if they fit the format and fit the size for Telegram Bot Api. I upload files to Telegram through a cluster of bots and save the file id.



I store files in the Telegram due to the fact that the server on which I keep all this does not have much space, and expanding for me is very difficult financially.



The whole database began to look like this:



image



But now I have a task: how will I give files back to users? Then I made almost the same logic as with the addition of files.



image



Then another bot was added for the Cart, but I will not describe it yet.



Basically, I think that the database structure is not bad, but I don’t like something here. Perhaps it will be useful for some of the newcomers to see how you can design the database structure during the development of the idea, and it will be interesting for me to listen to criticism on the organization of my base and take something into service.



The article is small, but it can bring down criticism.

Klats 1
“Full until 5 pm on my desk!”

I’ll post all the bot’s source code on git after defending the project, and it’s not a sin to write an article.



Klats 2
Some ideas that relate to working with files were prompted to me in the Habr chat in the cart, I can often be seen there.



Klats 3
You can poke a live bot here . I will be glad to feedback.




All Articles