Overview

Discover how to leverage the power of the n8n workflow and Telepilot to capture and manage all events from your Telegram account in real-time.

  1. Simplest workflow that is capturing all events and writing them directly into database: TelePilot__Listen_to_Events.json

  2. Specific workflow, capturing only “updateNewMessage” events and extracting few fields which are saved into DB: TelePilot__Listen_to_Events_with_Mapper.json

The n8n Workflow

The core workflow consists of two nodes and an optional third one:

  • Telegram Trigger: This is the core Telepilot node that receives messages and events from Telegram.
  • MongoDB Insert: This node is responsible for storing the incoming events into a MongoDB database.
  • Optional Code Node: If necessary, you can include a Code node to manipulate or transform the structure of incoming JSON data before it’s written into the database.

Capture Every Telegram Event with the Telepilot Trigger Node

Telepilot Trigger Node

Configuration of Telepilot Trigger node to allow all events

The Telepilot Trigger node uses the Napi::AsyncWorker to continually execute tdlib’s td_json_client_receive method, ensuring it receives new updateEvents as they occur.

The node can either provide all events or filter events based on your preferences:

  • The Telepilot Trigger node can be preconfigured to receive only specific events of interest from your Telegram account.
  • Without any preconfigured filtering, the node will capture and deliver all events.

To configure the Telepilot Trigger node to capture all Telegram events:

  • Add it to the canvas
  • Enter the node configuration and ensure the Events field is empty

MongoDB Insert Node

Configuration of MongoDB Insert node to save all keys

This node captures and inserts the events into your MongoDB database. You need to specify the JSON keys that should be written into the DB. To simply include all keys, you can use the following JavaScript Expression:

{{ Object.keys($json).join(",") }}

To configure the MongoDB Insert node to write all fields into DB:

  • Add it to the canvas and connect with previous node
  • Specify collection name where all the data will be written to - put into Collection input field all_events
  • Put following Expression into Fields input field: {{ Object.keys($json).join(",") }}

Optional Code Node for Mapping/Transforming JSON Events

Code Node for Mapping/Transforming JSON Events

In certain scenarios, you might want to only save a portion of the information you receive from each Telegram event. In such cases, you can add a Code node between the Trigger and Insert nodes to map or transform the incoming JSON using custom JavaScript.

Here is the example for mapper node that is compatible with “updateNewMessage” events. You would need to enhance it if you want to support more events:

// Get the first item from input (usually the data from the previous node)
let item = items[0].json;

// Extract the required fields
let result = {
    "_": item._,
    "id": item.message.id,
    "senderType": item.message.sender_id._,
    "user_id": item.message.sender_id.user_id,
    "chat_id": item.message.chat_id,
    "text": item.message.content.text.text,
};

// Output the transformed item
return [
    {
        json: result
    }
];

Testing

Activate your workflow, send some test messages and watch how your database if growing!

Wrapping It Up

In this tutorial, we’ve walked you through an n8n workflow that utilizes the Telepilot Trigger node to capture all events from your Telegram account, efficiently stored them in a MongoDB database, and optionally transformed the data with a Code node. This workflow is a powerful way to automate interactions within Telegram and leverage the potential of real-time data capture.

Whether you’re tracking all events or focusing on specific ones, this workflow will save you time and give you the insights you need. It’s all about making Telegram work for you, and with n8n and Telepilot, the possibilities are virtually limitless.

Need help or have questions about the tutorial? We’re here for you! Join our Telepilot Telegram Channel to stay updated, get support, and be part of the Telepilot community. We’re continuously working on new features and workflows, and we’d love for you to be a part of our journey.

Happy automation with Telepilot and n8n!