We hold a cloudy study session every Saturday, but shortly before the start, "Paste the URL of zoom and send an email considering the text of the email." This work is troublesome. So let's create a fixed form and send it to slack automatically! !! It is the content.
■ Rough flow
First, create a file to write the program. I created a file called Saturday_execute in the shellscript directory because it's a file to run on Saturday. (The path of ./shellscript/Saturday_execute is used in the cron settings.) Now let's grant permissions to Saturday_execute.
■ Display the file information in the directory
/shellscript
$ ls -l #Display file information in the directory
-rw-r--r--1 File owner name File affiliation group File size Update date Saturday_execute(file name)
Usage and options of the ls command
Details of the displayed information --Three characters on the left: Owner's access rights --Central 3 characters: Access rights of owning group --Three characters on the right: Owner's access rights
-r: Readable --w: Writable --x: Executable ---: No access authority
Since the third character of the owner's access authority is "-", you can see that you do not have execute authority. Since you do not have execute permission, let's give execute permission.
■ Granting execution authority Use the chmod command to change permissions. How to use chmod and options
/shellscript
$ chmod u+x Saturday_execute #chmod permission setting file path
Command explanation
symbol | meaning |
---|---|
u | Owner authority |
As explained in "■ Displaying directory information", x was the execution authority.
It means "give execute permission to the owner of the Saturday_execute file".
Search for Incoming Webhook from "+ button next to App" at the bottom left of slack. Click "Add Settings" Select the channel you want to be notified of and click Add Incoming Webhook Integration. Copy the Webhook URL for use by embedding it in the code.
This completes the settings! !!
Set the URL setting of Incoming Webhook, send channel of slack, send name, icon, message and send with curl command.
/shellscript/Saturday_execute
#!/bin/bash
#################################################################################
#Email the URL for study session participation on Saturday morning
#Overview:Set the URL setting of Incoming Webhook, send channel of slack, send name, icon, message and send with curl command.
#Execution date:Every Saturday 9:55
#################################################################################
#Notify slack's "study session" channel(URL of Incoming Web Hooks)
WEBHOOKURL="URL of the copied Incoming Webhook"
###############################################################
#Creating a fixed text string
###############################################################
#slack send channel
CHANNEL="#study group"
#slack sender name
BOTNAME="Notice of study session information"
#slack icon
FACEICON=":snail:" #I am using the slug icon. If you want to change the setting, "https"://www.webfx.com/tools/emoji-cheat-sheet/"use.
#Message content
MESSAGE="I will start a study session from now on.\n Please enter from the URL below.\n ■ Time: 10:00 to 17:00\n <zoom URL>"
curl -X POST --data-urlencode "payload={\"channel\": \"${CHANNEL}\", \"username\": \"${BOTNAME}\", \"icon_emoji\": \"${FACEICON}\", \"text\": \"${MESSAGE}\" }" ${WEBHOOKURL}
The code content is just storing the character string in a variable and sending it with the curl command! !!
・ If you don't understand the basic operation, see below! Basic commands for shell scripts ・ If you don't know how to use the curl command, see below! curl command options
When you reach this point, all you have to do is set to "execute the specified program at the specified time"! !! ■ Job scheduling is a function that automatically executes a specified program at a specified time. The following two types
・ Cron --Automatic execution on a regular basis --Configure with crond and crontab commands ・ At --Automatic execution only once --Specify a script with standard input
This implementation will be notified weekly, so we will do it with cron. Now let's set the "/ shellscript / Saturday_execute" file to run at "9:55 every week".
python
$ crontab -e #Start with e option
Switch to insert mode with i and enter the following
crontab
# (Minutes)(Time)(Day)(Month)(曜Day)The path of the command to execute
55 9 * * sat bash ./shellscript/Saturday_execute #Every Saturday at 9:55 "Saturday_execute "file execute
#After inputting, press the esc key:Save with wq
Press the exc key: Save with wq and you're done! !! !!
A notification will be sent to slack like this.
If you don't know the details of cron, please refer to the following.
Perform regular processing with cron I want to stop using numbers to specify the day of the week in the ron format
Recommended Posts