内容はタイトルのとおりです。
: 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);
}
})
結果
attachmentsできれいに整形されたメッセージが投稿される