Simple Telegram Bot in RocketChat

Relay messages between Telegram and Rocket.Chat in a specified room of your choice

Simple Telegram Bot in RocketChat

1. Background

Due to certain network restraints, some users may find it troublesome to use some type of instant message Apps, such as Telegram, Line, WhatsApp, WeChat, Facebook Messenger, etc. In this case, this article will show you an example of using a simple relay communication between Rocket.Chat and Telegram configuring a basic bot. The guide below shows a community example of how this can be achieved with webhooks. This gives you the possibility of relaying messages between Telegram and Rocket.Chat in a specified room of your choice.

2. On Telegram

  • Log in to your Telegram account on your mobile or using a browser.
  • Add and open a conversation with the user BotFather.
  • Click start.
  • Send /newbot to start creating a new bot.
  • Set the bot's username. Telegram successfully creates the bot and shows how to access it.
  • Copy the token provided, it is needed for the configuration.

3. On RocketChat

  • Create a channel on RocketChat for incoming and outgoing conversations.
  • Create a user with bot role, to be used for the relay.
  • Creating an Incoming Telegram webhook in RocketChat. The incoming webhook is responsible for relaying messages from Telegram into RocketChat into a specific Channel. Go to Administration > Workspace > Integrations in RocketChat.
  • Create a new Incoming Webhook. Enable the integration
  • Set the webhook integration name.
  • Set the Post to Channel as the channel created above.
  • Set Post as, as the user created above.
  • Enable the script and paste the following code:
class Script {
    addQuotePrefix(str) {
        let tmp = str.split('\n'),
            res = [];

        for (const frag of tmp) {
            res.push(`> ${frag}`);

        return res.join('\n');

    process_incoming_request({ request }) {
        // console.log(request.content);
        if ('edited_message' in request.content) {
            request.content.message = request.content.edited_message;
        let from = request.content.message.from;
        let who = from.username
        let icon_url = '/avatar/' + from.username + '.jpg'
        if(!who)  {
          if (from.first_name && from.last_name) {
            who = `${from.first_name} ${from.last_name}`
          } else if (from.first_name) {
            who = from.first_name
          } else {
            who = from.last_name
          icon_url = `/avatar/${request.content.message.from.first_name}.jpg`
        let body = request.content.message.text

        if(!body) {
          if(request.content.message.hasOwnProperty("sticker") && request.content.message.sticker.emoji) {
            // It's a sticker
            body = request.content.message.sticker.emoji
        } else {
           return {}

        if(request.content.message.reply_to_message) {
              var quotedMessage = 
                "*" +
                request.content.message.reply_to_message.from.username +
                "*\n" +

              quotedMessage = this.addQuotePrefix(quotedMessage);
            body = quotedMessage + '\n' + body;

        return {
            content: {
                username: who,
                icon_url: icon_url,
                text: body
  • Save the integration.
    This creates a new incoming integration with a webhook URL and token provided.

4. On any browser

Setting Telegram webhook.

  • Copy the incoming webhook URL provided by Rocket.Chat after saving.
  • Change the following URL with yourTelegramBotToken and Incoming webhookURL and open it on your browser.<my-telegram-authorization-token>/setwebhook?url=<Incoming_Webhook_Link_from_Rocket.Chat>
  • A response indicating success is sent which looks like this:
"ok": true,
"result": true,
"description": "Webhook was set"

5. Test Telegram Incoming Integration

Test your incoming Webhook by sending a telegram message to the Telegram bot.
The message sent gets posted in channel by the user you specified in the incoming webhook configuration page.

6. Outgoing messages

The instruction of sending outgoing messages from RocketChat to Telegram can be found at: Creating a Telegram Group with Bot access
Personally I’m not using it for the reason of: (i) user experience of others (ii) security. You can try it yourself if you want to.

Copyright statement: Unless otherwise stated, all articles on this blog adopt the CC BY-NC-SA 4.0 license agreement. For non-commercial reprints and citations, please indicate the author: Henry, and original article URL. For commercial reprints, please contact the author for authorization.