Update tg issues

This commit is contained in:
xCyanGrizzly
2026-03-16 16:51:30 +01:00
parent 2763de2711
commit d7bbb7587e
13 changed files with 450 additions and 70 deletions

View File

@@ -1,7 +1,7 @@
import { config } from "./util/config.js";
import { logger } from "./util/logger.js";
import { db, pool } from "./db/client.js";
import { createBotClient, closeBotClient, onBotUpdate } from "./tdlib/client.js";
import { createBotClient, closeBotClient, onBotUpdate, getUser } from "./tdlib/client.js";
import { startSendListener, stopSendListener } from "./send-listener.js";
import { handleMessage } from "./commands.js";
import { mkdir } from "fs/promises";
@@ -49,14 +49,27 @@ async function main(): Promise<void> {
const userId = senderId.user_id as number;
if (text && userId) {
// Get user info for display name (async but fire-and-forget for perf)
handleMessage({
chatId: BigInt(chatId),
userId: BigInt(userId),
text,
firstName: "User", // TDLib provides this via a separate getUser call
username: undefined,
}).catch((err) => {
(async () => {
let firstName = "User";
let lastName: string | undefined;
let username: string | undefined;
try {
const userInfo = await getUser(userId);
firstName = userInfo.firstName;
lastName = userInfo.lastName;
username = userInfo.username;
} catch {
// Fall back to defaults if getUser fails
}
await handleMessage({
chatId: BigInt(chatId),
userId: BigInt(userId),
text,
firstName,
lastName,
username,
});
})().catch((err) => {
log.error({ err, chatId, userId }, "Failed to handle message");
});
}

View File

@@ -182,7 +182,7 @@ async function handleNewPackage(payload: string): Promise<void> {
userSubs.set(key, patterns);
}
const creator = data.creator ? ` by ${data.creator}` : "";
const creator = data.creator ? ` by ${escapeHtml(data.creator)}` : "";
for (const [telegramUserId, patterns] of userSubs) {
const msg = [
`🔔 <b>New package matching your subscriptions:</b>`,

View File

@@ -143,6 +143,28 @@ export async function sendPhotoMessage(
}
}
/**
* Get basic info about a Telegram user (name, username).
*/
export async function getUser(
userId: number
): Promise<{ firstName: string; lastName?: string; username?: string }> {
if (!client) throw new Error("Bot client not initialized");
const user = (await client.invoke({
_: "getUser",
user_id: userId,
})) as {
first_name?: string;
last_name?: string;
usernames?: { editable_username?: string };
};
return {
firstName: user.first_name ?? "User",
lastName: user.last_name || undefined,
username: user.usernames?.editable_username || undefined,
};
}
/**
* Get updates from TDLib. The bot listens for new messages this way.
*/