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
1 2 | curl -X POST -H "X-ChatWorkToken: API token" -d "body=Hello+ChatWork%21" "https://api.chatwork.com/v2/rooms/{room_id}/messages" |
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:
1 2 | curl -X POST -H "X-ChatWorkToken: xxxxxxxxxxxxxxxxxxx" -d "body=Hello+ChatWork%21" "https://api.chatwork.com/v2/rooms/170206592/messages" |
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:
1 2 | vi /etc/call_me.sh |
The content in that file will be my command above
1 2 3 4 | #!/bin/bash curl -X POST -H "X-ChatWorkToken: xxxxxxxxxxxxxxxxxxx" -d "body=Hello+ChatWork%21" "https://api.chatwork.com/v2/rooms/170206592/messages" |
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 nè
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
1 2 | crontab -e |
Then enter the code below
1 2 | 0 8,11,13,16,18 * * * sh /etc/call_me.sh >/dev/null 2>&1 |
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:
1 2 | >/dev/null 2>&1 |
So that it will not send a log to the email, in the docvps documentation already listed above
Time to use
1 2 | crontab -l |
Let me check the written job file
and run the command
1 2 | sudo service cron restart |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #!/bin/bash hour=$(date +%H) if [ "$hour" = "08" ] then body="[info][title]Let's go !!![/title]Báo cáo công việc hôm nay nào anh ơi, Let's go!!![/info]" elif [ "$hour" = "11" ] then body="[info][title]Keep moving forward!!![/title]Làm việc cẩn thận anh ơi, Keep moving forward!!![/info]" elif [ "$hour" = "13" ] then body="[info][title]Keep moving forward!!![/title]Cố lên anh ơi, Keep moving forward!!![/info]" elif [ "$hour" = "16" ] then body="[info][title]Keep moving forward!!![/title]Báo cáo nào anh ơi, Keep moving forward!!![/info]" elif [ "$hour" = "18" ] then body="[info][title]Toang rồi!!![/title]18h rồi đấy, giờ sao nào[/info]" fi curl -X POST -H "X-ChatWorkToken: 3f10faa0e371f25b89bc135d5817596b" -d "body=$body" "https://api.chatwork.com/v2/rooms/170206592/messages" |
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 =))