-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.js
More file actions
61 lines (49 loc) · 1.66 KB
/
index.js
File metadata and controls
61 lines (49 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const core = require( '@actions/core' );
const { WebClient } = require( '@slack/web-api' );
( async () => {
try {
const botToken = core.getInput( 'bot_token' );
const channel = core.getInput( 'channel' );
const messageId = core.getInput( 'message_id' );
const payload = core.getInput( 'payload' );
const slack = new WebClient( botToken );
if ( ! channel && ! core.getInput( 'channel_id' ) ) {
core.setFailed( `You must provide either a 'channel' or a 'channel_id'.` );
return;
}
const channelId =
core.getInput( 'channel_id' ) || ( await lookUpChannelId( { slack, channel } ) );
if ( ! channelId ) {
core.setFailed( `Slack channel ${ channel } could not be found.` );
return;
}
const apiMethod = Boolean( messageId ) ? 'update' : 'postMessage';
const args = JSON.parse( payload );
args.channel = channelId;
if ( messageId ) {
args.ts = messageId;
}
// Send the message.
const response = await slack.chat[ apiMethod ]( args );
core.setOutput( 'message_id', response.ts );
} catch ( error ) {
core.setFailed( error );
}
} )();
async function lookUpChannelId( { slack, channel } ) {
let result;
const formattedChannel = channel.replace( /[#@]/g, '' );
// Async iteration is similar to a simple for loop.
// Use only the first two parameters to get an async iterator.
for await ( const page of slack.paginate( 'conversations.list', {
types: 'public_channel, private_channel',
} ) ) {
// You can inspect each page, find your result, and stop the loop with a `break` statement
const match = page.channels.find( ( c ) => c.name === formattedChannel );
if ( match ) {
result = match.id;
break;
}
}
return result;
}