Reminder with cronjob and chatwork api

Tram Ho

Target:

I want to create a bot that reminds me of work and encourages me at the right time =))

I simply use an account chatwork clone to send encouragement messages, cronjob to set schedule to run periodically when sending messages. Over.

That’s the theory, let’s get started (go)

1. Create chatwork clone

Very simple, go to https://go.chatwork.com/ and sign up as instructed =))

Next, we have to make friends with our main account to send messages

And the most important step is to get API Token, only personal accounts can get API token easily

For domain accounts like sun-asterisk , you need admin approval to get it

Handling only:

You click on your profile in the upper right corner -> API settings -> API Token

Chatwork forces you to enter your password to display the API Token

If successful we will get like this:

With this API Token, we can retrieve any messages that the account can access, or can send messages in the boxes we have access to.

2. Send message chatwork with curl

  • After completing the steps in part 1, we will have API token, this is the material for me to continue step 2, which is to send a message to my main chatwork account with the command curl
  • Everyone can find out a bit about the chatwork api here , Doc is quite specific but easy to read.
  • In this article, I only care about api post to send messages to main account only. So, I’ll find the api post chat with messages, here
  • The syntax of that API would be

In the input, we need the API token , room_id (the id of the room chat, even if it is a private chat together, it is counted as the room), the body (is the message we will send each other the affection)

Handling only:

Let’s try the above command:

With xxxxxxxxxxxxxxxxxxxxxxxx is the API Token I just got

This result:

There is a proper message too =))

So that’s it, let’s move to the next step =))

3. Create sh file so the timer will run cronjob

The .sh file stands for shell script. A shell script is a text file containing a sequence of commands for UNIX / Debian / Linux / Ubuntu based systems.

Anyone who wants to learn more, please come here

Handling only:

I will write a simple file only

Create file:

The content in that file will be my command above

Then I will run that file to check if it can be called from outside

Run away:

With /etc/call_me.sh is your file path

Result:

So we have completed 70% of the process already, and one more step, which is to write cronjob (len3)

4. Create cronjob file

Cronjob is a function used to periodically execute certain commands for a period of time predefined by the user

You can learn more here

A crontab file usually has 5 time-defined fields (probably 6).

If a column is assigned the * character, it means the task will then be run at every value for that column.

You can use it for testing at https://crontab.guru/ , this page displays very full of information that people need.

The theory is that, I will start writing crontab file

Handling only:

I will edit the file cronjob by command

Then enter the code below

Looking at the code above, we can see that I will schedule the file /etc/call_me.sh to run at 8am (Just arrived), 11h (prepare lunch time), 13h (just wake up), 16h (standard) time back), 18h (stay late)

Code:

So that it will not send a log to the email, in the docvps documentation already listed above

Time to use

Let me check the written job file

and run the command

to restart the cron service after editing it

The last problem is, how to send different messages at different time frames, of course I have to edit the file sh above.

Which achievement:

Explain a little:

I know cronjob file to schedule call_me.sh run and when this file is called, I will take the current time to be able to customize for each message, because the format is 2-digit format, so it will be “08” not just “8”)

Note that cronjob only runs when your device is on, if you turn it off then cronjob will not be able to run anymore. So it’s suitable for work, isn’t it, Saturday and Sunday will not be disturbed anymore

That’s it, that’s it for today’s task

Thanks for watching =))

Share the news now

Source : Viblo