Skip to content

Instantly share code, notes, and snippets.

@Ahe4d
Last active November 30, 2023 20:37
Show Gist options
  • Save Ahe4d/866ef3b42cb5ca6ca7c84ff7da70828c to your computer and use it in GitHub Desktop.
Save Ahe4d/866ef3b42cb5ca6ca7c84ff7da70828c to your computer and use it in GitHub Desktop.

Revisions

  1. Ahe4d revised this gist May 10, 2018. No changes.
  2. Ahe4d revised this gist May 10, 2018. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions telegram-bridge.js
    Original file line number Diff line number Diff line change
    @@ -3,9 +3,9 @@ const TelegramBot = require('node-telegram-bot-api'); // https://github.com/yago
    const Discord = require('discord.js'); // https://github.com/discordjs/discord.js

    /* Values:
    token: Telegram bot token (logging into Telegram's API)
    token2: Discord bot token (logging into Discord's API)
    channelid: Discord channel ID (channel the Discord bot can access)
    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 = '';
  3. Ahe4d created this gist May 10, 2018.
    43 changes: 43 additions & 0 deletions telegram-bridge.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    // 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)
    token2: Discord bot token (logging into Discord's API)
    channelid: Discord channel ID (channel the Discord bot can access)
    */
    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);