Skip to content

Instantly share code, notes, and snippets.

@Ahe4d
Last active November 30, 2023 20:37
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Ahe4d/866ef3b42cb5ca6ca7c84ff7da70828c to your computer and use it in GitHub Desktop.
Save Ahe4d/866ef3b42cb5ca6ca7c84ff7da70828c to your computer and use it in GitHub Desktop.
Node.js program creating a bridge between Telegram and Discord.
// Telegram-Discord bridge
const TelegramBot = require('node-telegram-bot-api'); // https://github.com/yagop/node-telegram-bot-api
const Discord = require('discord.js'); // https://github.com/discordjs/discord.js
/* Values:
token: Telegram bot token (logging into Telegram's API, you can get this from @BotFather on Telegram)
token2: Discord bot token (logging into Discord's API, you can get this from Discord's developer docs > my apps > app)
channelid: Discord channel ID (channel the Discord bot can access, right clicking a channel and clicking copy ID with developer mode enabled)
*/
const token = '';
const token2 = '';
const channelid = '';
/* Bots:
bot: Telegram bot
bot2: Discord bot
*/
const bot = new TelegramBot(token, {polling: true});
const bot2 = new Discord.Client();
// Matches "/echo [whatever]" in Telegram chat
bot.onText(/\/echo (.+)/, (msg, match) => {
// 'msg' is the received Message from Telegram
// 'match' is the result of executing the regexp above on the text content
// of the message
const chatId = msg.chat.id;
const resp = match[1]; // the captured "whatever"
// send back the matched "whatever" to the Telegram chat
bot.sendMessage(chatId, resp);
});
// Listen for any kind of message in Telegram. There are different kinds of messages.
// Check out node-telegram-bot-api's GitHub & the Telegram API docs for more info
// https://github.com/yagop/node-telegram-bot-api & https://core.telegram.org/api
bot.on('message', (msg) => {
const chatId = msg.chat.id;
console.log("we got a message from telegram");
bot2.channels.get(channelid).send("[Telegram] **" + msg.from.first_name + " (@" + msg.from.username + "):** " + msg.text);
console.log("sent that message to discord");
});
bot2.login(token2);
@shohibi
Copy link

shohibi commented Mar 30, 2022

should use a server?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment