Skip to content

Commit

Permalink
♻️ refactor: improve floodWaitError's message text
Browse files Browse the repository at this point in the history
  • Loading branch information
chupapee committed May 20, 2024
1 parent 2d332d5 commit bb07f65
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
14 changes: 12 additions & 2 deletions src/controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { bot } from 'index';
import { cleanUpTempMessagesFired, tempMessageSent, UserInfo } from 'model';
import { Markup } from 'telegraf';
import { Api } from 'telegram';
import { FloodWaitError } from 'telegram/errors';
import {
chunkMediafiles,
downloadStories,
Expand Down Expand Up @@ -64,7 +65,13 @@ export const getAllStoriesFx = createEffect(async (task: UserInfo) => {

return '🚫 Stories not found!';
} catch (error) {
console.log('ERROR occured:', error);
if (error instanceof FloodWaitError) {
return (
"⚠️ There're too much requests from the users, please wait " +
(error.seconds / 60).toFixed(0) +
' minutes\n\n(You can use [scheduled message](https://telegram.org/blog/scheduled-reminders-themes) feature btw)'
);
}

// TODO: set sleep time after each request to avoid this error
if (JSON.stringify(error).includes('FloodWaitError')) {
Expand Down Expand Up @@ -153,7 +160,10 @@ export const sendErrorMessageFx = createEffect(
status: 'error',
errorInfo: { cause: message },
});
bot.telegram.sendMessage(task.chatId, message);
bot.telegram.sendMessage(task.chatId, message, {
parse_mode: 'Markdown',
link_preview_options: { is_disabled: true },
});
}
);

Expand Down
12 changes: 10 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,32 @@ export async function notifyAdmin({
2
);

const msgOptions = { link_preview_options: { is_disabled: true } };

if (status === 'error' && errorInfo) {
bot.telegram.sendMessage(
BOT_ADMIN_ID,
'πŸ›‘ ERROR πŸ›‘\n' +
`πŸ”— Target link: ${task?.link}\n` +
`reason: ${JSON.stringify(errorInfo.cause)}\n` +
`author: ${userInfo}`
`author: ${userInfo}`,
msgOptions
);
return;
}

if (status === 'info' && baseInfo) {
bot.telegram.sendMessage(BOT_ADMIN_ID, baseInfo);
let text = baseInfo;
if (task?.user) {
text += '\nπŸ‘€ user: ' + userInfo;
}
bot.telegram.sendMessage(BOT_ADMIN_ID, text, msgOptions);
return;
}

if (status === 'start') {
bot.telegram.sendMessage(BOT_ADMIN_ID, `πŸ‘€ Task started by: ${userInfo}`, {
...msgOptions,
parse_mode: 'HTML',
});
}
Expand Down

0 comments on commit bb07f65

Please sign in to comment.