0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Slack: Webhook経由でSlackに投稿してみる

0
Posted at
実施環境:

Slack 4.52.182 (64-bit)

0. 概要

Slack に外部のプログラムから投稿する方法には、 SlackAPI を使用したものがあります。

それとは別に、 Webhook 経由で投稿する方法もあります。
今回は、 Webhook 経由で投稿する方法について試してみます。

1. Slack App の作成

あらかじめ、以下の過去記事と同じ流れで、 Slack App を作成しておきます。

2. Webhook の有効化

「管理者」⇒「アプリ&ワークフロー」⇒「ビルド」から事前に作成した Slack App を開きます。

WS000402.JPG

WS000403.JPG

WS000406.JPG

左メニューから、「 Incoming Webhooks 」を選択します。

WS000407.JPG

「 Active Incoming Webhooks 」を ON にして Webhook を有効化します。

WS000408.JPG

権限の有効化が必要なため、上にメッセージが表示されます。
上のメッセージの「 reinstall your app 」をクリックします。

WS000409.JPG

Webhooks の送り先となるプロジェクトとチャンネルを選択し、「許可する」をクリックします。

WS000411.JPG

権限が正常に許可できていれば、ページの下の方に Webhook URL とサンプルコマンドが表示されます。

WS000418.JPG

別のチャンネルへの Webhook を追加したい場合は、「 Add New Webhook 」をクリックします。

WS000415.JPG

逆に Webhook を削除したい場合は、ごみ箱マークをクリックし、ポップアップで「 Remove 」をクリックします。

WS000417.JPG

WS000416.JPG

3. curlコマンド実行

「 Copy 」をクリックしてサンプルコマンドをコピーします。

WS000413.JPG

これでコピーされるコマンドは以下の通りですが、 Windows のコマンドプロンプトでこれをこのまま実行しても、以下の通りエラーとなります。

コマンドプロンプト
>curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello, World!"}' https://hooks.slack.com/services/***/***/***
curl: (6) Could not resolve host: application
invalid_payload

これは、 Windows のコマンドプロンプトだとシングルクォーテーションが正常に処理できないためです。
以下のようにダブルクォーテーションのみにして、 JSON 内部のダブルクォーテーションにエスケープを追加すると、正常に実行できます。
正常に実行できれば、「 ok 」と表示されます。

コマンドプロンプト
>curl -X POST -H "Content-type: application/json" --data "{\"text\":\"Hello, World!\"}" https://hooks.slack.com/services/***/***/***
ok

以下の通り、「 text 」に指定した文字列がチャンネルに投稿されました。

WS000414.JPG

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?