Writing a Chat Bot application that reminds you of class schedules has never been easier with Google Script

Tram Ho

What is Google Script?

Google Script – read through the name, we can also imagine its function: is a programming language based on Javascript with the editor, the translators are located on Google’s servers. With this tool, you can program to manipulate and intervene directly with Google services. In addition, there are many other interesting functions, among them, there is a function implementation with a certain time in a day, I took advantage of this point to push the schedule notice.


Start writing code only

The interface of Google App Script will look like this Click the New Project button to proceed with the Code writing

Initially, of course, I had to build a database for the Bot. Here I use google sheet to make database.

The content of this sheet is as follows: Since this is my first semester’s schedule, only from August to December.

There is a database already, how can I read the data from this sheet? This is when promoting the strength of Google Script. We can read the file sheet with the following function

You can see the details here https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app

Where file_tkb is the id of the database sheet it is taken as in the image below


Next we will build the sheet reading logic so we can get the desired data:

In the above code, I take out the current day ( today ), the first day of the first week of the new semester ( first here is July 29), then calculate how much today is the first day of school. day ( theDay ).

Next, create the following variables

Inside:

  1. If week_mod == 0 || week_mod == 6 , today is the weekend and assign message = "Hôm nay là cuối tuần chịu khó tập thể dục đi";
  2. In contrast, we have:

The example is a little easier to understand:

Assuming today is December 20/12/2019 , theDay = 145 , we have theDay = 145 , week_mod = 5 so that means Friday. real_col = 23 ; real_row = 14 we will start grabbing the land in the sheet in column 23, line 14.


To retrieve data I use the following code:

Once you have the data position in the sheet, take the data out with the getSheetValues function, you can see the details here https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet Here data The return data is a 2-dimensional array, we obtain study_code = a[0][0] , this is the subject code we need.


Create the corresponding dictionary for the subject code you just received

So there is enough information to send the message and then reformat the message to send a bit for a reasonable


The next step is to send the message with the above message . Here I use Chatwork, Facebook Messenger, Telegram to receive notification messages.

Send a message about Chatwork

To be able to send messages about chatwork, we must first have the API_KEY of Chatwork . I have the document of the chatwork, so to be able to mention a person in the message, I have to use webhook, but my function is not working by this mechanism, so I used a small trick as follows:

Create your own and Bot a room chat, then add two people in the message sent to the text [toall] so all the members in the room are mentioned in the message ?


Send messages via Messenger and Telegram

Telegram:

Messenger

For Telegram we have to create a Telegram Bot and get YOUR_CHAT_ID through YOUR_API_BOT_TELEGRAM of that bot you can see the instructions here

For Messenger, we need to create a Page for Bot, then send Page a message to be able to get YOUR_CHAT_ID through YOUR_GRAPH_API_FACEBOOK , and about the details, I do not guide anymore to avoid lengthy, if really If you are interested, you can refer to other articles related to Graph API of Facebook


Finally, the setting for my function runs daily at a certain time frame

Here select the function name to execute to run daily


So you all know how the Bot works ? ).

Depending on the creativity, you can write a Bot with different functions, here I have to write more weather alerts.


And here is the result


My article here is the end of hope to help and bring joy to everyone. However, when I finished writing this article, my concept of ChatBot seemed to be confused ?? , because ChatBot can reply automatically when receiving a message, currently my Bot has not done that, in fact it is just a function that pushes the message for a while.

So you understand what the ChatBot concept is like, if I’m misunderstanding then please let me know ??


Share the news now

Source : Viblo