Telegram-user: ignore sends after client destroy
This commit is contained in:
parent
62873a11b7
commit
39d9eb4589
@ -20,6 +20,11 @@ type NormalizedPollInput = {
|
|||||||
maxSelections: number;
|
maxSelections: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function isDestroyedClientError(err: unknown): boolean {
|
||||||
|
const message = err instanceof Error ? err.message : String(err);
|
||||||
|
return /client is destroyed/i.test(message);
|
||||||
|
}
|
||||||
|
|
||||||
export type TelegramUserSendOpts = {
|
export type TelegramUserSendOpts = {
|
||||||
client?: TelegramClient;
|
client?: TelegramClient;
|
||||||
accountId?: string;
|
accountId?: string;
|
||||||
@ -121,9 +126,17 @@ export async function sendMessageTelegramUser(
|
|||||||
});
|
});
|
||||||
try {
|
try {
|
||||||
const target = resolveTelegramUserPeer(normalizeTarget(to));
|
const target = resolveTelegramUserPeer(normalizeTarget(to));
|
||||||
const message = await client.sendText(target, text, {
|
let message: Awaited<ReturnType<TelegramClient["sendText"]>> | null = null;
|
||||||
...(opts.replyToId ? { replyTo: opts.replyToId } : {}),
|
try {
|
||||||
});
|
message = await client.sendText(target, text, {
|
||||||
|
...(opts.replyToId ? { replyTo: opts.replyToId } : {}),
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
if (!isDestroyedClientError(err)) throw err;
|
||||||
|
}
|
||||||
|
if (!message) {
|
||||||
|
return { messageId: "", chatId: String(target) };
|
||||||
|
}
|
||||||
return { messageId: String(message.id), chatId: String(target) };
|
return { messageId: String(message.id), chatId: String(target) };
|
||||||
} finally {
|
} finally {
|
||||||
if (stopOnDone) {
|
if (stopOnDone) {
|
||||||
@ -151,9 +164,17 @@ export async function sendMediaTelegramUser(
|
|||||||
fileMime: media.contentType,
|
fileMime: media.contentType,
|
||||||
caption: text,
|
caption: text,
|
||||||
});
|
});
|
||||||
const message = await client.sendMedia(target, input, {
|
let message: Awaited<ReturnType<TelegramClient["sendMedia"]>> | null = null;
|
||||||
...(opts.replyToId ? { replyTo: opts.replyToId } : {}),
|
try {
|
||||||
});
|
message = await client.sendMedia(target, input, {
|
||||||
|
...(opts.replyToId ? { replyTo: opts.replyToId } : {}),
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
if (!isDestroyedClientError(err)) throw err;
|
||||||
|
}
|
||||||
|
if (!message) {
|
||||||
|
return { messageId: "", chatId: String(target) };
|
||||||
|
}
|
||||||
return { messageId: String(message.id), chatId: String(target) };
|
return { messageId: String(message.id), chatId: String(target) };
|
||||||
} finally {
|
} finally {
|
||||||
if (stopOnDone) {
|
if (stopOnDone) {
|
||||||
@ -181,9 +202,17 @@ export async function sendPollTelegramUser(
|
|||||||
answers: normalized.options,
|
answers: normalized.options,
|
||||||
multiple: normalized.maxSelections > 1,
|
multiple: normalized.maxSelections > 1,
|
||||||
});
|
});
|
||||||
const message = await client.sendMedia(target, input, {
|
let message: Awaited<ReturnType<TelegramClient["sendMedia"]>> | null = null;
|
||||||
...(opts.replyToId ? { replyTo: opts.replyToId } : {}),
|
try {
|
||||||
});
|
message = await client.sendMedia(target, input, {
|
||||||
|
...(opts.replyToId ? { replyTo: opts.replyToId } : {}),
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
if (!isDestroyedClientError(err)) throw err;
|
||||||
|
}
|
||||||
|
if (!message) {
|
||||||
|
return { messageId: "", chatId: String(target) };
|
||||||
|
}
|
||||||
return { messageId: String(message.id), chatId: String(target) };
|
return { messageId: String(message.id), chatId: String(target) };
|
||||||
} finally {
|
} finally {
|
||||||
if (stopOnDone) {
|
if (stopOnDone) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user