Web Laravel – Create Bot to send message to chatwork to remind students of class schedule

Tram Ho

Every IT developer who used to be a student, but has been a student of credits, cannot remember all the schedules and I am also a person who often forgets the schedule, mistakes the schedule for When looking at the schedule from the school’s web site, I want to explode, I have to see what day it is today, then check what subject I study today, what period, which class … the class schedule displays in file format excel look real headache. That’s why I wrote a small web application that solves this.

Target

The goal of this article is to create a web application with the following functionality

  1. Import study calendar from excel file, analyze and display it on calendar to easily view calendars
  2. Create a Bot that automatically sends messages to remind you of your class schedule each day
    Attached to the product, I also introduced and instructed how to use some good packages.
    Some demo photos:

Technology and some packages used

Framework Laravel
Laravel is a free and open source PHP Framework …
Maatwebsite / Excel Package
This package handles importing and exporting data into the database through files such as excel, csv …
https://docs.laravel-excel.com/3.1/getting-started/
Package sun-asterisk / chatwork-php
This package was developed by the R&D department of Sun-asterisk. Package makes chatwork easier to work with …
https://github.com/sun-asterisk-research/chatwork-php
Laravel Full Calendar
This package supports calendar creation
https://github.com/maddhatter/laravel-fullcalendar

Embark on the code

Before coding, I also list out what to do first

  1. Create project laravel, create database connection
  2. Create a calendar display interface
  3. Get excel file of class schedule, create table to save subject data, create form to send files to handle insert into database
  4. Display the class schedule on the calendar
  5. Create a Bot chatwork, create a cronjob Bot send a message to the chatwork to remind you of the daily schedule
    Part 1 install project laravel create database connection, composer …. do it yourself, I will do the tutorial from part 2.

Create a calendar display interface

This section will use the Laravel Full Calendar package so I will install it by command
composer require maddhatter/laravel-fullcalendar
Continue to create CalendarController and router to return view
Router
Route::get('/calendar', ' [email protected] ');
Controller, this section only displays the temporary calendar without any data, the data we will pass into the array of $ events is done.

Create file view calendar.blade.php in foder resources

The interface above I use the CDN mn links that can be copied and run, the interface includes a form for me to send the file and the calendar to display the calendar, then the route / calendar will display the interface like this.

Get excel file of class schedule, create table to save subject data, create form to send files to handle insert into database

Next, I went to the school’s website to download the schedule file, here I exported the file in the class format displayed by the module, it seems that many schools use the same website template so the format is the same, if the school If you are different, then it’s okay, just edit a little bit in the file reading only
The file opened looks like this, it looks complicated.

Next I need to create a table to save data, migrate as follows

Class column to save the class name, date is the school day, lession is the lesson, room is the classroom location. Remember to create a model named Schedule for this schedule data table.

In the previous interface form, I created the action sent to the route “/ calender / import” and now I create the router and the function to receive the file
Router
Route::post('/calender/import', ' [email protected] ');
Function import in CalenderController

This section uses the Maatwebsite Excel package so I will install it by command

Publish config
php artisan vendor:publish --provider="MaatwebsiteExcelExcelServiceProvider"

Create a file to handle reading excel file and insert data into DB
php artisan make:import CalenderImport --model=Schedule
The content of this file is as follows

It looks so clunky: this piece of code I wrote and used about a year ago but I will stay like this a bit long but it seems easier to understand
The above function works as follows, it will read each row in the excel file, then check if the row is the subject (ignoring the other information rows), if it is the row that does not contain each subject then I handle cutting sequence to take class time, class day, lesson period, place of study and calculate the exact time of the school day according to the above time period and then save information of each class to DB …
The data is saved as follows

Display class schedule on calender

The display I just need to get the data out, create even a display on the calender, edit the function index on the controller is done, the completed CalenderController will be as follows.

Now go back into the view and you’ll see the class schedule displayed

Create a Bot chatwork, create a cronjob Bot send a message to the chatwork to remind you of the daily schedule

This section firstly you need to have 2 accounts https://www.chatwork.com/ , 1 account as a bot to send messages and an account to receive messages.
have 2 accounts then I started creating cronjob to send messages to chatwork
To send a message to the chatwork, I need to install the sun-asterisk / chatwork-php package introduced at the beginning of the article
composer require sun-asterisk/chatwork-php
After that, we continue to run commands to create the command
php artisan make:command DailyNotification
This command will create DailyNotification.php file I will handle firing the message here
Now I need to get the token from the bot account, it will represent the bot account to send a message instead of entering the account and password.
Log in to the chatwork and go to this link to get the token https://www.chatwork.com/service/packages/chatwork/subpackages/api/token.php
If you already have a token, then modify the command as follows

here your-api-token is the token you got on your bot account
and your-room-id is the chat id between the bot and you, can be taken directly from the chat url or use the command in the package SunAsterisk Chatwork
Done then you run this command immediately Bot will send you a messgae learning schedule, php artisan command:notification Now let me create a schedule for it to run daily, register in the kernel and set up cronjob on the server.

I let him run the notification at 6am. Or you can run before each lesson …

End

This article I introduced a number of packages and how to apply it, just a few small ones only make life simpler and more convenient. Thank you everyone for watching, looking forward to receiving your suggestions to improve the quality of the article. If you find it interesting, don’t forget to upvote
Make Awesome Things That Matter!

Share the news now

Source : Viblo