|
1 | 1 | import type { Context, Telegram } from 'telegraf'; |
2 | 2 |
|
3 | | -import { craftExtraPhoto } from '#action/utils.js'; |
| 3 | +import { craftExtra } from '#action/utils.js'; |
4 | 4 | import type { Picture } from '#types/index.js'; |
5 | 5 | import * as user from '#user/index.js'; |
6 | 6 | import * as wiki from '#wiki/index.js'; |
7 | 7 |
|
| 8 | +function isVideo(pic: Picture) { |
| 9 | + return pic.url.endsWith('webm'); |
| 10 | +} |
| 11 | + |
8 | 12 | export async function replyPotd(ctx: Context, date: string) { |
9 | 13 | try { |
10 | 14 | const pic_src = await user.getPicSource(ctx.message!.from.id); |
11 | 15 | if (pic_src === null) throw new Error('picture source is null'); |
12 | 16 |
|
13 | 17 | const pic = await wiki.getPotd(date, pic_src); |
| 18 | + const action = isVideo(pic) ? |
| 19 | + ctx.replyWithVideo(pic.url, craftExtra(pic)) : |
| 20 | + ctx.replyWithPhoto(pic.url, craftExtra(pic)); |
14 | 21 |
|
15 | | - await ctx.replyWithPhoto(pic.url, craftExtraPhoto(pic)); |
| 22 | + await action; |
16 | 23 | } catch (err) { |
17 | 24 | await ctx.reply('An error occurred internally!'); |
18 | 25 | console.error(err.message); |
19 | 26 | } |
20 | 27 | } |
21 | 28 |
|
22 | 29 | export async function sendPicture(bot: Telegram, user_id: number, pic: Picture) { |
23 | | - return bot.sendPhoto(user_id, pic.url, craftExtraPhoto(pic)) |
24 | | - .catch(async err => { |
25 | | - switch (err.response.description) { |
26 | | - case 'Forbidden: bot was blocked by the user': |
27 | | - if (await user.setBlockedBot(user_id)) return; |
28 | | - console.error(`Failed to mark the bot is blocked by the user: ${user_id}.`); |
29 | | - break; |
30 | | - case 'Forbidden: user is deactivated': |
31 | | - if (await user.unsubscribe(user_id)) return; |
32 | | - console.error(`Failed to unsubscribe for the user: ${user_id}.`); |
33 | | - break; |
34 | | - default: |
35 | | - console.error(`An error occurred while sending a picture to the user: '${user_id}'.`); |
36 | | - console.error(err); |
37 | | - } |
38 | | - }); |
| 30 | + try { |
| 31 | + const action = isVideo(pic) ? |
| 32 | + bot.sendVideo(user_id, pic.url, craftExtra(pic)) : |
| 33 | + bot.sendPhoto(user_id, pic.url, craftExtra(pic)); |
| 34 | + await action; |
| 35 | + } catch (err) { |
| 36 | + switch (err.response.description) { |
| 37 | + case 'Forbidden: bot was blocked by the user': |
| 38 | + if (await user.setBlockedBot(user_id)) return; |
| 39 | + console.error(`Failed to mark the bot is blocked by the user: ${user_id}.`); |
| 40 | + break; |
| 41 | + case 'Forbidden: user is deactivated': |
| 42 | + if (await user.unsubscribe(user_id)) return; |
| 43 | + console.error(`Failed to unsubscribe for the user: ${user_id}.`); |
| 44 | + break; |
| 45 | + default: |
| 46 | + console.error(`An error occurred while sending a picture to the user: '${user_id}'.`); |
| 47 | + console.error(err); |
| 48 | + } |
| 49 | + } |
39 | 50 | } |
0 commit comments