Skip to content

Instantly share code, notes, and snippets.

@lidaobing
Created February 13, 2023 06:52
Show Gist options
  • Save lidaobing/ec540188f80612a89f70c2741f497f06 to your computer and use it in GitHub Desktop.
Save lidaobing/ec540188f80612a89f70c2741f497f06 to your computer and use it in GitHub Desktop.
// 部署流程请参考: https://github.com/bestony/ChatGPT-Feishu
// @see https://docs.aircode.io/guide/functions/
const aircode = require('aircode');
const lark = require('@larksuiteoapi/node-sdk');
const EventDB = aircode.db.table('event');
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const client = new lark.Client({
appId: process.env.APPID,
appSecret: process.env.SECRET,
disableTokenCache: false
});
// 回复消息
async function reply(messageId, content) {
return await client.im.message.reply({
path: {
message_id: messageId,
},
data: {
content: JSON.stringify({
"text": content
}),
msg_type: 'text',
}
})
}
// 通过 OpenAI API 获取回复
async function getOpenAIReply(content) {
const response = await openai.createImage({
prompt: content.trim(),
n: 1,
size:"1024x1024"
})
// 去除多余的换行
return response.data.data[0].url;
}
module.exports = async function (params, context) {
// 处理飞书开放平台的服务端校验
if (params.type == "url_verification") {
return {
challenge: params.challenge
}
}
// 处理飞书开放平台的事件回调
if (params.header.event_type = "im.message.receive_v1") {
let eventId = params.header.event_id;
let messageId = params.event.message.message_id;
// 对于同一个事件,只处理一次
const count = await EventDB.where({ event_id: eventId }).count();
if (count != 0) {
return { code: 1 }
}
await EventDB.save({event_id: eventId})
// 私聊直接回复
if (params.event.message.chat_type == "p2p") {
// 不是文本消息,不处理
if (params.event.message.message_type != "text") {
await reply(messageId, "暂不支持其他类型的提问")
}
// 是文本消息,直接回复
const userInput = JSON.parse(params.event.message.content);
const openaiResponse = await getOpenAIReply(userInput.text)
await reply(messageId, openaiResponse)
}
// 群聊,需要 @ 机器人
if (params.event.message.chat_type == "group") {
// 这是日常群沟通,不用管
if (!params.event.message.mentions || params.event.message.mentions.length == 0) {
return { "code": 0 }
}
// 没有 mention 机器人,则退出。
if (params.event.message.mentions[0].name != process.env.BOTNAME) {
return { "code": 0 }
}
const userInput = JSON.parse(params.event.message.content);
const question = userInput.text.replace("@_user_1", "");
const openaiResponse = await getOpenAIReply(question)
await reply(messageId, openaiResponse)
return { "code": 0 }
}
}
return {
code:2
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment