* feat: Add support for Telegram quote (partial message replies) - Enhanced describeReplyTarget() to detect and extract quoted text from msg.quote - Updated reply formatting to distinguish between full message replies and quotes - Added isQuote flag to replyTarget object for proper identification - Quote replies show as [Quoting user] "quoted text" [/Quoting] - Regular replies unchanged: [Replying to user] full message [/Replying] Resolves need for partial message reply support in Telegram Bot API. Backward compatible with existing reply functionality. * updating references * Mac: finish Moltbot rename * Mac: finish Moltbot rename (paths) * fix(macOS): rename Clawdbot directories to Moltbot for naming consistency Directory renames: - apps/macos/Sources/Clawdbot → Moltbot - apps/macos/Sources/ClawdbotDiscovery → MoltbotDiscovery - apps/macos/Sources/ClawdbotIPC → MoltbotIPC - apps/macos/Sources/ClawdbotMacCLI → MoltbotMacCLI - apps/macos/Sources/ClawdbotProtocol → MoltbotProtocol - apps/macos/Tests/ClawdbotIPCTests → MoltbotIPCTests - apps/shared/ClawdbotKit → MoltbotKit - apps/shared/MoltbotKit/Sources/Clawdbot* → Moltbot* - apps/shared/MoltbotKit/Tests/ClawdbotKitTests → MoltbotKitTests Resource renames: - Clawdbot.icns → Moltbot.icns Code fixes: - Update Package.swift paths to reference Moltbot* directories - Fix clawdbot* → moltbot* symbol references in Swift code: - clawdbotManagedPaths → moltbotManagedPaths - clawdbotExecutable → moltbotExecutable - clawdbotCommand → moltbotCommand - clawdbotNodeCommand → moltbotNodeCommand - clawdbotOAuthDirEnv → moltbotOAuthDirEnv - clawdbotSelectSettingsTab → moltbotSelectSettingsTab * fix: update remaining ClawdbotKit path references to MoltbotKit - scripts/bundle-a2ui.sh: A2UI_APP_DIR path - package.json: format:swift and protocol:check paths - scripts/protocol-gen-swift.ts: output paths - .github/dependabot.yml: directory path and comment - .gitignore: build cache paths - .swiftformat: exclusion paths - .swiftlint.yml: exclusion path - apps/android/app/build.gradle.kts: assets.srcDir path - apps/ios/project.yml: package path - apps/ios/README.md: documentation reference - docs/concepts/typebox.md: documentation reference - apps/shared/MoltbotKit/Package.swift: fix argument order * chore: update Package.resolved after dependency resolution * fix: add MACOS_APP_SOURCES_DIR constant and update test to use new path The cron-protocol-conformance test was using LEGACY_MACOS_APP_SOURCES_DIR which points to the old Clawdbot path. Added a new MACOS_APP_SOURCES_DIR constant for the current Moltbot path and updated the test to use it. * fix: finish Moltbot macOS rename (#2844) (thanks @fal3) * Extensions: use workspace moltbot in memory-core * fix(security): recognize Venice-style claude-opus-45 as top-tier model The security audit was incorrectly flagging venice/claude-opus-45 as 'Below Claude 4.5' because the regex expected -4-5 (with dash) but Venice uses -45 (without dash between 4 and 5). Updated isClaude45OrHigher() regex to match both formats. Added test case to prevent regression. * Branding: update bot.molt bundle IDs + launchd labels * Branding: remove legacy android packages * fix: wire telegram quote support (#2900) Co-authored-by: aduk059 <aduk059@users.noreply.github.com> * fix: support Telegram quote replies (#2900) (thanks @aduk059) --------- Co-authored-by: Gustavo Madeira Santana <gumadeiras@users.noreply.github.com> Co-authored-by: Shadow <shadow@clawd.bot> Co-authored-by: Alex Fallah <alexfallah7@gmail.com> Co-authored-by: Josh Palmer <joshp123@users.noreply.github.com> Co-authored-by: jonisjongithub <jonisjongithub@users.noreply.github.com> Co-authored-by: Gustavo Madeira Santana <gumadeiras@gmail.com> Co-authored-by: aduk059 <aduk059@users.noreply.github.com>
309 lines
8.2 KiB
TypeScript
309 lines
8.2 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
import type { Bot } from "grammy";
|
|
|
|
import { deliverReplies } from "./delivery.js";
|
|
|
|
const loadWebMedia = vi.fn();
|
|
|
|
vi.mock("../../web/media.js", () => ({
|
|
loadWebMedia: (...args: unknown[]) => loadWebMedia(...args),
|
|
}));
|
|
|
|
vi.mock("grammy", () => ({
|
|
InputFile: class {
|
|
constructor(
|
|
public buffer: Buffer,
|
|
public fileName?: string,
|
|
) {}
|
|
},
|
|
GrammyError: class GrammyError extends Error {
|
|
description = "";
|
|
},
|
|
}));
|
|
|
|
describe("deliverReplies", () => {
|
|
beforeEach(() => {
|
|
loadWebMedia.mockReset();
|
|
});
|
|
|
|
it("skips audioAsVoice-only payloads without logging an error", async () => {
|
|
const runtime = { error: vi.fn() };
|
|
const bot = { api: {} } as unknown as Bot;
|
|
|
|
await deliverReplies({
|
|
replies: [{ audioAsVoice: true }],
|
|
chatId: "123",
|
|
token: "tok",
|
|
runtime,
|
|
bot,
|
|
replyToMode: "off",
|
|
textLimit: 4000,
|
|
});
|
|
|
|
expect(runtime.error).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("invokes onVoiceRecording before sending a voice note", async () => {
|
|
const events: string[] = [];
|
|
const runtime = { error: vi.fn() };
|
|
const sendVoice = vi.fn(async () => {
|
|
events.push("sendVoice");
|
|
return { message_id: 1, chat: { id: "123" } };
|
|
});
|
|
const bot = { api: { sendVoice } } as unknown as Bot;
|
|
const onVoiceRecording = vi.fn(async () => {
|
|
events.push("recordVoice");
|
|
});
|
|
|
|
loadWebMedia.mockResolvedValueOnce({
|
|
buffer: Buffer.from("voice"),
|
|
contentType: "audio/ogg",
|
|
fileName: "note.ogg",
|
|
});
|
|
|
|
await deliverReplies({
|
|
replies: [{ mediaUrl: "https://example.com/note.ogg", audioAsVoice: true }],
|
|
chatId: "123",
|
|
token: "tok",
|
|
runtime,
|
|
bot,
|
|
replyToMode: "off",
|
|
textLimit: 4000,
|
|
onVoiceRecording,
|
|
});
|
|
|
|
expect(onVoiceRecording).toHaveBeenCalledTimes(1);
|
|
expect(sendVoice).toHaveBeenCalledTimes(1);
|
|
expect(events).toEqual(["recordVoice", "sendVoice"]);
|
|
});
|
|
|
|
it("renders markdown in media captions", async () => {
|
|
const runtime = { error: vi.fn(), log: vi.fn() };
|
|
const sendPhoto = vi.fn().mockResolvedValue({
|
|
message_id: 2,
|
|
chat: { id: "123" },
|
|
});
|
|
const bot = { api: { sendPhoto } } as unknown as Bot;
|
|
|
|
loadWebMedia.mockResolvedValueOnce({
|
|
buffer: Buffer.from("image"),
|
|
contentType: "image/jpeg",
|
|
fileName: "photo.jpg",
|
|
});
|
|
|
|
await deliverReplies({
|
|
replies: [{ mediaUrl: "https://example.com/photo.jpg", text: "hi **boss**" }],
|
|
chatId: "123",
|
|
token: "tok",
|
|
runtime,
|
|
bot,
|
|
replyToMode: "off",
|
|
textLimit: 4000,
|
|
});
|
|
|
|
expect(sendPhoto).toHaveBeenCalledWith(
|
|
"123",
|
|
expect.anything(),
|
|
expect.objectContaining({
|
|
caption: "hi <b>boss</b>",
|
|
parse_mode: "HTML",
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("includes link_preview_options when linkPreview is false", async () => {
|
|
const runtime = { error: vi.fn(), log: vi.fn() };
|
|
const sendMessage = vi.fn().mockResolvedValue({
|
|
message_id: 3,
|
|
chat: { id: "123" },
|
|
});
|
|
const bot = { api: { sendMessage } } as unknown as Bot;
|
|
|
|
await deliverReplies({
|
|
replies: [{ text: "Check https://example.com" }],
|
|
chatId: "123",
|
|
token: "tok",
|
|
runtime,
|
|
bot,
|
|
replyToMode: "off",
|
|
textLimit: 4000,
|
|
linkPreview: false,
|
|
});
|
|
|
|
expect(sendMessage).toHaveBeenCalledWith(
|
|
"123",
|
|
expect.any(String),
|
|
expect.objectContaining({
|
|
link_preview_options: { is_disabled: true },
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("does not include link_preview_options when linkPreview is true", async () => {
|
|
const runtime = { error: vi.fn(), log: vi.fn() };
|
|
const sendMessage = vi.fn().mockResolvedValue({
|
|
message_id: 4,
|
|
chat: { id: "123" },
|
|
});
|
|
const bot = { api: { sendMessage } } as unknown as Bot;
|
|
|
|
await deliverReplies({
|
|
replies: [{ text: "Check https://example.com" }],
|
|
chatId: "123",
|
|
token: "tok",
|
|
runtime,
|
|
bot,
|
|
replyToMode: "off",
|
|
textLimit: 4000,
|
|
linkPreview: true,
|
|
});
|
|
|
|
expect(sendMessage).toHaveBeenCalledWith(
|
|
"123",
|
|
expect.any(String),
|
|
expect.not.objectContaining({
|
|
link_preview_options: expect.anything(),
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("uses reply_parameters when quote text is provided", async () => {
|
|
const runtime = { error: vi.fn(), log: vi.fn() };
|
|
const sendMessage = vi.fn().mockResolvedValue({
|
|
message_id: 10,
|
|
chat: { id: "123" },
|
|
});
|
|
const bot = { api: { sendMessage } } as unknown as Bot;
|
|
|
|
await deliverReplies({
|
|
replies: [{ text: "Hello there", replyToId: "500" }],
|
|
chatId: "123",
|
|
token: "tok",
|
|
runtime,
|
|
bot,
|
|
replyToMode: "all",
|
|
textLimit: 4000,
|
|
replyQuoteText: "quoted text",
|
|
});
|
|
|
|
expect(sendMessage).toHaveBeenCalledWith(
|
|
"123",
|
|
expect.any(String),
|
|
expect.objectContaining({
|
|
reply_parameters: {
|
|
message_id: 500,
|
|
quote: "quoted text",
|
|
},
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("falls back to text when sendVoice fails with VOICE_MESSAGES_FORBIDDEN", async () => {
|
|
const runtime = { error: vi.fn(), log: vi.fn() };
|
|
const sendVoice = vi
|
|
.fn()
|
|
.mockRejectedValue(
|
|
new Error(
|
|
"GrammyError: Call to 'sendVoice' failed! (400: Bad Request: VOICE_MESSAGES_FORBIDDEN)",
|
|
),
|
|
);
|
|
const sendMessage = vi.fn().mockResolvedValue({
|
|
message_id: 5,
|
|
chat: { id: "123" },
|
|
});
|
|
const bot = { api: { sendVoice, sendMessage } } as unknown as Bot;
|
|
|
|
loadWebMedia.mockResolvedValueOnce({
|
|
buffer: Buffer.from("voice"),
|
|
contentType: "audio/ogg",
|
|
fileName: "note.ogg",
|
|
});
|
|
|
|
await deliverReplies({
|
|
replies: [
|
|
{ mediaUrl: "https://example.com/note.ogg", text: "Hello there", audioAsVoice: true },
|
|
],
|
|
chatId: "123",
|
|
token: "tok",
|
|
runtime,
|
|
bot,
|
|
replyToMode: "off",
|
|
textLimit: 4000,
|
|
});
|
|
|
|
// Voice was attempted but failed
|
|
expect(sendVoice).toHaveBeenCalledTimes(1);
|
|
// Fallback to text succeeded
|
|
expect(sendMessage).toHaveBeenCalledTimes(1);
|
|
expect(sendMessage).toHaveBeenCalledWith(
|
|
"123",
|
|
expect.stringContaining("Hello there"),
|
|
expect.any(Object),
|
|
);
|
|
});
|
|
|
|
it("rethrows non-VOICE_MESSAGES_FORBIDDEN errors from sendVoice", async () => {
|
|
const runtime = { error: vi.fn(), log: vi.fn() };
|
|
const sendVoice = vi.fn().mockRejectedValue(new Error("Network error"));
|
|
const sendMessage = vi.fn();
|
|
const bot = { api: { sendVoice, sendMessage } } as unknown as Bot;
|
|
|
|
loadWebMedia.mockResolvedValueOnce({
|
|
buffer: Buffer.from("voice"),
|
|
contentType: "audio/ogg",
|
|
fileName: "note.ogg",
|
|
});
|
|
|
|
await expect(
|
|
deliverReplies({
|
|
replies: [{ mediaUrl: "https://example.com/note.ogg", text: "Hello", audioAsVoice: true }],
|
|
chatId: "123",
|
|
token: "tok",
|
|
runtime,
|
|
bot,
|
|
replyToMode: "off",
|
|
textLimit: 4000,
|
|
}),
|
|
).rejects.toThrow("Network error");
|
|
|
|
expect(sendVoice).toHaveBeenCalledTimes(1);
|
|
// Text fallback should NOT be attempted for other errors
|
|
expect(sendMessage).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("rethrows VOICE_MESSAGES_FORBIDDEN when no text fallback is available", async () => {
|
|
const runtime = { error: vi.fn(), log: vi.fn() };
|
|
const sendVoice = vi
|
|
.fn()
|
|
.mockRejectedValue(
|
|
new Error(
|
|
"GrammyError: Call to 'sendVoice' failed! (400: Bad Request: VOICE_MESSAGES_FORBIDDEN)",
|
|
),
|
|
);
|
|
const sendMessage = vi.fn();
|
|
const bot = { api: { sendVoice, sendMessage } } as unknown as Bot;
|
|
|
|
loadWebMedia.mockResolvedValueOnce({
|
|
buffer: Buffer.from("voice"),
|
|
contentType: "audio/ogg",
|
|
fileName: "note.ogg",
|
|
});
|
|
|
|
await expect(
|
|
deliverReplies({
|
|
replies: [{ mediaUrl: "https://example.com/note.ogg", audioAsVoice: true }],
|
|
chatId: "123",
|
|
token: "tok",
|
|
runtime,
|
|
bot,
|
|
replyToMode: "off",
|
|
textLimit: 4000,
|
|
}),
|
|
).rejects.toThrow("VOICE_MESSAGES_FORBIDDEN");
|
|
|
|
expect(sendVoice).toHaveBeenCalledTimes(1);
|
|
expect(sendMessage).not.toHaveBeenCalled();
|
|
});
|
|
});
|