前提
Slack APIでwebhook URLを取得済み
コード
import axios from 'axios';
const notification = (content, image_url) => {
const data = {
"text": "料理が新しく投稿されました!",
"attachments": [{
"fields": [
{
"title": content,
"value": content,
}],
"image_url": image_url
}]
}
axios.post(
{WebhookのURL},
JSON.stringify(data),
// corsエラー回避
{
withCredentials: false,
transformRequest: [(data, headers) => {
delete headers.post["Content-Type"]
return data
}]
}
)
.then(res => {
console.log(res)
})
.catch(e => {
console.log(e)
});
}
結果
参考
CORSエラーの対処法
https://stackoverflow.com/questions/41042786/cors-issue-using-axios-with-slack-api
リッチな通知UIの作り方(公式)
https://api.slack.com/messaging/composing/layouts
SlackAPI attachment チートシート
https://qiita.com/daikiojm/items/759ea40c00f9b539a4c8