LoginSignup
2
4

More than 5 years have passed since last update.

Node.jsのrequestモジュールを使用してattachments付きでSlackに投稿するサンプル

Posted at

内容はタイトルのとおりです。
:warning: : attachmentsをJSON.stringify()してから渡す

サンプル

slack-post.js
'use static';
const request = require('request');

let slack_endpoint = "https://slack.com/api/chat.postMessage";
let payload = {
  form: {
      token: '<slack_api_token>',
      channel: 'bot-test',
      username: 'test-bot',
      attachments: JSON.stringify([
        {
          "color": "#36a64f",
          "pretext": "node.jsのrequestモジュールを使用してattachments付きで投稿",
          "author_name": "daikiojm",
          "author_link": "http://qiita.com/daikiojm",
          "title": "テスト投稿",
          "text": "テスト投稿の内容"
        }
      ])
  }
}
request.post(slack_endpoint, payload, (error, response, body) => {
  if (error) {
    console.log(error);
  } else {
    console.log(body);
  }
})

結果

SnapCrab_NoName_2017-6-7_22-0-36_No-00.png

attachmentsできれいに整形されたメッセージが投稿される:zap:

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