Skip to content

Commit

Permalink
fix: handle sending errors for announcements as well
Browse files Browse the repository at this point in the history
Signed-off-by: Eiko Wagenknecht <[email protected]>
  • Loading branch information
eikowagenknecht committed Feb 10, 2025
1 parent 47ec1c3 commit 56a601d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
13 changes: 3 additions & 10 deletions src/services/database/telegramChatRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,13 @@ import { handleError, handleInsertResult } from "./common";

export async function getTelegramChatById(
chatId: number,
threadId?: number | null,
): Promise<TelegramChat | undefined> {
try {
let query = getDb()
return await getDb()
.selectFrom("telegram_chats")
.selectAll()
.where("id", "=", chatId);

if (threadId !== undefined) {
query = query.where("thread_id", "=", threadId);
}

return await query.executeTakeFirst();
.where("id", "=", chatId)
.executeTakeFirst();
} catch (error) {
handleError("get telegram chat", error);
}
Expand Down Expand Up @@ -51,7 +45,6 @@ export async function getAllActiveTelegramChats(): Promise<TelegramChat[]> {
.execute();
} catch (error) {
handleError("get all active telegram chats", error);
return [];
}
}

Expand Down
36 changes: 20 additions & 16 deletions src/services/telegrambot/utils/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ export async function sendNewOffersToChat(
interactive = false,
): Promise<void> {
const chat = await getTelegramChatById(dbChatId);
if (!chat) {
logger.error(`Chat ${dbChatId.toFixed()} not found.`);
if (chat === undefined) {
logger.error(
`Can't send offer, chat ${dbChatId.toFixed()} not found in database.`,
);
return;
}

Expand Down Expand Up @@ -102,13 +104,7 @@ export async function sendNewOffersToChat(
}
} catch (error) {
// Check for blocked chat errors
if (
error instanceof GrammyError &&
(error.description.includes("chat not found") ||
error.description.includes("bot was blocked by the user") ||
error.description.includes("user is deactivated") ||
error.description.includes("message thread not found"))
) {
if (error instanceof GrammyError && isPermanentlyBlockedChat(error)) {
logger.info(
`Chat ${chat.chat_id.toFixed()} is no longer accessible, marking as inactive.`,
);
Expand All @@ -128,8 +124,10 @@ export async function sendNewAnnouncementsToChat(
dbChatId: number,
): Promise<void> {
const chat = await getTelegramChatById(dbChatId);
if (!chat) {
logger.error(`Chat ${dbChatId.toFixed()} not found`);
if (chat === undefined) {
logger.error(
`Can't send announcement, chat ${dbChatId.toFixed()} not found in database.`,
);
return;
}

Expand All @@ -154,11 +152,7 @@ export async function sendNewAnnouncementsToChat(
}
} catch (error) {
// Check for blocked chat errors
if (
error instanceof GrammyError &&
(error.description.includes("chat not found") ||
error.description.includes("bot was blocked by the user"))
) {
if (error instanceof GrammyError && isPermanentlyBlockedChat(error)) {
logger.info(
`Chat ${chat.chat_id.toFixed()} is no longer accessible, marking as inactive.`,
);
Expand All @@ -173,3 +167,13 @@ export async function sendNewAnnouncementsToChat(
);
}
}

function isPermanentlyBlockedChat(error: Error): boolean {
return (
error instanceof GrammyError &&
(error.description.includes("chat not found") ||
error.description.includes("bot was blocked by the user") ||
error.description.includes("user is deactivated") ||
error.description.includes("message thread not found"))
);
}

0 comments on commit 56a601d

Please sign in to comment.