refactor(telegram): unify chat metadata parsing

This commit is contained in:
Ayaan Zaidi
2026-03-28 09:45:44 +05:30
parent 12488f45c2
commit 9905f39e9d
4 changed files with 14 additions and 20 deletions

View File

@@ -19,6 +19,7 @@ import { buildTelegramInboundContextPayload } from "./bot-message-context.sessio
import type { BuildTelegramMessageContextParams } from "./bot-message-context.types.js";
import {
buildTypingThreadParams,
extractTelegramForumFlag,
resolveTelegramForumFlag,
resolveTelegramThreadSpec,
} from "./bot/helpers.js";
@@ -83,7 +84,7 @@ export const buildTelegramMessageContext = async ({
chatId,
chatType: msg.chat.type,
isGroup,
isForum: (msg.chat as { is_forum?: boolean }).is_forum,
isForum: extractTelegramForumFlag(msg.chat),
getChat: getChatApi,
});
const threadSpec = resolveTelegramThreadSpec({

View File

@@ -65,6 +65,7 @@ import {
buildTelegramThreadParams,
buildSenderName,
buildTelegramGroupFrom,
extractTelegramForumFlag,
resolveTelegramForumFlag,
resolveTelegramGroupAllowFromContext,
resolveTelegramThreadSpec,
@@ -190,7 +191,7 @@ async function resolveTelegramCommandAuth(params: {
chatId,
chatType: msg.chat.type,
isGroup,
isForum: (msg.chat as { is_forum?: boolean }).is_forum,
isForum: extractTelegramForumFlag(msg.chat),
getChat,
});
const threadSpec = resolveTelegramThreadSpec({

View File

@@ -18,7 +18,7 @@ export type TelegramThreadSpec = {
scope: "dm" | "forum" | "none";
};
function extractTelegramForumFlag(value: unknown): boolean | undefined {
export function extractTelegramForumFlag(value: unknown): boolean | undefined {
if (!value || typeof value !== "object") {
return undefined;
}

View File

@@ -1,4 +1,5 @@
import { DEFAULT_EMOJIS, type StatusReactionEmojis } from "openclaw/plugin-sdk/channel-feedback";
import type { TelegramChatDetails, TelegramGetChat } from "./bot/types.js";
type StatusReactionEmojiKey = keyof Required<StatusReactionEmojis>;
@@ -156,35 +157,26 @@ export function isTelegramSupportedReactionEmoji(emoji: string): boolean {
}
export function extractTelegramAllowedEmojiReactions(
chat: unknown,
chat: TelegramChatDetails | null | undefined,
): Set<string> | null | undefined {
if (!chat || typeof chat !== "object") {
if (!chat) {
return undefined;
}
if (!Object.prototype.hasOwnProperty.call(chat, "available_reactions")) {
const availableReactions = chat.available_reactions;
if (typeof availableReactions === "undefined") {
return undefined;
}
const availableReactions = (chat as { available_reactions?: unknown }).available_reactions;
if (availableReactions == null) {
// Explicitly omitted/null => all emoji reactions are allowed in this chat.
return null;
}
if (!Array.isArray(availableReactions)) {
return new Set<string>();
}
const allowed = new Set<string>();
for (const reaction of availableReactions) {
if (!reaction || typeof reaction !== "object") {
if (reaction.type !== "emoji") {
continue;
}
const typedReaction = reaction as { type?: unknown; emoji?: unknown };
if (typedReaction.type !== "emoji" || typeof typedReaction.emoji !== "string") {
continue;
}
const emoji = typedReaction.emoji.trim();
const emoji = reaction.emoji.trim();
if (emoji) {
allowed.add(emoji);
}
@@ -193,9 +185,9 @@ export function extractTelegramAllowedEmojiReactions(
}
export async function resolveTelegramAllowedEmojiReactions(params: {
chat: unknown;
chat: TelegramChatDetails | null | undefined;
chatId: string | number;
getChat?: (chatId: string | number) => Promise<unknown>;
getChat?: TelegramGetChat;
}): Promise<Set<string> | null> {
const fromMessage = extractTelegramAllowedEmojiReactions(params.chat);
if (fromMessage !== undefined) {