From ff0f922a4d02c0bba62c4fda7ba377a8ba330291 Mon Sep 17 00:00:00 2001 From: hanshino Date: Fri, 13 Dec 2024 15:43:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20OpenaiController.r?= =?UTF-8?q?ecordSession=20=E6=96=B9=E6=B3=95=E4=BB=A5=E8=A8=98=E9=8C=84?= =?UTF-8?q?=E5=B0=8D=E8=A9=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/app.js | 1 + .../application/OpenaiController.js | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/app/src/app.js b/app/src/app.js index 620262c..5f546b9 100644 --- a/app/src/app.js +++ b/app/src/app.js @@ -468,6 +468,7 @@ async function App(context) { OrderBased, // 指令分析 CustomerOrderBased, // 自訂指令分析 interactWithBot, // 標記機器人回應 + OpenaiController.recordSession, // 記錄對話 Nothing, // 無符合事件 ]); } diff --git a/app/src/controller/application/OpenaiController.js b/app/src/controller/application/OpenaiController.js index 970e6f1..87b370e 100644 --- a/app/src/controller/application/OpenaiController.js +++ b/app/src/controller/application/OpenaiController.js @@ -52,6 +52,30 @@ exports.naturalLanguageUnderstanding = async function (context, { next }) { await context.replyText(reponseText); }; +/** + * 只記錄對話 + * @param {import("bottender").LineContext} context + */ +exports.recordSession = async function (context, { next }) { + // 只處理文字訊息 + if (!context.event.isText) { + return next; + } + const { text } = context.event.message; + + // 不處理太長的文字訊息 + if (text.length > 100) { + return next; + } + + const sourceType = get(context, "event.source.type"); + const sourceId = get(context, `event.source.${sourceType}Id`); + const displayName = get(context, "event.source.displayName"); + + await recordSession(sourceId, `${displayName}:${text}`); + return next; +}; + /** * 紀錄對話 * @param {String} groupId