UNPKG

@levellr/crossgram

Version:

Repost Tweets to Telegram automatically

55 lines 1.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TweetDispatcher = void 0; const unshortener_1 = require("./unshortener"); class TweetDispatcher { constructor() { this.botsByChatId = new Map(); } registerChat(chatId, bot) { this.botsByChatId.set(chatId, bot); } unregisterChat(chatId) { this.botsByChatId.delete(chatId); } async dispatch(streamResult) { const unprocessedTweet = streamResult.data.text; const text = await unshortener_1.TwitterLinkUnshortener.unshortenLinks(unprocessedTweet); const mediaArray = TweetDispatcher.extractMediaArrayFromTweetStreamResult(streamResult); for (const chatId of this.botsByChatId.keys()) { const bot = this.botsByChatId.get(chatId); if (!bot) { continue; } try { if (mediaArray.length > 0) { await bot.telegram.sendMediaGroup(chatId, mediaArray); } await bot.telegram.sendMessage(chatId, text); } catch (err) { console.log(`Error sending Tweet to chat ${chatId}: ${err}`); } } } static extractMediaArrayFromTweetStreamResult(streamResult) { var _a; if (!((_a = streamResult.includes) === null || _a === void 0 ? void 0 : _a.media) || streamResult.includes.media.length === 0) { return []; } const mediaArray = []; for (const media of streamResult.includes.media) { if (media.url && (media.type === 'photo' || media.type === 'video')) { mediaArray.push({ type: media.type, caption: media.alt_text, media: media.url, }); } } return mediaArray; } } exports.TweetDispatcher = TweetDispatcher; //# sourceMappingURL=dispatcher.js.map