How to split listeners into different js files in bolt js?
See original GitHub issueDescription
Describe your issue here.
What type of issue is this? (place an x
in one of the [ ]
)
- bug
- enhancement (feature request)
- question
- documentation related
- example code related
- testing related
- discussion
Requirements (place an x
in each of the [ ]
)
- I’ve read and understood the Contributing guidelines and have done my best effort to follow them.
- I’ve read and agree to the Code of Conduct.
- I’ve searched for any related issues and avoided creating a duplicate issue.
Bug Report
Filling out the following details about bugs will help us solve your issue sooner.
Reproducible in:
package version:
node version:
OS version(s):
Steps to reproduce:
Expected result:
What you expected to happen
Actual result:
What actually happened
Attachments:
Logs, screenshots, screencast, sample project, funny gif, etc.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Integrating a Bolt app into another server or framework ...
I would like to use bolt as an express middleware similar to how the @slack/events-api work. app.use('/slack/events', slackEvents.
Read more >App interface and configuration - Slack | Bolt for JavaScript
This guide is intended to detail the Bolt interface–including listeners and their arguments, initialization options, and errors. It may be helpful to first ......
Read more >How can I split a javascript application into multiple files?
The basic idea is that the individual JS files add to the same module with code like this: var MODULE = (function (my)...
Read more >Build a Slackbot in Node.js with Slack's Bolt API
Develop and customize your own Slackbot using the Bolt API, ... Head into the app.js file we created earlier and add the code...
Read more >Build a Bolt App Unit | Salesforce Trailhead
For now, you use the message() listener to listen and respond to messages. Head over to your Glitch project. Add the following to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’ve found the best way is to have a collection of handlers that are fetched and handled in the middleware:
A
RequestHandler
class that maps a string that represents an event to the method that needs to handle it. It’s very simplified here, but there are a lot of things that could be stored here, such as roles the user needs to have to execute the function:A
RequestHandlerCollection
class that basically acts as a bus. Also very simplified, just returns the handler, but it could essentially call the handler and do some other things, such as logging the names of handlers that are being called (can be super helpful when using Socket Mode), etc.:An actual handler function that receives a request and handles it:
Add it to the handler collection:
The Bolt middleware whose responsibility is to fetch the name of the intended handler, get the handler, do any logic necessary around the handler, such as checking roles, etc. And finally, calling the
handle()
function, passing in any injections from the application level:Adding this middleware to
app.action
for basically any block actions request.It’s verbose, but it provides a lot of flexibility, structure, and reusable code, especially in larger applications, or projects where multiple applications live (such as all of our Slack apps at MacPaw, one codebase, multiple apps, Socket Mode).
I also do a lot of other stuff in middleware before the request hits the handler:
persistedData
.Hi @AlishaK30, thanks for asking the question! I think there are several ways to organize your modules but one simple way could be like this. I know it’s not a fancy way but it works.
We’d love to know how the folks in the community are organizing listeners!