接入流程:
注册slack账号
注册地址:https://slack.com/
不分是否是国区,尽可能用谷歌邮箱
安装Claude应用
安装地址:https://www.anthropic.com/claude-in-slack
开通slack高级功能
有的账户有免费试用,直接免费试用即可,后续不用续费
新建频道拉Claude进频道
配置Slack 机器人
参考github地址:https://github.com/bincooo/claude-api
新建云函数并替换三个参数发布云函数
laf.run
和 laf.dev
均可用
记得先安装NPM包:claude-api-slack
代码如下:
import cloud from '@lafjs/cloud'
export default async function (ctx: FunctionContext) {
const { question, conversationId } = ctx.query
return await askCluadeAPi(question, conversationId)
}
async function askCluadeAPi(question, conversationId) {
const token = ''
const bot = ''
const chatId = ""
const { Authenticator } = await import('claude-api-slack')
let claudeClient = cloud.shared.get('claudeClient')
if (!claudeClient) {
claudeClient = new Authenticator(token, bot)
cloud.shared.set('claudeClient', claudeClient)
}
const channel = await claudeClient.newChannel(chatId)
let result
if (conversationId) {
result = await claudeClient.sendMessage({
text: question,
channel,
conversationId,
onMessage: (originalMessage) => {
console.log("loading", originalMessage)
}
})
} else {
result = await claudeClient.sendMessage({
text: question,
channel,
onMessage: (originalMessage) => {
console.log("loading", originalMessage)
}
})
}
console.log("success", result)
return {
code: 0,
msg: result.text,
conversationId: result.conversationId
}
}
调用测试
get请求
question
问题
conversationId
上下文