surface: envelope inbound messages for agent
This commit is contained in:
@@ -23,7 +23,13 @@ vi.mock("@grammyjs/transformer-throttler", () => ({
|
||||
apiThrottler: () => throttlerSpy(),
|
||||
}));
|
||||
|
||||
vi.mock("../auto-reply/reply.js", () => {
|
||||
const replySpy = vi.fn();
|
||||
return { getReplyFromConfig: replySpy, __replySpy: replySpy };
|
||||
});
|
||||
|
||||
import { createTelegramBot } from "./bot.js";
|
||||
import * as replyModule from "../auto-reply/reply.js";
|
||||
|
||||
describe("createTelegramBot", () => {
|
||||
it("installs grammY throttler", () => {
|
||||
@@ -31,4 +37,32 @@ describe("createTelegramBot", () => {
|
||||
expect(throttlerSpy).toHaveBeenCalledTimes(1);
|
||||
expect(useSpy).toHaveBeenCalledWith("throttler");
|
||||
});
|
||||
|
||||
it("wraps inbound message with Telegram envelope", async () => {
|
||||
onSpy.mockReset();
|
||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<
|
||||
typeof vi.fn
|
||||
>;
|
||||
replySpy.mockReset();
|
||||
|
||||
createTelegramBot({ token: "tok" });
|
||||
expect(onSpy).toHaveBeenCalledWith("message", expect.any(Function));
|
||||
const handler = onSpy.mock.calls[0][1] as (ctx: any) => Promise<void>;
|
||||
|
||||
const message = {
|
||||
chat: { id: 1234, type: "private" },
|
||||
text: "hello world",
|
||||
date: 1736380800, // 2025-01-09T00:00:00Z
|
||||
};
|
||||
await handler({
|
||||
message,
|
||||
me: { username: "clawdis_bot" },
|
||||
getFile: async () => ({ download: async () => new Uint8Array() }),
|
||||
});
|
||||
|
||||
expect(replySpy).toHaveBeenCalledTimes(1);
|
||||
const payload = replySpy.mock.calls[0][0];
|
||||
expect(payload.Body).toMatch(/^\[Telegram telegram:1234 2025-01-09 00:00]/);
|
||||
expect(payload.Body).toContain("hello world");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,6 +7,7 @@ import { Bot, InputFile, webhookCallback } from "grammy";
|
||||
import { chunkText } from "../auto-reply/chunk.js";
|
||||
import { getReplyFromConfig } from "../auto-reply/reply.js";
|
||||
import type { ReplyPayload } from "../auto-reply/types.js";
|
||||
import { formatAgentEnvelope } from "../auto-reply/envelope.js";
|
||||
import { loadConfig } from "../config/config.js";
|
||||
import { danger, logVerbose } from "../globals.js";
|
||||
import { getChildLogger } from "../logging.js";
|
||||
@@ -98,8 +99,15 @@ export function createTelegramBot(opts: TelegramBotOptions) {
|
||||
}
|
||||
|
||||
const media = await resolveMedia(ctx, mediaMaxBytes);
|
||||
const body = (msg.text ?? msg.caption ?? media?.placeholder ?? "").trim();
|
||||
if (!body) return;
|
||||
const rawBody = (msg.text ?? msg.caption ?? media?.placeholder ?? "").trim();
|
||||
if (!rawBody) return;
|
||||
|
||||
const body = formatAgentEnvelope({
|
||||
surface: "Telegram",
|
||||
from: isGroup ? `group:${chatId}` : `telegram:${chatId}`,
|
||||
timestamp: msg.date ? msg.date * 1000 : undefined,
|
||||
body: rawBody,
|
||||
});
|
||||
|
||||
const ctxPayload = {
|
||||
Body: body,
|
||||
|
||||
Reference in New Issue
Block a user