UNPKG

@levellr/crossgram

Version:

Repost Tweets to Telegram automatically

85 lines 4 kB
"use strict"; var _a, _b, _c, _d, _e; Object.defineProperty(exports, "__esModule", { value: true }); const streamer_1 = require("../src/streamer"); const dotenv_1 = require("dotenv"); const process_1 = require("process"); (0, dotenv_1.config)(); async function startStreamer() { console.log('Starting Twitter API client'); const streamer = await streamer_1.TwitterStreamer.create({ twitterAppKey: TWITTER_CONSUMER_KEY, twitterAppSecret: TWITTER_CONSUMER_SECRET, onErrorCallback: (err) => { console.log(`Connection error: ${err}, exiting...`); (0, process_1.exit)(1); }, onConnectionClosedCallback: () => { console.log('Connection closed, exiting...'); (0, process_1.exit)(1); }, }); console.log(`Registering streams for ${TWITTER_USERNAMES.length} Twitter username to ${TELEGRAM_CHAT_IDS.length} Telegram chats via ${TELEGRAM_BOT_TOKENS.length} Telegram bots`); if (TELEGRAM_CHAT_IDS.length === 1 && TWITTER_USERNAMES.length > 1) { TELEGRAM_CHAT_IDS.push(...Array(TWITTER_USERNAMES.length - 1).fill(TELEGRAM_CHAT_IDS[0])); } if (TELEGRAM_BOT_TOKENS.length === 1 && TELEGRAM_CHAT_IDS.length > 1) { TELEGRAM_BOT_TOKENS.push(...Array(TELEGRAM_CHAT_IDS.length - 1).fill(TELEGRAM_BOT_TOKENS[0])); } for (let i = 0; i < TWITTER_USERNAMES.length; i++) { try { console.log(`Registering stream to repost tweets from @${TWITTER_USERNAMES[i]} to Telegram chat ID ${TELEGRAM_CHAT_IDS[i]} via Telegram bot ${TELEGRAM_BOT_TOKENS[i]}`); await streamer.registerStream({ twitterUsername: TWITTER_USERNAMES[i], telegramBotToken: TELEGRAM_BOT_TOKENS[i], telegramChatId: TELEGRAM_CHAT_IDS[i], }); } catch (err) { console.log(err); } } } const TWITTER_CONSUMER_KEY = (_a = process_1.env['TWITTER_CONSUMER_KEY']) !== null && _a !== void 0 ? _a : ''; const TWITTER_CONSUMER_SECRET = (_b = process_1.env['TWITTER_CONSUMER_SECRET']) !== null && _b !== void 0 ? _b : ''; const TELEGRAM_BOT_TOKENS = ((_c = process_1.env['TELEGRAM_BOT_TOKENS']) !== null && _c !== void 0 ? _c : '') .split(',') .map((u) => u.trim()); const TELEGRAM_CHAT_IDS = ((_d = process_1.env['TELEGRAM_CHAT_IDS']) !== null && _d !== void 0 ? _d : '') .split(',') .map((u) => u.trim()); const TWITTER_USERNAMES = ((_e = process_1.env['TWITTER_USERNAMES']) !== null && _e !== void 0 ? _e : '') .split(',') .map((u) => u.trim()); const missingEnvironmentVariables = []; if (!TWITTER_CONSUMER_KEY) { missingEnvironmentVariables.push('TWITTER_CONSUMER_KEY'); } if (!TWITTER_CONSUMER_SECRET) { missingEnvironmentVariables.push('TWITTER_CONSUMER_SECRET'); } if (TELEGRAM_BOT_TOKENS.length === 0) { missingEnvironmentVariables.push('TELEGRAM_BOT_TOKENS'); } if (TELEGRAM_CHAT_IDS.length === 0) { missingEnvironmentVariables.push('TELEGRAM_CHAT_IDS'); } if (TWITTER_USERNAMES.length === 0) { missingEnvironmentVariables.push('TWITTER_USERNAMES'); } if (missingEnvironmentVariables.length > 0) { console.log(`Environment variable${missingEnvironmentVariables.length !== 1 ? 's' : ''} ${missingEnvironmentVariables.join(', ')} are required but not provided, exiting...`); (0, process_1.exit)(1); } if (TELEGRAM_CHAT_IDS.length !== 1 && TELEGRAM_CHAT_IDS.length !== TWITTER_USERNAMES.length) { console.log(`The number of Twitter usernames (${TWITTER_USERNAMES.length}) and Telegram chat IDs (${TELEGRAM_CHAT_IDS.length}) does not match, exiting...`); (0, process_1.exit)(1); } if (TELEGRAM_BOT_TOKENS.length !== 1 && TELEGRAM_BOT_TOKENS.length !== TELEGRAM_CHAT_IDS.length) { console.log(`The number of Telegram chat IDs (${TELEGRAM_CHAT_IDS.length}) and Telegram bot tokens (${TELEGRAM_BOT_TOKENS.length}) does not match, exiting...`); (0, process_1.exit)(1); } startStreamer(); //# sourceMappingURL=stream.js.map