UNPKG

@gravityforms/gulp-tasks

Version:
56 lines (49 loc) 1.68 kB
const { WebClient } = require( '@slack/web-api' ); const slackClient = new WebClient( process.env.HAL_SLACK_TOKEN ); const errorMessageEmojis = [ 'alert', 'face_with_symbols_on_mouth', 'facetater', 'sob', 'cool-sob', 'trynottocry', 'trudy-cry', 'oof' ]; function getRandomErrorEmoji() { const randomIndex = Math.floor( Math.random() * errorMessageEmojis.length ); return `:${ errorMessageEmojis[ randomIndex ] }:`; } const sendSlackMessage = async ( message = '', messageType = 'success', attachments = [], channel = '' ) => { if ( process.env.GULP_PRODUCTION_MODE !== 'true' ) { console.log( 'Slack message not sent in non-production mode.' ); return; } if ( messageType === 'error' ) { const emoji = getRandomErrorEmoji(); try { const res = await slackClient.chat.postMessage({ channel: channel || process.env.GRAVITY_BUILD_CHANNEL_ID, text: `${ emoji } *Build Error*\n\nI was unable to complete the build process for you <@${ process.env.SLACK_USER_ID }>.\n\n`, mrkdwn: true, attachments: [ { color: "#f00", text: `Error: ${ message }`, }, { color: "#03DBE8", text: `Check out the <${ process.env.GHA_URL }|GitHub Actions Workflow> for more details.`, } ], }); console.log('Slack message sent: ', res.ts); } catch (error) { console.error(error); } } else { try { const res = await slackClient.chat.postMessage({ channel: channel || process.env.GRAVITY_BUILD_CHANNEL_ID, text: message, mrkdwn: true, attachments, }); console.log('Slack message sent: ', res.ts); } catch (error) { console.error(error); } } } module.exports = sendSlackMessage;