Skip to content

Commit 45dc4ef

Browse files
julianengelsteipete
authored andcommitted
fix(telegram): make /activation command work by checking session state
The /activation command now properly controls group activation mode: - Loads session state before filtering messages - Checks groupActivation field (from /activation command) - Falls back to config telegram.groups requireMention setting Previously, the bot only checked config and ignored session state, making the /activation command appear to work but have no effect. Changes: - Add resolveGroupActivation() to check session before config - Import loadSessionStore to read session state early - Pass messageThreadId to support forum topics correctly
1 parent 1601be5 commit 45dc4ef

File tree

1 file changed

+46
-11
lines changed

1 file changed

+46
-11
lines changed

src/telegram/bot.ts

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ import {
3030
resolveProviderGroupPolicy,
3131
resolveProviderGroupRequireMention,
3232
} from "../config/group-policy.js";
33-
import { resolveStorePath, updateLastRoute } from "../config/sessions.js";
33+
import {
34+
loadSessionStore,
35+
resolveStorePath,
36+
updateLastRoute,
37+
} from "../config/sessions.js";
3438
import { danger, logVerbose, shouldLogVerbose } from "../globals.js";
3539
import { formatErrorMessage } from "../infra/errors.js";
3640
import { getChildLogger } from "../logging.js";
@@ -208,6 +212,29 @@ export function createTelegramBot(opts: TelegramBotOptions) {
208212
provider: "telegram",
209213
groupId: String(chatId),
210214
});
215+
const resolveGroupActivation = (params: {
216+
chatId: string | number;
217+
agentId?: string;
218+
messageThreadId?: number;
219+
sessionKey?: string;
220+
}) => {
221+
const agentId = params.agentId ?? cfg.agent?.id ?? "main";
222+
const sessionKey =
223+
params.sessionKey ??
224+
`agent:${agentId}:telegram:group:${buildTelegramGroupPeerId(params.chatId, params.messageThreadId)}`;
225+
const storePath = resolveStorePath(cfg.session?.store, { agentId });
226+
try {
227+
const store = loadSessionStore(storePath);
228+
const entry = store[sessionKey];
229+
if (entry?.groupActivation === "always") return false;
230+
if (entry?.groupActivation === "mention") return true;
231+
} catch (err) {
232+
logVerbose(
233+
`Failed to load session for activation check: ${String(err)}`,
234+
);
235+
}
236+
return undefined;
237+
};
211238
const resolveGroupRequireMention = (chatId: string | number) =>
212239
resolveProviderGroupRequireMention({
213240
cfg,
@@ -246,6 +273,17 @@ export function createTelegramBot(opts: TelegramBotOptions) {
246273
chatId,
247274
messageThreadId,
248275
);
276+
const peerId = isGroup
277+
? buildTelegramGroupPeerId(chatId, messageThreadId)
278+
: String(chatId);
279+
const route = resolveAgentRoute({
280+
cfg,
281+
provider: "telegram",
282+
peer: {
283+
kind: isGroup ? "group" : "dm",
284+
id: peerId,
285+
},
286+
});
249287
const effectiveDmAllow = normalizeAllowFrom([
250288
...(allowFrom ?? []),
251289
...storeAllowFrom,
@@ -380,8 +418,15 @@ export function createTelegramBot(opts: TelegramBotOptions) {
380418
const hasAnyMention = (msg.entities ?? msg.caption_entities ?? []).some(
381419
(ent) => ent.type === "mention",
382420
);
421+
const activationOverride = resolveGroupActivation({
422+
chatId,
423+
messageThreadId,
424+
sessionKey: route.sessionKey,
425+
agentId: route.agentId,
426+
});
383427
const baseRequireMention = resolveGroupRequireMention(chatId);
384428
const requireMention = firstDefined(
429+
activationOverride,
385430
topicConfig?.requireMention,
386431
groupConfig?.requireMention,
387432
baseRequireMention,
@@ -471,16 +516,6 @@ export function createTelegramBot(opts: TelegramBotOptions) {
471516
body: `${bodyText}${replySuffix}`,
472517
});
473518

474-
const route = resolveAgentRoute({
475-
cfg,
476-
provider: "telegram",
477-
peer: {
478-
kind: isGroup ? "group" : "dm",
479-
id: isGroup
480-
? buildTelegramGroupPeerId(chatId, messageThreadId)
481-
: buildTelegramDmPeerId(chatId, messageThreadId),
482-
},
483-
});
484519
const skillFilter = firstDefined(topicConfig?.skills, groupConfig?.skills);
485520
const systemPromptParts = [
486521
groupConfig?.systemPrompt?.trim() || null,

0 commit comments

Comments
 (0)