Marc Kupietz | ea6dab2 | 2022-01-15 18:29:27 +0100 | [diff] [blame^] | 1 | #!/usr/bin/env node |
| 2 | const { WebClient } = require('@slack/web-api'); |
| 3 | const { Command } = require('commander'); |
| 4 | const program = new Command(); |
| 5 | |
| 6 | program |
| 7 | .arguments('<message>') |
| 8 | .description( 'Sends a message to a slack channel.') |
| 9 | .option('-d, --debug', 'output extra debugging') |
| 10 | .option('-c, --channel <channel_id>', 'send message to slack channel', "dereko") |
| 11 | .parse(process.argv); |
| 12 | |
| 13 | const options = program.opts(); |
| 14 | if (options.debug) console.log(options); |
| 15 | |
| 16 | // An access token (from your Slack app or custom integration - xoxp, xoxb) |
| 17 | const token = process.env.SLACK_TOKEN; |
| 18 | |
| 19 | const web = new WebClient(token); |
| 20 | const conversationId = options.channel; |
| 21 | |
| 22 | (async () => { |
| 23 | // See: https://api.slack.com/methods/chat.postMessage |
| 24 | const res = await web.chat.postMessage({ channel: conversationId, text: program.args[0] }); |
| 25 | |
| 26 | // `res` contains information about the posted message |
| 27 | console.log('Message sent: ', res.ts); |
| 28 | })(); |