Learn how to forward Jotihunt Telegram messages to Discord or WhatsApp
This guide will show you how to create a Node.js application that forwards messages from the Jotihunt Telegram bot to Discord or WhatsApp. This can help teams stay updated on Jotihunt events through their preferred communication channels.
Enable “Message Content Intent” under Privileged Gateway Intents
To get the channel ID, enable Developer Mode in Discord (Settings > App Settings > Advanced)
Right-click the target channel and click “Copy ID”
WhatsApp Group Setup:
Note the exact name of your WhatsApp group (case sensitive)
You must be a member of the group you want to forward messages to
Make sure to keep your WhatsApp connected on your phone while using the forwarder
Add these values to your .env file:
.env
Copy
# Telegram API credentialsTELEGRAM_API_ID=123456 # From my.telegram.org/appsTELEGRAM_API_HASH=abcdef123456abcdef12 # From my.telegram.org/appsTELEGRAM_STRING_SESSION= # Leave empty, will be generated on first runTELEGRAM_CHAT_USERNAME=@Jotihunt_bot # The Jotihunt bot username (@Jotihunt_bot)# Discord Bot credentials (optional)DISCORD_TOKEN=your_discord_bot_token # From Discord Developer PortalDISCORD_CHANNEL_ID=123456789012345678 # Right-click channel > Copy ID# WhatsApp Configuration (optional)WHATSAPP_GROUP_NAME=My Group Name # Exact group name, case sensitive
Never commit your .env file to version control. Make sure to add .env to your .gitignore.
3
Create the forwarder code
Create a new file called index.js in your project root and add the following code:
index.js
Copy
require('dotenv').config();const { TelegramClient } = require('telegram');const { StringSession } = require('telegram/sessions');const { NewMessage } = require('telegram/events');const { Client: DiscordClient, GatewayIntentBits } = require('discord.js');const { Client: WhatsAppClient, LocalAuth } = require('whatsapp-web.js');const qrcode = require('qrcode-terminal');// Load env variablesconst {TELEGRAM_API_ID,TELEGRAM_API_HASH,TELEGRAM_STRING_SESSION,TELEGRAM_CHAT_USERNAME,DISCORD_TOKEN,DISCORD_CHANNEL_ID,WHATSAPP_GROUP_NAME} = process.env;if (!TELEGRAM_API_ID || !TELEGRAM_API_HASH || !TELEGRAM_CHAT_USERNAME) {console.error('Missing Telegram configuration in .env');process.exit(1);}// ----------------------- TELEGRAM -----------------------const telegramClient = new TelegramClient(new StringSession(TELEGRAM_STRING_SESSION || ''),parseInt(TELEGRAM_API_ID, 10),TELEGRAM_API_HASH,{ connectionRetries: 5 });// ----------------------- DISCORD -----------------------let discordClient;if (DISCORD_TOKEN && DISCORD_CHANNEL_ID) {discordClient = new DiscordClient({intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages]});discordClient.once('ready', () => {console.log(`Discord: Logged in as ${discordClient.user.tag}`);});discordClient.login(DISCORD_TOKEN).catch(err =>console.error('Discord login failed:', err));}// ----------------------- WHATSAPP -----------------------let whatsappClient;let whatsappGroupId = null;if (WHATSAPP_GROUP_NAME) {whatsappClient = new WhatsAppClient({authStrategy: new LocalAuth(),puppeteer: { headless: true }});whatsappClient.on('qr', qr => {console.log('Scan this QR code to log into WhatsApp:');qrcode.generate(qr, { small: true });});whatsappClient.on('ready', async () => {console.log('WhatsApp: Client is ready');try { const chats = await whatsappClient.getChats(); const group = chats.find( chat => chat.isGroup && chat.name === WHATSAPP_GROUP_NAME ); if (!group) { console.error(`WhatsApp: Group "${WHATSAPP_GROUP_NAME}" not found.`); } else { whatsappGroupId = group.id._serialized; console.log(`WhatsApp: Found group "${group.name}" with ID ${whatsappGroupId}`); }} catch (err) { console.error('WhatsApp group lookup failed:', err);}});whatsappClient.initialize();}// ----------------------- MESSAGE HANDLING -----------------------function formatMessage(message) {let text = message.text || message.message || '';const entities = message.entities || [];// Format Telegram message to use bold for Discord/WhatsAppentities.sort((a, b) => b.offset - a.offset);for (const entity of entities) {const start = entity.offset;const end = entity.offset + entity.length;if (entity.className === 'MessageEntityBold') { text = text.slice(0, start) + '**' + text.slice(start, end) + '**' + text.slice(end);}}return text;}// ----------------------- MAIN -----------------------async function start() {console.log('Connecting to Telegram...');await telegramClient.start();console.log('Telegram: Logged in');if (!TELEGRAM_STRING_SESSION) {console.log('Telegram: Save this session string to .env:');console.log(telegramClient.session.save());}telegramClient.addEventHandler(async event => { const message = event.message; if (!message || !message.text) return; const content = formatMessage(message); // Forward to Discord if (discordClient) { try { const channel = await discordClient.channels.fetch(DISCORD_CHANNEL_ID); await channel.send(content); console.log('Discord: Message forwarded'); } catch (err) { console.error('Discord: Failed to send message:', err); } } // Forward to WhatsApp if (whatsappClient && whatsappGroupId) { try { await whatsappClient.sendMessage(whatsappGroupId, content); console.log('WhatsApp: Message forwarded'); } catch (err) { console.error('WhatsApp: Failed to send message:', err); } }},new NewMessage({ chats: [TELEGRAM_CHAT_USERNAME] }));}// ----------------------- SHUTDOWN HANDLER -----------------------async function shutdown() {console.log('Shutting down...');try {await telegramClient.disconnect();console.log('Telegram disconnected');} catch (err) {console.error('Telegram disconnect failed:', err);}if (discordClient) {discordClient.destroy();console.log('Discord client destroyed');}if (whatsappClient) {await whatsappClient.destroy();console.log('WhatsApp client destroyed');}process.exit(0);}process.once('SIGINT', shutdown);process.once('SIGTERM', shutdown);start().catch(err => {console.error('Startup error:', err);process.exit(1);});
By implementing this Telegram forwarder, you can ensure your team stays updated on Jotihunt events through their preferred communication channels. Remember to monitor the forwarder’s performance and handle errors appropriately.
This tutorial is inspired by the jotihunt-telegram-forwarder project by ScoutingScherpenzeel, which provides a Python implementation of similar functionality. This Node.js version offers an alternative approach while maintaining the same core functionality.