Skip to content

Instantly share code, notes, and snippets.

@ekellener
Last active February 28, 2023 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ekellener/6e825c23179e27955fd2d0c2443320ac to your computer and use it in GitHub Desktop.
Save ekellener/6e825c23179e27955fd2d0c2443320ac to your computer and use it in GitHub Desktop.
Pipedream Action for Slack Bolt app
/*
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