Slack Curl


This bash script will post a message to Slack. It can be easily changed to fit your needs but it can be used as it is as well. This script is very useful to send notifications for background tasks for example.

概要SlackのAPIを使用してメッセージを送信するまでの操作メモです。環境Slack (Free plan).curl (Windows版)参考Welcome to the Slack API. Affiliate Links Making curl requests to search your Slack History is a little clunky, so we have modified the example UI that MeiliSearch provides in their docs to search through the Slack results. בין אם מדובר בבדיקת הפלט של ממשק ה- API לפני פריסתו לייצור, או פשוט השגת תשובה מאתר אינטרנט (למשל, כדי לבדוק שהיא לא מופעלת), תלתל הוא כמעט בכל מקום. כמדען נתונים הייתי צריך להשתמש בו מדי פעם. Justa quick video showing how to use curl to post to slack rooms or even users.

This bash script is utelising curl to post a message to a given slack channel. The script expects 4 arguments:

  1. title of the message
  2. message body
  3. Slack channel name
  4. Slack hook url

Slack Webhook Curl

Having said that there is nothing preventing you from adding extra parameters, for example you can add an emoji as an argument for further customising your messages look and feel.

Usage block

Slack Curl

The script starts with a function which returns the usage block. Usage function acts as documentation for the program. This function is called if the user doesn't pass any of the parameters or as a default response if the user just trid to execute the script without passing anything. Listing 1.1 shows the usage function

Listing 1: usage function

Extracting arguments

Listing 2 shows how I am extracting and validating arguments for this script

Listing 2: extracting and validating arguments

Minimal validating for arguments is happening here however you can add whatever extra validation that you want. For example you may want to restrict the channel name to be of certain values

Posting the message

The rest of the script is using curl to post the message to Slack.

Note how I am utilising the read command to format the json payload. This technique offers maximum readability and customisation for your message payload. This program output the status code returned from curl. So anything but 200 means failure

Listing 3: posting message to Slack and return status code

If you don't have a bin folder under your home folder then create one and place this script in it. Alternatively you can place this script in /usr/local/bin/code so that it is available for all users

Slack Curl

Slack Curl Chat.postmessage


Next you need to make this script executable

Create symbolic link - optional

You can create a symlink for this script so you can name it whatever you want or use it without the .sh extension. If you do this then there is no need to place the script in the bin folder mentioned before

Now you can use the script from command line like this

Calling from other scripts

you can call this program from other bash scripts like this for example

Slack Curl Emoji

Хочу сделать интеграцию с внешней системой, в частности мессенджером Slack, он позволяет сделать WebHook, и обращаться к нему посредством POST запроса, например:

curl -X POST --data-urlencode 'payload={'channel': '#general', 'username': 'BPM-bot', 'text': 'This is posted to #general and comes from a bot named webhookbot.', 'icon_emoji': ':ghost:'}'

Это запостит в канал #general от имени BPM-bot сообщение text и смайл, уникальный адрес webhook`а для моего инстанса Slack.

Как в BPM реализовать отправку этого сообщения? Т.е. как сделать бизнес-процесс мне понятно, а вот с отправкой сложности...


Показать все комментарии
Войдите или зарегистрируйтесь, что бы комментировать