LoginSignup
4
4

More than 5 years have passed since last update.

PowerShellスクリプトでSlackAPIにメッセージをPOSTする方法

Last updated at Posted at 2016-11-13

はじめに

SlackにPowerShellスクリプトやAzure Automationでメッセージを投稿した時の備忘録

SlackAPIのトークン取得

Slackに投稿する時は固有のトークンが必要なので
https://api.slack.com/docs/oauth-test-tokens
からトークンを生成しておく。

PowerShellスクリプトの作成

特定のURL( https://slack.com/api/chat.postMessage )に
特定の形式のデータをPOSTするだけでメッセージ投稿できる。

$postSlackMessage = @{token='YourToken';
                      channel='targetChannel';
                      text='BOTテスト';
                      username='BOTテスト'}
Invoke-RestMethod -Uri "https://slack.com/api/chat.postMessage" -Body $postSlackMessage

これを実行するとSlackに投稿されてAPIからレスポンスが返ってくる。

スクリーンショット 2016-11-13 21.27.24.png

成功した場合のレスポンス例

 ok channel   ts                message                                                                                
  -- -------   --                -------                                                                                
True C307S1WQK 1479039659.000002 @{text=BOTテスト; username=BOTテスト; bot_id=B3011E4GH; type=message; subtype=bot_message;...

投稿に失敗したらこんな感じ(指定チャンネルが無いとき)

   ok error            
   -- -----            
False channel_not_found

POSTデータに必須のプロパティ

プロパティ名 概要
token 取得したトークン
channel 投稿するチャンネル
text 投稿する文章

その他オプションでつけることができるプロパティは以下のリンク
https://api.slack.com/methods/chat.postMessage

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