feat: 企微回调服务基建(加解密+事件分发,默认关闭)
This commit is contained in:
54
lib/wecom-callback-events.ts
Normal file
54
lib/wecom-callback-events.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
export type WecomCallbackFields = Record<string, string>;
|
||||
|
||||
export type WecomCallbackEventResult = {
|
||||
recognized: boolean;
|
||||
action: string;
|
||||
taskId: string;
|
||||
btnKey: string;
|
||||
operator: string;
|
||||
};
|
||||
|
||||
export function parseWecomCallbackXml(xml: string): WecomCallbackFields {
|
||||
const fields: WecomCallbackFields = {};
|
||||
const source = String(xml ?? "");
|
||||
const pattern =
|
||||
/<([A-Za-z][A-Za-z0-9_]*)>(?:<!\[CDATA\[([\s\S]*?)\]\]>|([^<]*))<\/\1>/g;
|
||||
let match: RegExpExecArray | null;
|
||||
while ((match = pattern.exec(source)) !== null) {
|
||||
const tag = match[1];
|
||||
const value = match[2] ?? match[3] ?? "";
|
||||
if (!(tag in fields)) fields[tag] = value.trim();
|
||||
}
|
||||
return fields;
|
||||
}
|
||||
|
||||
export function extractWecomEncryptField(body: string): string {
|
||||
const fields = parseWecomCallbackXml(body);
|
||||
return fields.Encrypt ?? "";
|
||||
}
|
||||
|
||||
export function handleWecomCallbackEvent(
|
||||
fields: WecomCallbackFields,
|
||||
): WecomCallbackEventResult {
|
||||
const event = fields.Event ?? "";
|
||||
const taskId = fields.TaskId ?? "";
|
||||
const btnKey = fields.BtnKey ?? "";
|
||||
const operator = fields.FromUserName ?? "";
|
||||
|
||||
if (fields.MsgType === "event" && event === "template_card_event") {
|
||||
if (btnKey === "mark_handled") {
|
||||
console.info(
|
||||
"[KOC LOOP] wecom card ack",
|
||||
{ taskId, operator },
|
||||
);
|
||||
return { recognized: true, action: "card_ack", taskId, btnKey, operator };
|
||||
}
|
||||
return { recognized: true, action: "ignored", taskId, btnKey, operator };
|
||||
}
|
||||
|
||||
console.info("[KOC LOOP] wecom callback event ignored", {
|
||||
msgType: fields.MsgType ?? "",
|
||||
event,
|
||||
});
|
||||
return { recognized: false, action: "ignored", taskId, btnKey, operator };
|
||||
}
|
||||
Reference in New Issue
Block a user