Last active
November 30, 2023 20:37
-
-
Save Ahe4d/866ef3b42cb5ca6ca7c84ff7da70828c to your computer and use it in GitHub Desktop.
Node.js program creating a bridge between Telegram and Discord.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
should use a server?