LoginSignup
9
4

More than 5 years have passed since last update.

Node.js(axios利用)でSlackにPOSTするサンプル

Posted at

Node.js v10.6.0です。 自分がオーナーのSlackって実は持ってない気がするのであんまりSlackのBOTって試してなかったり...

app.js
'use strcit';

const axios = require('axios');
const BASE_URL = `https://hooks.slack.com/services/xxxxxxxxxxxxxxxxxx`; // トークンURL
let options = {
    method: 'post',
    baseURL: BASE_URL,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    },
    data: `payload={
        "channel": "#general",
        "username": "webhookbot",
        "text": "test",
        "icon_emoji":":ghost:"
    }`
};

const main = async () =>{
    try {
        const res = await axios.request(options);
        console.log(res.data);
    } catch (error) {
       console.log(error);
    }
}
main();

実行してエラーがなければOKです。

$ node app.js
ok
9
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
9
4