Last active
February 28, 2023 15:38
-
-
Save ekellener/6e825c23179e27955fd2d0c2443320ac to your computer and use it in GitHub Desktop.
Pipedream Action for Slack Bolt app
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
/* | |
Example Template to create a Slack Application using the Bolt framework within Pipedream. | |
input: to this function is a slack request: passed via steps.trigger.event.body to the payload variable | |
Note: it's still a good idea to have the initial request as a Slack source, which acts as a queue. | |
Used examples below on integrating a non-blocking Bolt | |
Requires the use of processEvent(slackEvent) instead of using .start() | |
https://github.com/slackapi/bolt-js/issues/884 | |
https://levelup.gitconnected.com/creating-a-slack-bot-using-netlify-functions-465d2a981686 | |
*/ | |
import pkg from '@slack/bolt'; | |
// Entry point | |
export default defineComponent({ | |
'name': 'Template Slack Bolt Action', | |
'description': 'Template for implementing a Slack Bolt App within Pipedream', | |
'type': 'action', | |
'key': 'evolution-Slack-Bolt_Action', | |
'version': '0.0.1', | |
async run({ steps, $ }) { | |
const { App, ExpressReceiver } = pkg; | |
// Utility functions | |
function isUrlVerificationRequest(payload) { | |
if (payload && payload.type && payload.type === "url_verification") { | |
return true; | |
} | |
return false; | |
} | |
// Format for generating a receiver event | |
function generateReceiverEvent(payload){ | |
return { | |
body: payload, | |
ack: async (response) => { | |
return { | |
statusCode: 200, | |
body: response ?? "" | |
}; | |
} | |
}; | |
} | |
// Use Express Receiver | |
const expressReceiver = new ExpressReceiver({ | |
signingSecret: process.env.TEST_SLACK_SIGNING_SECRET, | |
processBeforeResponse: true | |
}); | |
const app = new App({ | |
token: process.env.TEST_SLACK_BOT_TOKEN, | |
clientId: process.env.TEST_SLACK_CLIENT_ID, | |
signingSecret: process.env.TEST_SLACK_SIGNING_SECRET, | |
receiver: expressReceiver | |
}); | |
/*************************************** */ | |
// register listeners | |
// Example that listens for the keyword hello in the slack channel. | |
app.message('hello', async ({ message, say }) => { | |
// say() sends a message to the channel where the event was triggered | |
await say(`Hey there <@${message.user}>!`); | |
}); | |
/*************************************** */ | |
// Build payload from Event that triggered invocation | |
const payload = steps.trigger.event.body; | |
// Respond if Slack needs to verify the URL | |
if(isUrlVerificationRequest(payload)) { | |
return { | |
statusCode: 200, | |
body: payload?.challenge | |
}; | |
} | |
const slackEvent = generateReceiverEvent(payload); | |
await app.processEvent(slackEvent); | |
return { | |
statusCode: 200, | |
body: "" | |
}; | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment