Last active
June 6, 2020 20:06
-
-
Save sweetyclem/dc0f0d6dc03db17336dfb85978385082 to your computer and use it in GitHub Desktop.
This file contains 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 { createEventAdapter } = require('@slack/events-api') | |
const slackSigningSecret = process.env.SLACK_SIGNING_SECRET | |
const slackEvents = createEventAdapter(slackSigningSecret) | |
const { WebClient } = require('@slack/web-api') | |
const token = process.env.SLACK_BOT_TOKEN | |
const web = new WebClient(token) | |
const subjects = require('./elements/subjects.json') | |
function listenForEvents(app) { | |
app.use('/events', slackEvents.requestListener()) | |
slackEvents.on('app_mention', (event) => { | |
console.log(`Received an app_mention event from user ${event.user} in channel ${event.channel}`) | |
respondToEvent(event.channel) | |
}) | |
// All errors in listeners are caught here. If this weren't caught, the program would terminate. | |
slackEvents.on('error', (error) => { | |
console.log(`error: ${error}`) | |
}) | |
} | |
async function respondToEvent(channelId) { | |
try { | |
await web.chat.postMessage({ | |
channel: channelId, | |
text: '', | |
attachments: [subjects] | |
}) | |
console.log('Message posted!') | |
} catch (error) { | |
console.log(error) | |
} | |
} | |
module.exports.listenForEvents = listenForEvents | |
module.exports.respondToEvent = respondToEvent |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment