Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

内容はタイトルのとおりです。
: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:

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away