Frequently at work, data acquisition, processing, and analysis execution are processed in the Linux environment of the server.
You can check if the process is finished with ps
on the terminal, but it is troublesome to check each time.
I want to take prompt action when processing is broken.
A common method is to detect by POSTing a message via email or SNS.
Communication in Slack is the mainstream in my workplace.
POST a message in Slack to determine if it's done.
It is assumed that you have already created a Slack account.
There seem to be various Apps & Integrations in Slack. This time, I will summarize the methods of POSTing messages by the following two methods that are often seen on the Internet. Personally, I like "Incoming Web Hooks" because it has fewer required items and shorter code.
name | Easy features |
---|---|
Slack Web API | You can do various things |
Incoming WebHooks | Easy channel posting |
Very easy. Log in to slack on the WEB and follow the steps below.
name | Acquisition method |
---|---|
Slack Web API | ・Create tokenpush. ・パスワード入力後、押したところにトークンが表示される。 |
Incoming WebHooks | ・Select Post to Channel and Add Incoming WebHooks Integrationpush. ・Webhook URLの横にトークンが表示される。 |
Try POSTing a message with Shell
and Python
Slack Web API Click here for a list of API methods (https://api.slack.com/methods). This time I want to POST the message, so I use the "chat.postMessage" method.
The list of parameters is here. The required items are as follows.
Parameter name | Description |
---|---|
token | token |
text | Text to post |
channel | Channel name Encoded ID for direct messages |
This time, we will create a sample using "token", "text", "channel", "username", and "link_names".
※ Caution
If you want to POST a message to channel, there is no problem with "#
Shell
#Setting
TOKEN='<Obtained token>'
CHANNEL='<Channel name>'
USERNAME='test_username'
LINK_NAMES='1'
URL='https://slack.com/api/chat.postMessage'
# post
curl="curl -XPOST -d \"token=${TOKEN}\" \
-d \"text=${TEXT}\" \
-d \"channel=${CHANNEL}\" \
-d \"username=${USERNAME}\" \
-d \"link_names=${LINK_NAMES}\" \
${URL}"
eval ${curl}
Python
#Library import
import requests
#Setting
TOKEN='<Obtained token>'
CHANNEL='<Channel name>'
TEXT='test'
USERNAME='test_username'
URL='https://slack.com/api/chat.postMessage'
# post
post_json = {
'token': TOKEN,
'text': TEXT,
'channel': CHANNEL,
'username': USERNAME,
'link_names': 1
}
requests.post(URL, data = post_json)
Incoming WebHooks Note that the POST method is slightly different from that of the Slack Web API.
The parameters are as follows. This time, create a sample using "text", "username", and "link_names".
Parameter name | Required item | Description |
---|---|---|
text | 〇 | Text to post |
username | Post username | |
icon_url | URL in the profile image of the post | |
icon_emoji | Emoji to put in the profile image of the post | |
link_names | Mention | |
channel | Channel name | |
attachments | AttachmentsFill in the form |
Shell
#Setting
URL='<Obtained token>'
TEXT='test_text'
USERNAME='test_username'
LINK_NAMES='1'
# post
curl="curl -X POST --data '{ \
\"text\": \"${TEXT}\" \
,\"username\": \"${USERNAME}\" \
,\"link_names\" : ${LINK_NAMES}}' \
${URL}"
eval ${curl}
Python
#Library import
import requests
import json
#Setting
URL='<Obtained token>'
TEXT='test'
USERNAME='test_username'
# post
post_json = {
'text': TEXT,
'username': USERNAME,
'link_names': 1
}
requests.post(URL, data = json.dumps(post_json))
--The list of pictograms is listed here [http://qiita.com/koukun/items/ae673f2bae8f1525b6af)
Recommended Posts