这个包提供一个服务,用于与 Slack 机器人交互,以及一些辅助模型,你可以使用这些模型来接收和发送数据到 Slack。
SlashCommand
是一个模型,它可以封装当你设置服务器以接受斜杠命令时,从 Slack 收到的 POST 请求体。
router.post(SlashCommand.self, at: "queue") { (req, slashCommand) in
// Do what you need to do with incoming information
}
如果你想,你可以返回一个 String
,或者如果你想要更多控制,你可以返回一个 SlashCommandResponse
router.post(SlashCommand.self, at: "queue") { (req, slashCommand) -> Future<SlashCommandResponse> in
// Do what you need to do with incoming information
return SlashCommandResponse(responseType: .ephemeral, text: "Your slash command works!")
}
默认的响应类型是 .ephemeral
。
注册你的客户端
if let verificationToken = Environment.get("SLACK_VERIFICATION_TOKEN"),
let botToken = Environment.get("SLACK_BOT_TOKEN") {
let slackService = Slack(verificationToken: verificationToken, slackBotToken: botToken)
services.register(slackService, as: SlackProvider.self)
}
创建客户端
let slackClient = try req.make(SlackProvider.self)
打开 IM 频道
return try slackClient.openIM(with: userId, on: req)
发送消息到聊天
return try slackClient.send(message: "The restroom is ready for you", to: channelId, on: req)