feat: add map function

This commit is contained in:
Kyle Fang
2023-09-18 17:01:40 +08:00
parent 146c3e5eaf
commit 34c03bd102
2 changed files with 57 additions and 0 deletions

10
src/get.ts Normal file
View File

@@ -0,0 +1,10 @@
export function get(obj: any, path: string): any {
const keys = path.split(/[.\[\]]+/).filter(Boolean);
for (let key of keys) {
if (obj == null || typeof obj !== 'object') {
return undefined;
}
obj = obj[key];
}
return obj;
}

View File

@@ -10,6 +10,7 @@
import { Application, Router } from "@cfworker/web";
import type { Update } from "@grammyjs/types";
import { formatRichMessage, RichMessage } from "./message";
import { get } from "./get";
declare global {
const BOT_TOKEN: string;
@@ -75,6 +76,52 @@ router.post("/t/:webhookId", async (context) => {
context.res.body = { ok: true };
});
router.post("/t/:webhookId/map", async (context) => {
const chat = await TG_GROUPS.get(
`webhook-chat:${context.req.params.webhookId}`
);
if (chat == null) {
context.res.body = { ok: false, error: "chatId not found" };
context.res.status = 404;
return;
}
const input = await context.req.body.json().catch(() => ({}));
const search = context.req.url.searchParams;
function getContent(key: string): string | undefined {
const param = search.get(key);
if (!param) {
return undefined;
}
return param.startsWith("$json") ? get(input, param.slice(6)) : param;
}
const metadataParams = Array.from(search.entries()).filter(([k]) =>
k.startsWith("metadata.")
);
const result: RichMessage = {
event: getContent("event") || "Unknown Event",
text: getContent("text"),
channel: getContent("channel"),
emoji: getContent("emoji"),
notify: search.get("notify") !== "false",
metadata:
metadataParams.length === 0
? undefined
: Object.fromEntries(
metadataParams
.map(([k, v]) => [k.slice(9), v])
.map(([key, value]) => [
key,
value.startsWith("$json") ? get(input, value.slice(6)) : value,
])
),
};
await sendToChat(JSON.parse(chat), formatRichMessage(result), {
parseMode: "HTML",
disableNotification: result.notify === false,
});
context.res.body = { ok: true };
});
new Application().use(router.middleware).listen();
async function processUpdate(update: Update): Promise<void> {