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'); |
Marc Kupietz | 229eba2 | 2022-01-16 17:02:50 +0100 | [diff] [blame] | 4 | const fs = require('fs'); |
Marc Kupietz | ea6dab2 | 2022-01-15 18:29:27 +0100 | [diff] [blame] | 5 | const program = new Command(); |
Marc Kupietz | 229eba2 | 2022-01-16 17:02:50 +0100 | [diff] [blame] | 6 | var stdin = ""; |
| 7 | |
| 8 | // An access token (from your Slack app or custom integration - xoxp, xoxb) |
| 9 | const token = process.env.SLACK_TOKEN; |
| 10 | const web = new WebClient(token); |
Marc Kupietz | ea6dab2 | 2022-01-15 18:29:27 +0100 | [diff] [blame] | 11 | |
| 12 | program |
Marc Kupietz | 229eba2 | 2022-01-16 17:02:50 +0100 | [diff] [blame] | 13 | .arguments('[message]') |
| 14 | .description('Sends <message> to a slack channel. Use - for reading message from stdin') |
Marc Kupietz | ea6dab2 | 2022-01-15 18:29:27 +0100 | [diff] [blame] | 15 | .option('-d, --debug', 'output extra debugging') |
| 16 | .option('-c, --channel <channel_id>', 'send message to slack channel', "dereko") |
Marc Kupietz | 229eba2 | 2022-01-16 17:02:50 +0100 | [diff] [blame] | 17 | .parse(process.argv) |
| 18 | .action(function (message) { |
| 19 | if (stdin) message = stdin; |
| 20 | |
| 21 | (async () => { |
| 22 | // See: https://api.slack.com/methods/chat.postMessage |
| 23 | const res = await web.chat.postMessage({ channel: options.channel, text: message }); |
| 24 | // `res` contains information about the posted message |
| 25 | console.log('Message sent: ', res.ts); |
| 26 | })(); |
| 27 | }) |
Marc Kupietz | ea6dab2 | 2022-01-15 18:29:27 +0100 | [diff] [blame] | 28 | |
| 29 | const options = program.opts(); |
| 30 | if (options.debug) console.log(options); |
| 31 | |
Marc Kupietz | 229eba2 | 2022-01-16 17:02:50 +0100 | [diff] [blame] | 32 | if (program.args.length == 0 || program.args[0] == '-') { |
| 33 | console.debug("Reading from stdin") |
| 34 | stdin = '```\n' + fs.readFileSync(0, 'utf-8') + '```'; |
| 35 | } |
Marc Kupietz | ea6dab2 | 2022-01-15 18:29:27 +0100 | [diff] [blame] | 36 | |
Marc Kupietz | 229eba2 | 2022-01-16 17:02:50 +0100 | [diff] [blame] | 37 | program.parse(process.argv); |