Avatar

z233

4 public vals
Joined January 15, 2023
1
2
3
4
5
6
7
8
9
10
11
12
13
14
export function expressApiTest(req: express.Request, res: express.Response) {
function listAllMembers(obj) {
const members = new Set();
let currentObject = obj;
while (currentObject !== null) {
Object.getOwnPropertyNames(currentObject).forEach((name) => {
members.add(name);
});
currentObject = Object.getPrototypeOf(currentObject);
}
return Array.from(members);
}
return res.json(req.options);
}
1
2
3
4
export let feishuGetInteractiveMessage = ({ card }) => ({
card,
msg_type: "interactive",
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { feishuGetInteractiveMessage } from "https://esm.town/v/z233/feishuGetInteractiveMessage";
import { bebopBotFeishuWebhookUrl } from "https://esm.town/v/z233/bebopBotFeishuWebhookUrl";
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
import { createFeishuCardActionElementBuilder } from "https://esm.town/v/z233/createFeishuCardActionElementBuilder";
import { createFeishuCardBuilder } from "https://esm.town/v/z233/createFeishuCardBuilder";
import { bebopBotSecretToken } from "https://esm.town/v/z233/bebopBotSecretToken";
export async function bebopBotWebhook(
req: express.Request,
res: express.Response
) {
const requestSecretToken =
req?.options?.headers?.["x-telegram-bot-api-secret-token"];
if (requestSecretToken !== bebopBotSecretToken) {
res.status(403).send("Access denied");
return;
}
const { channel_post: channelPost } = req.body;
if (!channelPost) {
res.send("ok");
return;
}
const {
text,
entities: textEntities,
caption,
caption_entities: captionEntities,
} = channelPost;
const entities = textEntities || captionEntities;
let textToSend = text || caption;
let accumOffset = 0;
for (const entry of entities) {
if (entry.type === "text_link") {
const { offset, length, url } = entry;
const entryStart = offset + accumOffset;
const mdUrl = getMarkdownLink(
textToSend.substring(entryStart, entryStart + length),
url
);
textToSend =
textToSend.substring(0, entryStart) +
mdUrl +
textToSend.substring(entryStart + length);
accumOffset = mdUrl.length - length;
}
}
const { forward_from_chat: fowardFromChat, reply_markup: replyMarkup } =
channelPost;
const cardBuilder = await createFeishuCardBuilder();
if (fowardFromChat) {
cardBuilder.setHeader({
template: "blue",
title: {
content: `Forwarded from ${fowardFromChat.title} @${fowardFromChat.username}`,
tag: "plain_text",
},
});
}
cardBuilder.addMarkdownElement(textToSend);
if (replyMarkup && replyMarkup.inline_keyboard?.length) {
const actionBuilder =
await createFeishuCardActionElementBuilder();
const { inline_keyboard: inlineKeyboard } = replyMarkup;
const markups = inlineKeyboard.flat();
markups.forEach((m) =>
actionBuilder.addAction({
tag: "button",
text: {
tag: "plain_text",
content: m.text,
},
type: "default",
url: m.url,
})
);
cardBuilder.addActionElement(actionBuilder.build());
}
const card = cardBuilder.build();
await fetchJSON(
bebopBotFeishuWebhookUrl,
{
method: "POST",
body: JSON.stringify(
await feishuGetInteractiveMessage({ card })
),
}
);
res.send("ok");
function getMarkdownLink(text, url) {
return `[${text}](${url})`;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { feishuHandleImMessageReceive } from "https://esm.town/v/z233/feishuHandleImMessageReceive";
export let feishuBotEventSubscribe = async (body) => {
if (!body) throw new Error(`Error: body is ${body}`);
const { event_type: eventType, event_id: eventId } = body.header;
try {
switch (body?.header.event_type) {
case "im.message.receive_v1":
await feishuHandleImMessageReceive(body);
break;
default:
break;
}
} catch (e) {
throw e;
}
return body;
};
Next