Home > Blockchain >  Slack Bolt, Moving command callback into another file
Slack Bolt, Moving command callback into another file

Time:01-19

I am learning to create a Slack app. I have created an app and it works, however the main app.ts file is quite bloated. I want to go from having code that looks like this

slackApp.command('command', async ({ack,command,response}) = {
 // Do all of the things here 
 // and here
 // and here
})

to something like this

import { MyFunctionHere } from './commands.ts'

slackApp.command('command', async MyFunctionHere)

with another typescript file called something like commands.ts with

export MyFunctionHere = () => {
 // Do all of the things here 
 // and here
 // and here
}

Though I am not sure how to go about moving the anonymous function from the first example into a separate file like the second example.

CodePudding user response:

I was able to solve this.

The resolution I used was to set the type of the const MyFunctionHere to Middleware<SlackCommandMiddlewareArgs> resulting in no compilation errors from TypeScript.

The following is my example code:

app.ts

import { MyFunctionHere } from './commands.ts'

slackApp.command('command', async MyFunctionHere)

command.ts

const MyFunctionHere: Middleware<SlackCommandMiddlewareArgs> = async ({ ack,command,response }) => {

await ack()

}

export = { MyFunctionHere }

Thanks for the comments, I hope this is able to help someone else.

  • Related