Skip to content

Commit

Permalink
chore: more logs
Browse files Browse the repository at this point in the history
  • Loading branch information
zerosoul committed May 26, 2023
1 parent d511cec commit f84bbd6
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions pages/api/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const sendMessageToBot = async (url: string, message: string) => {
}

const handler = async (req: Request): Promise<Response> => {
console.log("bot.ts handler", req.method, VOCECHAT_BOT_SECRET, VOCECHAT_BOT_UID, VOCECHAT_ORIGIN);
console.log("bot: from webhook push", req.method, VOCECHAT_BOT_UID, VOCECHAT_ORIGIN, VOCECHAT_BOT_SECRET.slice(-5));
let _url = `${VOCECHAT_ORIGIN}/api/bot/`;
try {
switch (req.method) {
Expand All @@ -34,12 +34,14 @@ const handler = async (req: Request): Promise<Response> => {
const mentions = (data.detail.properties ?? {}).mentions ?? [];
// 机器人本人发的消息不处理
if (data.from_uid == VOCECHAT_BOT_UID) {
console.log("bot: ignore sent by bot self");
return new Response(`ignore sent by bot self`, { status: 200 });
}
// 群里没at 此bot的消息不处理
if ('gid' in data.target) {
const mentionedAtGroup = mentions.includes(+VOCECHAT_BOT_UID);
const mentionedAtGroup = mentions.some(m => m == VOCECHAT_BOT_UID);
if (!mentionedAtGroup) {
console.log("bot: ignore not mention at group");
return new Response(`ignore not mention at group`, { status: 200 });
}
}
Expand All @@ -49,7 +51,7 @@ const handler = async (req: Request): Promise<Response> => {
} else {
_url += `send_to_user/${data.from_uid}`;
}
console.log("start ChatGPT");
console.log("bot: start req ChatGPT");
const res = await fetch(`${OPENAI_API_HOST}/v1/chat/completions`, {
headers: {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -77,19 +79,20 @@ const handler = async (req: Request): Promise<Response> => {
});
const gptRespData = await res.json();
const [{ message: { content } }] = gptRespData.choices;
console.log("gpt resp", content);
console.log("bot: gpt resp", content);
// 通过bot给vocechat发消息
sendMessageToBot(_url, content);
return new Response(`OK`, { status: 200 });
}
// break;

default:
console.log("bot: handler default", req.method);
return new Response(`${req.method}: bot resp`, { status: 200 });
// break;
}
} catch (error) {
console.error(error);
console.error("bot: error", error);
// 通过bot给vocechat发消息
sendMessageToBot(_url, "**Something Error!**");
return new Response(`Error`, { status: 200 });
Expand Down

0 comments on commit f84bbd6

Please sign in to comment.