Last active
September 14, 2021 17:51
-
-
Save joawan/a4f47bc828272fcac72b92c885fc6bed to your computer and use it in GitHub Desktop.
Handler with callback to Slack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { WebClient } = require('@slack/web-api'); | |
const wc = new WebClient(process.env.SLACK_TOKEN); | |
const handler = async (event) => { | |
console.log('Received request', event); | |
const { body } = event; | |
if (body.challenge) { | |
return { | |
statusCode: 200, | |
headers: { 'Content-Type': 'text/plain' }, | |
body: body.challenge, | |
}; | |
} | |
// Ignore messages from bot in IM | |
const { channel_type: channelType, bot_id: botId } = body.event; | |
if (channelType === 'im' && botId) { | |
return { statusCode: 200 }; | |
} | |
if (body.event) { | |
const { channel } = body.event; | |
const text = 'Hej Världen'; | |
return wc.chat.postMessage({ channel, text }); | |
} | |
return { statusCode: 200 }; | |
}; | |
module.exports = { handler }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment