LoginSignup
30
17

More than 3 years have passed since last update.

slack webhookにcurlしたらつらかった話

Last updated at Posted at 2016-12-21

はじまり

Slack APIを使用してメッセージを送信する - Qiita
この記事を読み、slackの1チャンネルにメッセージを送りたいなと考えました。
webhookを使って、curlで叩いてjsonを投げてPOST requestを送ります。

webアプリなら、jsonを動的に生成して投げれば良いので、余裕っすね。
(このときはまだ作業の大変さを知らない...)

状況

解析の日報をmarkdownで作って、それをslackに飛ばして日報にしようと思ったわけです。

環境

  • OS: Windows10
  • console: Git bash

序章: 記事の再現

slackのwebhookや、上記qiita記事を参考に、最初のコマンドを組みます。
slackの例だとこんな感じ。

slack-example
$ curl -X POST \
  --data-urlencode 'payload={"channel": "#chemspec-iot"
  , "username": "webhookbot"
  , "text": "This is posted to #chemspec-iot and comes from a bot named webhookbot."
  , "icon_emoji": ":ghost:"}'  \ 
https://hooks.slack.com/services/{TOKEN}

Qiita記事 (上記と同じ)では、こんな感じ。

qiita-article-example
$ curl -X POST \
  -d @message.json  \ 
https://hooks.slack.com/services/{TOKEN}
message.json
payload={
      "username": "webhook"
    , "channel": "@sshojiro"
    , "text": "see more detail about `webhook` in http://qiita.com/rubytomato@github/items/6558bfdb37d982891c09#incoming-webhooks \n this message is sent via `curl`"
    , "icon_emoji": ":ghost:"
}

結果がこちら:

bot1.jpg

ここまででそこそこ嬉しくて喜びます

二章: 苦節数時間

下記のmarkdownを、上記のようなcurlcommandでslackのチャンネルに表示したいというのがゴールです。下記のmarkdownは、日報とかを想定しています。

notes/2016-12-21.md
# 2016/12/21 \n
## TODO \n
- *bold* \n
- _Italy_ \n
- ~stank~ \n
- aaaa

そこで、苦悩しながら作ったのが次のスクリプト。
$1.mdから、$1.jsonを作成した後、curlでPOST requestします。そののち、作った一時ファイルの$1.jsonを削除。

send_msg.sh
send_msg () {
  val=`cat notes/$1.md`
  echo 'payload={
      "username": "webhook"
    , "channel": "#chemspec-iot"
    , "text": "'"$val"'"}' > notes/$1.json
  curl -X POST \
    -d @notes/$1.json \
    https://hooks.slack.com/services/{TOKEN} # web-hook url
  rm notes/$1.json
}
send_msg $1

終章: 完遂

これを実行してみました。

$ ./send_msg.sh 2016-12-21

無事にslackのchannelへと送信できました。めでたしめでたし。

bot2.jpg

結言

slackのwebhookに対してPOST requestを送り、shell経由で*.mdの内容を自分宛てのchannelに表示することが出来ました。
これにより、一覧性のある日報を示すことが可能になります。

課題として、改行文字を入れないとslack上では1行で出てしまいます。別の記事でこれに対応したいです。

追記

2016/12/21

コメント欄の通り、@manji-0さんからコメントいただきました。

val=`cat notes/$1.md`

の箇所を、

val=`cat notes/$1.md | sed -e 's/$/ \\n/g'`

のように修正すると、markdown中の\nが不要になります。

2020/05/26

Windowsのcurlをたたくとき(WindowsでMATLABを利用する場合など)は文字列のクォートの付け方が異なるので注意が必要です。
また、slack側が示したサンプルコードもWindowsのコマンドプロンプトには通用しません。
例えばMATLABからslackを叩く場合は以下のようなコードを書くと良いでしょう。

function slack(msg)
if~exist('msg','var'),msg = 'Hello World!'; end
secret = 'SLACK_SECRET';
cmd    = ['curl -X POST -H "Content-type: application/json"' ...
          ' --data "{'...
          '\"text\":\"' msg '\", ' ...
          '\"username\": \"@webhook\", '...
          '\"channel\": \"@sshojiro\", '...
          '\"icon_emoji\": \":ghost:\"  '...
          '}" https://hooks.slack.com/services/' secret];
[status, cURL_out] = system(cmd);
if status ~= 0, error(cURL_out); end 

Windowsからcurlでslack webhookを叩く記事

30
17
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
30
17