blob: fe07cb5a308d027958236fc8921a4d1414515ec2 [file] [log] [blame]
Marc Kupietzea6dab22022-01-15 18:29:27 +01001#!/usr/bin/env node
2const { WebClient } = require('@slack/web-api');
3const { Command } = require('commander');
Marc Kupietz229eba22022-01-16 17:02:50 +01004const fs = require('fs');
Marc Kupietzea6dab22022-01-15 18:29:27 +01005const program = new Command();
Marc Kupietz229eba22022-01-16 17:02:50 +01006var stdin = "";
7
8// An access token (from your Slack app or custom integration - xoxp, xoxb)
9const token = process.env.SLACK_TOKEN;
10const web = new WebClient(token);
Marc Kupietzea6dab22022-01-15 18:29:27 +010011
12program
Marc Kupietz229eba22022-01-16 17:02:50 +010013 .arguments('[message]')
14 .description('Sends <message> to a slack channel. Use - for reading message from stdin')
Marc Kupietzea6dab22022-01-15 18:29:27 +010015 .option('-d, --debug', 'output extra debugging')
16 .option('-c, --channel <channel_id>', 'send message to slack channel', "dereko")
Marc Kupietz229eba22022-01-16 17:02:50 +010017 .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 Kupietzea6dab22022-01-15 18:29:27 +010028
29const options = program.opts();
30if (options.debug) console.log(options);
31
Marc Kupietz229eba22022-01-16 17:02:50 +010032if (program.args.length == 0 || program.args[0] == '-') {
33 console.debug("Reading from stdin")
34 stdin = '```\n' + fs.readFileSync(0, 'utf-8') + '```';
35}
Marc Kupietzea6dab22022-01-15 18:29:27 +010036
Marc Kupietz229eba22022-01-16 17:02:50 +010037program.parse(process.argv);