feat: added capability for clawdbot to react

This commit is contained in:
Bohdan Podvirnyi
2026-01-13 21:34:40 +02:00
committed by Peter Steinberger
parent d05c3d0659
commit 0e1dcf9cb4
12 changed files with 503 additions and 56 deletions

View File

@@ -2244,14 +2244,13 @@ describe("createTelegramBot", () => {
expect(reactionHandler).toBeDefined();
});
it("enqueues system event for reaction on bot message", async () => {
it("enqueues system event for reaction", async () => {
onSpy.mockReset();
enqueueSystemEvent.mockReset();
wasSentByBot.mockReturnValue(true);
loadConfig.mockReturnValue({
channels: {
telegram: { dmPolicy: "open", reactionNotifications: "own" },
telegram: { dmPolicy: "open", reactionNotifications: "all" },
},
});
@@ -2312,37 +2311,6 @@ describe("createTelegramBot", () => {
expect(enqueueSystemEvent).not.toHaveBeenCalled();
});
it("skips reaction in own mode when message was not sent by bot", async () => {
onSpy.mockReset();
enqueueSystemEvent.mockReset();
wasSentByBot.mockReturnValue(false);
loadConfig.mockReturnValue({
channels: {
telegram: { dmPolicy: "open", reactionNotifications: "own" },
},
});
createTelegramBot({ token: "tok" });
const handler = getOnHandler("message_reaction") as (
ctx: Record<string, unknown>,
) => Promise<void>;
await handler({
update: { update_id: 502 },
messageReaction: {
chat: { id: 1234, type: "private" },
message_id: 99,
user: { id: 9, first_name: "Ada" },
date: 1736380800,
old_reaction: [],
new_reaction: [{ type: "emoji", emoji: "👍" }],
},
});
expect(enqueueSystemEvent).not.toHaveBeenCalled();
});
it("allows reaction in all mode regardless of message sender", async () => {
onSpy.mockReset();
enqueueSystemEvent.mockReset();
@@ -2381,11 +2349,10 @@ describe("createTelegramBot", () => {
it("skips reaction removal (only processes added reactions)", async () => {
onSpy.mockReset();
enqueueSystemEvent.mockReset();
wasSentByBot.mockReturnValue(true);
loadConfig.mockReturnValue({
channels: {
telegram: { dmPolicy: "open", reactionNotifications: "own" },
telegram: { dmPolicy: "open", reactionNotifications: "all" },
},
});
@@ -2408,4 +2375,120 @@ describe("createTelegramBot", () => {
expect(enqueueSystemEvent).not.toHaveBeenCalled();
});
it("uses correct session key for forum group reactions with topic", async () => {
onSpy.mockReset();
enqueueSystemEvent.mockReset();
loadConfig.mockReturnValue({
channels: {
telegram: { dmPolicy: "open", reactionNotifications: "all" },
},
});
createTelegramBot({ token: "tok" });
const handler = getOnHandler("message_reaction") as (
ctx: Record<string, unknown>,
) => Promise<void>;
await handler({
update: { update_id: 505 },
messageReaction: {
chat: { id: 5678, type: "supergroup", is_forum: true },
message_id: 100,
message_thread_id: 42,
user: { id: 10, first_name: "Bob", username: "bob_user" },
date: 1736380800,
old_reaction: [],
new_reaction: [{ type: "emoji", emoji: "🔥" }],
},
});
expect(enqueueSystemEvent).toHaveBeenCalledTimes(1);
expect(enqueueSystemEvent).toHaveBeenCalledWith(
"Telegram reaction added: 🔥 by Bob (@bob_user) on msg 100",
expect.objectContaining({
sessionKey: expect.stringContaining("telegram:group:5678:topic:42"),
contextKey: expect.stringContaining("telegram:reaction:add:5678:100:10"),
}),
);
});
it("uses correct session key for forum group reactions in general topic", async () => {
onSpy.mockReset();
enqueueSystemEvent.mockReset();
loadConfig.mockReturnValue({
channels: {
telegram: { dmPolicy: "open", reactionNotifications: "all" },
},
});
createTelegramBot({ token: "tok" });
const handler = getOnHandler("message_reaction") as (
ctx: Record<string, unknown>,
) => Promise<void>;
await handler({
update: { update_id: 506 },
messageReaction: {
chat: { id: 5678, type: "supergroup", is_forum: true },
message_id: 101,
// No message_thread_id - should default to general topic (1)
user: { id: 10, first_name: "Bob" },
date: 1736380800,
old_reaction: [],
new_reaction: [{ type: "emoji", emoji: "👀" }],
},
});
expect(enqueueSystemEvent).toHaveBeenCalledTimes(1);
expect(enqueueSystemEvent).toHaveBeenCalledWith(
"Telegram reaction added: 👀 by Bob on msg 101",
expect.objectContaining({
sessionKey: expect.stringContaining("telegram:group:5678:topic:1"),
contextKey: expect.stringContaining("telegram:reaction:add:5678:101:10"),
}),
);
});
it("uses correct session key for regular group reactions without topic", async () => {
onSpy.mockReset();
enqueueSystemEvent.mockReset();
loadConfig.mockReturnValue({
channels: {
telegram: { dmPolicy: "open", reactionNotifications: "all" },
},
});
createTelegramBot({ token: "tok" });
const handler = getOnHandler("message_reaction") as (
ctx: Record<string, unknown>,
) => Promise<void>;
await handler({
update: { update_id: 507 },
messageReaction: {
chat: { id: 9999, type: "group" },
message_id: 200,
user: { id: 11, first_name: "Charlie" },
date: 1736380800,
old_reaction: [],
new_reaction: [{ type: "emoji", emoji: "❤️" }],
},
});
expect(enqueueSystemEvent).toHaveBeenCalledTimes(1);
expect(enqueueSystemEvent).toHaveBeenCalledWith(
"Telegram reaction added: ❤️ by Charlie on msg 200",
expect.objectContaining({
sessionKey: expect.stringContaining("telegram:group:9999"),
contextKey: expect.stringContaining("telegram:reaction:add:9999:200:11"),
}),
);
// Verify session key does NOT contain :topic:
const sessionKey = enqueueSystemEvent.mock.calls[0][1].sessionKey;
expect(sessionKey).not.toContain(":topic:");
});
});

View File

@@ -28,9 +28,14 @@ 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 { resolveAgentRoute } from "../routing/resolve-route.js";
import type { RuntimeEnv } from "../runtime.js";
import { resolveTelegramAccount } from "./accounts.js";
import { resolveTelegramForumThreadId, resolveTelegramStreamMode } from "./bot/helpers.js";
import {
buildTelegramGroupPeerId,
resolveTelegramForumThreadId,
resolveTelegramStreamMode,
} from "./bot/helpers.js";
import type { TelegramContext, TelegramMessage } from "./bot/types.js";
import { registerTelegramHandlers } from "./bot-handlers.js";
import { createTelegramMessageProcessor } from "./bot-message.js";
@@ -322,15 +327,10 @@ export function createTelegramBot(opts: TelegramBotOptions) {
const messageId = reaction.message_id;
const user = reaction.user;
// Resolve reaction notification mode (default: "own")
const reactionMode = telegramCfg.reactionNotifications ?? "own";
// Resolve reaction notification mode (default: "off")
const reactionMode = telegramCfg.reactionNotifications ?? "off";
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
@@ -364,14 +364,25 @@ export function createTelegramBot(opts: TelegramBotOptions) {
}
senderLabel = senderLabel || "unknown";
// Extract forum thread info (similar to message processing)
const messageThreadId = (reaction as any).message_thread_id;
const isForum = (reaction.chat as any).is_forum === true;
const resolvedThreadId = resolveTelegramForumThreadId({
isForum,
messageThreadId,
});
// Resolve agent route for session
const isGroup =
reaction.chat.type === "group" || reaction.chat.type === "supergroup";
const peerId = isGroup
? buildTelegramGroupPeerId(chatId, resolvedThreadId)
: String(chatId);
const route = resolveAgentRoute({
cfg,
channel: "telegram",
accountId: account.accountId,
peer: { kind: isGroup ? "group" : "dm", id: String(chatId) },
peer: { kind: isGroup ? "group" : "dm", id: peerId },
});
// Enqueue system event for each added reaction

View File

@@ -33,6 +33,11 @@ export function createTelegramRunnerOptions(cfg: ClawdbotConfig): RunOptions<unk
fetch: {
// Match grammY defaults
timeout: 30,
// Request reaction updates from Telegram
allowed_updates: [
"message",
"message_reaction",
],
},
// Suppress grammY getUpdates stack traces; we log concise errors ourselves.
silent: true,
@@ -112,6 +117,21 @@ export async function monitorTelegramProvider(opts: MonitorTelegramOpts = {}) {
},
});
// When using polling mode, ensure no webhook is active
if (!opts.useWebhook) {
try {
const webhookInfo = await bot.api.getWebhookInfo();
if (webhookInfo.url) {
await bot.api.deleteWebhook({ drop_pending_updates: false });
log(`telegram: deleted webhook to enable polling`);
}
} catch (err) {
(opts.runtime?.error ?? console.error)(
`telegram: failed to check/delete webhook: ${String(err)}`,
);
}
}
if (opts.useWebhook) {
await startTelegramWebhook({
token,

View File

@@ -0,0 +1,117 @@
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import type { ClawdbotConfig } from "../config/config.js";
import { resolveTelegramReactionLevel } from "./reaction-level.js";
describe("resolveTelegramReactionLevel", () => {
const prevTelegramToken = process.env.TELEGRAM_BOT_TOKEN;
beforeAll(() => {
process.env.TELEGRAM_BOT_TOKEN = "test-token";
});
afterAll(() => {
if (prevTelegramToken === undefined) {
delete process.env.TELEGRAM_BOT_TOKEN;
} else {
process.env.TELEGRAM_BOT_TOKEN = prevTelegramToken;
}
});
it("defaults to ack level when reactionLevel is not set", () => {
const cfg: ClawdbotConfig = {
channels: { telegram: {} },
};
const result = resolveTelegramReactionLevel({ cfg });
expect(result.level).toBe("ack");
expect(result.ackEnabled).toBe(true);
expect(result.agentReactionsEnabled).toBe(false);
expect(result.agentReactionGuidance).toBeUndefined();
});
it("returns off level with no reactions enabled", () => {
const cfg: ClawdbotConfig = {
channels: { telegram: { reactionLevel: "off" } },
};
const result = resolveTelegramReactionLevel({ cfg });
expect(result.level).toBe("off");
expect(result.ackEnabled).toBe(false);
expect(result.agentReactionsEnabled).toBe(false);
expect(result.agentReactionGuidance).toBeUndefined();
});
it("returns ack level with only ackEnabled", () => {
const cfg: ClawdbotConfig = {
channels: { telegram: { reactionLevel: "ack" } },
};
const result = resolveTelegramReactionLevel({ cfg });
expect(result.level).toBe("ack");
expect(result.ackEnabled).toBe(true);
expect(result.agentReactionsEnabled).toBe(false);
expect(result.agentReactionGuidance).toBeUndefined();
});
it("returns minimal level with agent reactions enabled and minimal guidance", () => {
const cfg: ClawdbotConfig = {
channels: { telegram: { reactionLevel: "minimal" } },
};
const result = resolveTelegramReactionLevel({ cfg });
expect(result.level).toBe("minimal");
expect(result.ackEnabled).toBe(false);
expect(result.agentReactionsEnabled).toBe(true);
expect(result.agentReactionGuidance).toBe("minimal");
});
it("returns extensive level with agent reactions enabled and extensive guidance", () => {
const cfg: ClawdbotConfig = {
channels: { telegram: { reactionLevel: "extensive" } },
};
const result = resolveTelegramReactionLevel({ cfg });
expect(result.level).toBe("extensive");
expect(result.ackEnabled).toBe(false);
expect(result.agentReactionsEnabled).toBe(true);
expect(result.agentReactionGuidance).toBe("extensive");
});
it("resolves reaction level from a specific account", () => {
const cfg: ClawdbotConfig = {
channels: {
telegram: {
reactionLevel: "ack",
accounts: {
work: { botToken: "tok-work", reactionLevel: "extensive" },
},
},
},
};
const result = resolveTelegramReactionLevel({ cfg, accountId: "work" });
expect(result.level).toBe("extensive");
expect(result.ackEnabled).toBe(false);
expect(result.agentReactionsEnabled).toBe(true);
expect(result.agentReactionGuidance).toBe("extensive");
});
it("falls back to global level when account has no reactionLevel", () => {
const cfg: ClawdbotConfig = {
channels: {
telegram: {
reactionLevel: "minimal",
accounts: {
work: { botToken: "tok-work" },
},
},
},
};
const result = resolveTelegramReactionLevel({ cfg, accountId: "work" });
expect(result.level).toBe("minimal");
expect(result.agentReactionsEnabled).toBe(true);
expect(result.agentReactionGuidance).toBe("minimal");
});
});

View File

@@ -0,0 +1,65 @@
import type { ClawdbotConfig } from "../config/config.js";
import { resolveTelegramAccount } from "./accounts.js";
export type TelegramReactionLevel = "off" | "ack" | "minimal" | "extensive";
export type ResolvedReactionLevel = {
level: TelegramReactionLevel;
/** Whether ACK reactions (e.g., 👀 when processing) are enabled. */
ackEnabled: boolean;
/** Whether agent-controlled reactions are enabled. */
agentReactionsEnabled: boolean;
/** Guidance level for agent reactions (minimal = sparse, extensive = liberal). */
agentReactionGuidance?: "minimal" | "extensive";
};
/**
* Resolve the effective reaction level and its implications.
*/
export function resolveTelegramReactionLevel(params: {
cfg: ClawdbotConfig;
accountId?: string;
}): ResolvedReactionLevel {
const account = resolveTelegramAccount({
cfg: params.cfg,
accountId: params.accountId,
});
const level = (account.config.reactionLevel ??
"ack") as TelegramReactionLevel;
switch (level) {
case "off":
return {
level,
ackEnabled: false,
agentReactionsEnabled: false,
};
case "ack":
return {
level,
ackEnabled: true,
agentReactionsEnabled: false,
};
case "minimal":
return {
level,
ackEnabled: false,
agentReactionsEnabled: true,
agentReactionGuidance: "minimal",
};
case "extensive":
return {
level,
ackEnabled: false,
agentReactionsEnabled: true,
agentReactionGuidance: "extensive",
};
default:
// Fallback to ack behavior
return {
level: "ack",
ackEnabled: true,
agentReactionsEnabled: false,
};
}
}

View File

@@ -63,6 +63,10 @@ export async function startTelegramWebhook(opts: {
await bot.api.setWebhook(publicUrl, {
secret_token: opts.secret,
allowed_updates: [
"message",
"message_reaction",
],
});
await new Promise<void>((resolve) => server.listen(port, host, resolve));