LoginSignup
75
66

More than 5 years have passed since last update.

SlackのIncoming Webhooksのメッセージをカスタマイズ

Last updated at Posted at 2015-06-02

はじめに

  • SlackではIntegrationの一つにIncoming Webhooksがあり、Incoming Webhooksを使うと、特定のChannelに外部からメッセージを送信できます
  • 送信するメッセージは、次のようなJSONになります
{
    "text": "This is a line of text.\nAnd this is another one."
}

簡易的なカスタマイズ

  • username, icon_url, icon_emojiをJSONに指定することで、メッセージの送信者名、アバター画像をカスタマイズできます。

たとえば、次のJSONを送信すると、


{
  "text": "I found a MONSTER! ",
  "channel": "#incoming-test",
  "username": "Baymax",
  "icon_emoji": ":baymax:"
}

このようにタイムラインに表示されます。

スクリーンショット 2015-05-18 13.29.27.png

メンションをリンクにしたい場合

@nameをリンクにしたい場合

"link_names": 1

を付ける。

リッチなフォーマット

  • リッチなフォーマットで表示させたい場合は、Attachmentsの書式に従ってJSONを作成します
  • 詳しくは Attachments を参照してください

たとえば、次のJSONを送信すると、


{
  "channel": "#incoming-test",
  "username": "Baymax",
  "icon_emoji": ":baymax:",
  "text": "I found a MONSTER! ",
  "attachments": [
    {
      "color": "#36a64f",
      "image_url": "http://xxx/uploads/monster/image/3/thumb_thany1.jpg",
      "fields": [
                {
                    "title": "Priority",
                    "value": "High",
                    "short": false
                }
            ]
    },
    {
      "color": "danger",
      "image_url": "http://xxx/uploads/monster/image/3/thumb_thany1.jpg"
    }
  ]
}

このようにタイムラインに表示されます。

スクリーンショット 2015-05-18 13.18.57.png

JSONを送信する場合のTips

curlコマンドのパラメータに-d @xxxx.jsonをつけると、xxxx.jsonファイルを渡せます。

$ curl -X POST http://localhost:4567/message -d @message.json
$ cat message.json

{
  "text": "I found a MONSTER! ",
  "channel": "#incoming-test",
  "username": "Baymax",
  "icon_emoji": ":baymax:"
}

75
66
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
75
66