Slack Bolt:将命令回调迁移至独立文件的实现方法
Slack应用命令回调模块化迁移步骤
步骤1:提取回调函数到独立文件
在项目根目录下创建commands.ts文件,把原app.ts中命令回调的业务逻辑完整迁移过来,封装成命名函数并导出:
// commands.ts export const handleMyCommand = async ({ ack, command, response }) => { // 原有的业务逻辑代码 // 原有的业务逻辑代码 // 原有的业务逻辑代码 }
步骤2:在app.ts中导入并使用该函数
修改app.ts,导入刚才导出的函数,替换原来的匿名回调:
// app.ts import { handleMyCommand } from './commands.ts' slackApp.command('command', handleMyCommand)
额外提示(TypeScript环境适用)
如果是TypeScript项目,建议给回调参数加上类型定义,保证类型安全,需要先从Slack Bolt库导入对应类型:
// commands.ts import { CommandMiddlewareArgs } from '@slack/bolt' export const handleMyCommand = async ({ ack, command, response }: CommandMiddlewareArgs) => { // 业务逻辑代码 }
内容的提问来源于stack exchange,提问作者Ataxote
相关产品推荐
相关产品推荐

