|
1 | | -const Composer = require('telegraf/composer') |
| 1 | +import { Composer } from 'telegraf-esm' |
| 2 | + |
| 3 | +import tweetLoader from '../view/tweet-loader.js' |
| 4 | +import { templates, sleep } from '../lib/index.js' |
| 5 | +import { onlyPrivate } from '../middlewares/index.js' |
| 6 | +import { bot } from '../core/bot.js' |
| 7 | + |
2 | 8 | const composer = new Composer() |
3 | 9 |
|
4 | | -const tweetLoader = require('../view/tweet-loader') |
5 | | -const { onlyPrivate } = require('../middlewares') |
| 10 | +const sendTweets = async ({ |
| 11 | + chat, |
| 12 | + reply, |
| 13 | + message, |
| 14 | + telegram, |
| 15 | + replyWithMediaGroup |
| 16 | +}) => { |
| 17 | + const tweets = message.text.match(/twitter\.com\/.+\/status\/[0-9]+/ig) |
| 18 | + .map(tweet => tweet.match(/twitter\.com\/(\S+)\/status\/([0-9]+)/i)) |
| 19 | + const parsedTweets = [] |
| 20 | + |
| 21 | + const originalTweetsLength = tweets.length |
6 | 22 |
|
7 | | -composer.hears(/twitter\.com\/(.+)\/status\/([0-9]+)/i, onlyPrivate, async ctx => { |
8 | | - const [_, username, tweetId] = ctx.match |
9 | | - const { photo, text, response } = await tweetLoader(tweetId, username, { description: true }) |
| 23 | + let msg |
10 | 24 |
|
11 | 25 | try { |
12 | | - await ctx.replyWithChatAction('upload_photo') |
13 | | - } catch {} |
14 | | - |
15 | | - if (photo) { |
16 | | - await ctx.replyWithPhoto(photo, { |
17 | | - parse_mode: 'HTML', |
18 | | - caption: text, |
19 | | - reply_markup: { |
20 | | - inline_keyboard: [ |
21 | | - response.images.length > 1 |
22 | | - ? [ |
| 26 | + msg = await reply(`Processing ${tweets.length} tweet${tweets.length > 1 ? 's' : ''}...`) |
| 27 | + } catch (e) { |
| 28 | + return reply(templates.error(e)) |
| 29 | + } |
| 30 | + |
| 31 | + for (const [_, username, tweetId] of tweets) { |
| 32 | + try { |
| 33 | + const { response } = await tweetLoader(tweetId, username) |
| 34 | + parsedTweets.push(response) |
| 35 | + await sleep(600) |
| 36 | + } catch (e) { |
| 37 | + console.log(e) |
| 38 | + return reply(templates.error(e)) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + const albums = parsedTweets |
| 43 | + .filter(tweet => tweet.images.length) |
| 44 | + .reduce( |
| 45 | + (acc, tweet, index) => { |
| 46 | + if (acc.length) { |
| 47 | + if (acc[acc.length - 1].images.length <= 10 && acc[acc.length - 1].images.length + tweet.images.length <= 10) { |
| 48 | + const lastAlbum = acc[acc.length - 1] |
| 49 | + const lastImgId = lastAlbum.images.length |
| 50 | + lastAlbum.images = lastAlbum.images.concat(tweet.images) |
| 51 | + lastAlbum.caption += `\n<a href="${tweet.url}">${lastImgId + 1}${tweet.images.length > 1 ? `-${lastImgId + tweet.images.length}` : ''} ${tweet.title}</a>` |
| 52 | + } else { |
| 53 | + acc.push( |
23 | 54 | { |
24 | | - text: `Load ${response.images.length} images`, |
25 | | - callback_data: `allimg:${username}/${tweetId}` |
| 55 | + images: tweet.images.map(image => ({ url: image, filename: 'image' })), |
| 56 | + caption: `<a href="${tweet.url}">1${tweet.images.length > 1 ? `-${tweet.images.length}` : ''} ${tweet.title}</a>` |
26 | 57 | } |
27 | | - ] |
28 | | - : [], |
29 | | - [ |
| 58 | + ) |
| 59 | + } |
| 60 | + } else { |
| 61 | + acc.push( |
30 | 62 | { |
31 | | - text: 'Share', |
32 | | - switch_inline_query: `${username}/${tweetId}` |
| 63 | + images: tweet.images.map(image => ({ url: image, filename: 'image' })), |
| 64 | + caption: `<a href="${tweet.url}">1${tweet.images.length > 1 ? `-${tweet.images.length}` : ''} ${tweet.title}</a>` |
33 | 65 | } |
| 66 | + ) |
| 67 | + } |
| 68 | + return acc |
| 69 | + }, |
| 70 | + [] |
| 71 | + ) |
| 72 | + const sentTweetsLength = parsedTweets |
| 73 | + .filter(tweet => tweet.images.length) |
| 74 | + .length |
| 75 | + |
| 76 | + const getLostTweets = () => parsedTweets |
| 77 | + .filter(tweet => !tweet.title) |
| 78 | + .map(tweet => tweet.url) |
| 79 | + .join('\n') |
| 80 | + |
| 81 | + const messageText = ` |
| 82 | +Received ${originalTweetsLength} tweet${originalTweetsLength > 1 ? 's' : ''} |
| 83 | +Sent ${sentTweetsLength} tweet${sentTweetsLength > 1 ? 's' : ''} |
| 84 | +Your success - ${((sentTweetsLength / originalTweetsLength) * 100).toFixed(0)}% |
| 85 | +${originalTweetsLength - sentTweetsLength > 0 ? `Lost tweets:\n${getLostTweets()}` : ''} |
| 86 | + ` |
| 87 | + |
| 88 | + for (const album of albums) { |
| 89 | + try { |
| 90 | + await replyWithMediaGroup( |
| 91 | + album.images.map((image, index) => ({ |
| 92 | + type: 'photo', |
| 93 | + media: image, |
| 94 | + caption: index === 0 ? album.caption : undefined, |
| 95 | + parse_mode: 'HTML' |
| 96 | + })) |
| 97 | + ) |
| 98 | + |
| 99 | + await sleep(1000) |
| 100 | + } catch (e) { |
| 101 | + return reply(templates.error(e)) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + try { |
| 106 | + await reply( |
| 107 | + messageText, |
| 108 | + { |
| 109 | + disable_web_page_preview: true, |
| 110 | + reply_markup: { |
| 111 | + inline_keyboard: [ |
| 112 | + [ |
| 113 | + { |
| 114 | + text: 'ok', |
| 115 | + callback_data: 'delete' |
| 116 | + } |
| 117 | + ], |
| 118 | + ...( |
| 119 | + originalTweetsLength - sentTweetsLength > 0 ? [ |
| 120 | + [ |
| 121 | + { |
| 122 | + text: 'Resend lost tweets', |
| 123 | + callback_data: 'tweets' |
| 124 | + } |
| 125 | + ] |
| 126 | + ] : [] |
| 127 | + ) |
34 | 128 | ] |
35 | | - ] |
36 | | - } |
37 | | - }) |
38 | | - } else { |
39 | | - await ctx.reply(text, { |
40 | | - parse_mode: 'HTML' |
41 | | - }) |
| 129 | + } |
| 130 | + }) |
| 131 | + } catch (e) { |
| 132 | + return reply(templates.error(e)) |
42 | 133 | } |
43 | | -}) |
44 | 134 |
|
45 | | -module.exports = composer.middleware() |
| 135 | + return telegram.deleteMessage(chat.id, msg.message_id) |
| 136 | +} |
| 137 | + |
| 138 | +// Echo scene |
| 139 | +composer |
| 140 | + .hears( |
| 141 | + /twitter\.com\/\S+\/status\/[0-9]+/i, |
| 142 | + onlyPrivate, |
| 143 | + async (ctx) => { |
| 144 | + return sendTweets({ |
| 145 | + chat: ctx.chat, |
| 146 | + reply: ctx.reply, |
| 147 | + message: ctx.message, |
| 148 | + telegram: ctx.telegram, |
| 149 | + replyWithMediaGroup: ctx.replyWithMediaGroup |
| 150 | + }) |
| 151 | + } |
| 152 | + ) |
| 153 | + .action( |
| 154 | + 'tweets', |
| 155 | + onlyPrivate, |
| 156 | + async (ctx) => { |
| 157 | + try { |
| 158 | + await ctx.answerCbQuery('') |
| 159 | + await sendTweets({ |
| 160 | + chat: ctx.chat, |
| 161 | + reply: ctx.reply, |
| 162 | + message: ctx.callbackQuery.message, |
| 163 | + telegram: ctx.telegram, |
| 164 | + replyWithMediaGroup: ctx.replyWithMediaGroup |
| 165 | + }) |
| 166 | + await ctx.deleteMessage() |
| 167 | + } catch (e) { |
| 168 | + return ctx.reply(templates.error(e)) |
| 169 | + } |
| 170 | + } |
| 171 | + ) |
| 172 | + |
| 173 | +bot.use(composer.middleware()) |
0 commit comments