fix(bot): resolve private chat before sending to a user
continuous-integration/drone/push Build is passing

After the bot's TDLib session was rebuilt from scratch (following a
disk-full corruption on 2026-08-05), sendMessage started failing with
"Chat not found" for every previously-known user — a fresh TDLib
database has no cached chat/peer info until createPrivateChat
explicitly resolves it. Call it before every send.
This commit is contained in:
2026-08-10 08:27:41 +02:00
parent 7bc57ec227
commit 06a48a419b
+21
View File
@@ -53,6 +53,21 @@ export async function closeBotClient(): Promise<void> {
} }
} }
/**
* Make sure TDLib has resolved this user into a private chat before sending.
* A bot's local TDLib database only knows about chats it has seen since its
* last (re)authentication — after a state-directory reset it has no chat
* history cached, so `sendMessage` fails with "Chat not found" even for
* users who messaged the bot long ago. `createPrivateChat` forces TDLib to
* resolve/fetch the chat first.
*/
async function ensurePrivateChat(c: tdl.Client, userId: number): Promise<void> {
await withFloodWait(
() => c.invoke({ _: "createPrivateChat", user_id: userId, force: false }),
"createPrivateChat"
);
}
/** /**
* Send a document from a channel to a user's DM. * Send a document from a channel to a user's DM.
* *
@@ -71,6 +86,8 @@ export async function copyMessageToUser(
if (!client) throw new Error("Bot client not initialized"); if (!client) throw new Error("Bot client not initialized");
const c = client; const c = client;
await ensurePrivateChat(c, Number(toUserId));
log.info( log.info(
{ fromChatId: fromChatId.toString(), messageId: messageId.toString(), toUserId: toUserId.toString() }, { fromChatId: fromChatId.toString(), messageId: messageId.toString(), toUserId: toUserId.toString() },
"Sending file to user" "Sending file to user"
@@ -233,6 +250,8 @@ export async function sendTextMessage(
if (!client) throw new Error("Bot client not initialized"); if (!client) throw new Error("Bot client not initialized");
const c = client; const c = client;
await ensurePrivateChat(c, Number(chatId));
// Parse the text first // Parse the text first
const parsed = await withFloodWait( const parsed = await withFloodWait(
() => () =>
@@ -269,6 +288,8 @@ export async function sendPhotoMessage(
if (!client) throw new Error("Bot client not initialized"); if (!client) throw new Error("Bot client not initialized");
const c = client; const c = client;
await ensurePrivateChat(c, Number(chatId));
// Write the photo to a temp file // Write the photo to a temp file
const { writeFile, unlink } = await import("fs/promises"); const { writeFile, unlink } = await import("fs/promises");
const path = await import("path"); const path = await import("path");