Files
openclaw/extensions/telegram/src/inline-buttons.ts
xingzhou 3217165be7 fix(telegram): preserve inline buttons for empty capabilities (#96468)
Merged via squash.

Prepared head SHA: 5e55b5dd30
Co-authored-by: zhangguiping-xydt <275915537+zhangguiping-xydt@users.noreply.github.com>
Co-authored-by: vincentkoc <25068+vincentkoc@users.noreply.github.com>
Reviewed-by: @vincentkoc
2026-06-25 00:09:45 +08:00

89 lines
2.9 KiB
TypeScript

// Telegram plugin module implements inline buttons behavior.
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
import type { TelegramInlineButtonsScope } from "openclaw/plugin-sdk/config-contracts";
import {
normalizeLowercaseStringOrEmpty,
normalizeOptionalLowercaseString,
} from "openclaw/plugin-sdk/string-coerce-runtime";
import { inspectTelegramAccount } from "./account-inspect.js";
import { listTelegramAccountIds } from "./accounts.js";
const DEFAULT_INLINE_BUTTONS_SCOPE: TelegramInlineButtonsScope = "allowlist";
function normalizeInlineButtonsScope(value: unknown): TelegramInlineButtonsScope | undefined {
const trimmed = normalizeOptionalLowercaseString(value);
if (!trimmed) {
return undefined;
}
if (
trimmed === "off" ||
trimmed === "dm" ||
trimmed === "group" ||
trimmed === "all" ||
trimmed === "allowlist"
) {
return trimmed as TelegramInlineButtonsScope;
}
return undefined;
}
function readInlineButtonsCapability(value: unknown): unknown {
if (!value || Array.isArray(value) || typeof value !== "object" || !("inlineButtons" in value)) {
return undefined;
}
return value.inlineButtons;
}
export function resolveTelegramInlineButtonsConfigScope(
capabilities: unknown,
): TelegramInlineButtonsScope | undefined {
return normalizeInlineButtonsScope(readInlineButtonsCapability(capabilities));
}
export function resolveTelegramInlineButtonsScopeFromCapabilities(
capabilities: unknown,
): TelegramInlineButtonsScope {
if (!capabilities) {
return DEFAULT_INLINE_BUTTONS_SCOPE;
}
if (Array.isArray(capabilities)) {
if (capabilities.length === 0) {
return DEFAULT_INLINE_BUTTONS_SCOPE;
}
const enabled = capabilities.some(
(entry) => normalizeLowercaseStringOrEmpty(String(entry)) === "inlinebuttons",
);
return enabled ? "all" : "off";
}
if (typeof capabilities === "object") {
return resolveTelegramInlineButtonsConfigScope(capabilities) ?? DEFAULT_INLINE_BUTTONS_SCOPE;
}
return DEFAULT_INLINE_BUTTONS_SCOPE;
}
export function resolveTelegramInlineButtonsScope(params: {
cfg: OpenClawConfig;
accountId?: string | null;
}): TelegramInlineButtonsScope {
const account = inspectTelegramAccount({ cfg: params.cfg, accountId: params.accountId });
return resolveTelegramInlineButtonsScopeFromCapabilities(account.config.capabilities);
}
export function isTelegramInlineButtonsEnabled(params: {
cfg: OpenClawConfig;
accountId?: string | null;
}): boolean {
if (params.accountId) {
return resolveTelegramInlineButtonsScope(params) !== "off";
}
const accountIds = listTelegramAccountIds(params.cfg);
if (accountIds.length === 0) {
return resolveTelegramInlineButtonsScope(params) !== "off";
}
return accountIds.some(
(accountId) => resolveTelegramInlineButtonsScope({ cfg: params.cfg, accountId }) !== "off",
);
}
export { resolveTelegramTargetChatType } from "./targets.js";