You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 08:10:25