feat: make telegram reactions visible to clawdbot

This commit is contained in:
Bohdan Podvirnyi
2026-01-13 21:13:05 +02:00
committed by Peter Steinberger
parent 01c43b0b0c
commit d05c3d0659
7 changed files with 5915 additions and 3 deletions

View File

@@ -17,8 +17,16 @@ import {
resolveChannelGroupPolicy,
resolveChannelGroupRequireMention,
} from "../config/group-policy.js";
import { loadSessionStore, resolveStorePath } from "../config/sessions.js";
import { logVerbose, shouldLogVerbose } from "../globals.js";
import {
loadSessionStore,
resolveStorePath,
updateLastRoute,
} from "../config/sessions.js";
import { danger, logVerbose, shouldLogVerbose } from "../globals.js";
import { recordChannelActivity } from "../infra/channel-activity.js";
import { createDedupeCache } from "../infra/dedupe.js";
import { formatErrorMessage } from "../infra/errors.js";
import { enqueueSystemEvent } from "../infra/system-events.js";
import { getChildLogger } from "../logging.js";
import type { RuntimeEnv } from "../runtime.js";
import { resolveTelegramAccount } from "./accounts.js";
@@ -34,6 +42,12 @@ import {
type TelegramUpdateKeyContext,
} from "./bot-updates.js";
import { resolveTelegramFetch } from "./fetch.js";
import {
readTelegramAllowFromStore,
upsertTelegramPairingRequest,
} from "./pairing-store.js";
import { wasSentByBot } from "./sent-message-cache.js";
import { resolveTelegramVoiceSend } from "./voice.js";
export type TelegramBotOptions = {
token: string;
@@ -59,8 +73,14 @@ export function getTelegramSequentialKey(ctx: {
message?: TelegramMessage;
edited_message?: TelegramMessage;
callback_query?: { message?: TelegramMessage };
message_reaction?: { chat?: { id?: number } };
};
}): string {
// Handle reaction updates
const reaction = ctx.update?.message_reaction;
if (reaction?.chat?.id) {
return `telegram:${reaction.chat.id}`;
}
const msg =
ctx.message ??
ctx.update?.message ??
@@ -291,6 +311,86 @@ export function createTelegramBot(opts: TelegramBotOptions) {
opts,
});
// Handle emoji reactions to messages
bot.on("message_reaction", async (ctx) => {
try {
const reaction = ctx.messageReaction;
if (!reaction) return;
if (shouldSkipUpdate(ctx)) return;
const chatId = reaction.chat.id;
const messageId = reaction.message_id;
const user = reaction.user;
// Resolve reaction notification mode (default: "own")
const reactionMode = telegramCfg.reactionNotifications ?? "own";
if (reactionMode === "off") return;
// For "own" mode, only notify for reactions to bot's messages
if (reactionMode === "own" && !wasSentByBot(chatId, messageId)) {
return;
}
// Detect added reactions
const oldEmojis = new Set(
reaction.old_reaction
.filter(
(r): r is { type: "emoji"; emoji: string } => r.type === "emoji",
)
.map((r) => r.emoji),
);
const addedReactions = reaction.new_reaction
.filter(
(r): r is { type: "emoji"; emoji: string } => r.type === "emoji",
)
.filter((r) => !oldEmojis.has(r.emoji));
if (addedReactions.length === 0) return;
// Build sender label
const senderName = user
? [user.first_name, user.last_name].filter(Boolean).join(" ").trim() ||
user.username
: undefined;
const senderUsername = user?.username ? `@${user.username}` : undefined;
let senderLabel = senderName;
if (senderName && senderUsername) {
senderLabel = `${senderName} (${senderUsername})`;
} else if (!senderName && senderUsername) {
senderLabel = senderUsername;
}
if (!senderLabel && user?.id) {
senderLabel = `id:${user.id}`;
}
senderLabel = senderLabel || "unknown";
// Resolve agent route for session
const isGroup =
reaction.chat.type === "group" || reaction.chat.type === "supergroup";
const route = resolveAgentRoute({
cfg,
channel: "telegram",
accountId: account.accountId,
peer: { kind: isGroup ? "group" : "dm", id: String(chatId) },
});
// Enqueue system event for each added reaction
for (const r of addedReactions) {
const emoji = r.emoji;
const text = `Telegram reaction added: ${emoji} by ${senderLabel} on msg ${messageId}`;
enqueueSystemEvent(text, {
sessionKey: route.sessionKey,
contextKey: `telegram:reaction:add:${chatId}:${messageId}:${user?.id ?? "anon"}:${emoji}`,
});
logVerbose(`telegram: reaction event enqueued: ${text}`);
}
} catch (err) {
runtime.error?.(
danger(`telegram reaction handler failed: ${String(err)}`),
);
}
});
registerTelegramHandlers({
cfg,
accountId: account.accountId,