Overview

Discover how TelePilot enables you to streamline your searches through Telegram and seamlessly save the results for future use.

Whether you prefer to build your own workflow or directly download and import our pre-designed workflow file, we’ve got you covered: TelePilot__Search

The n8n Workflow

Searching for Telegram Chats in n8n workflow

We’ll be using the following straightforward algorithm:

  • Execute the searchPublicChats Telegram action using the Telegram CoPilot node, which returns a list of chat_ids.
  • Carry out some necessary utility work and transformations.
  • Run getChat for each member of the returned list.
  • Finally, save the results in the database.

Ready to delve into the details? Let’s go!

Set Node

Configure your search prompt in Set Node

Start by using the Set node to initialize your search prompt. This practice makes your input/reusable variables easy to change and maintain. Alternatively, you can utilize an incoming Webhook node or call this workflow from a parent one.

Set Node configuration:

  • Add a new “String” Value.
  • Ensure its Name is search_value.
  • Input Javascript developer.

Telegram CoPilot: Search Public Chats Node

Providing search query into your Telergam Search Public Chats Node

There are two main actions for searching in Telegram: searchPublicChat and searchPublicChats. The former requires a username and returns the ID of a channel, group, or user, while the latter accepts any string and attempts to match it with a username, title, and description. We’ll be using the latter.

To set up the node, input the search_query from the Set Node into the Query field. Your configuration should look like this: {{ $node["Set: search_query"].json["search_query"] }}.

Code Node for JSON Mapping

Using Code Node to pull up chat_ids

The Code Node is essential for data transformation. The previous node returns a JSON like this:

[{"_": "chats",
"total_count": 10,
"chat_ids": [ -1111469926810, -1121234788613 ]}]

To make it work for us, we need to bring chat_ids one level up, enabling us to iterate over the values in the chat_ids array. If we don’t do this, all downstream nodes will only be executed once, limiting our ability to get details about all chats.

Split in Batches Node

Use Split in Batches to iterate over list of Telegram chat_ids

We will use Split in Batches to loop through the data. Make sure to specify a Batch Size of 1 to process every entry.

Important: The Split in Batches node requires a feedback connection. When your workflow is ready, connect the output of your last node with the input of the “Split in Batches” node to initiate the iterations.

Telegram CoPilot: Get Chat Information

Using Telegram Get Chat to retrieve information about Telegram Cats

The Telegram CoPilot: Get Chat node fetches information about a Telegram chat based on its ID. We will use it to retrieve details for every chat_id received from the SearchPublicChats node.

Input the following configuration:

  • Chat ID should use the following Expression to read value from the previous step in a loop: {{ $json.chat_id }}.

MongoDB Insert Node

Saving Telegram Chat Search Result into MongoDB search collection

The MongoDB Insert node captures and stores your search results in your MongoDB database. You’ll need to specify the JSON keys that you’d like to write into the database.

Set up the MongoDB Insert node to store all fields in the database by:

  • Specifying search as the collection name where all the data will be stored in the Collection field.
  • Including all keys by inputting the following expression into the Fields field: {{ Object.keys($json).join(",") }}.

Time to Test

Execute your workflow and then check your MongoDB database! You’ll now find your search results saved and ready for your automation tasks!

Wrapping up

And that’s it! You have now successfully configured a TelePilot search workflow using n8n. This powerful tool allows you to easily search through Telegram channels, groups, or users and efficiently store your search results in MongoDB for future automation tasks.

If you run into any issues or if you have any questions, we’re here to help. Join our growing community over on Telegram: https://t.me/telepilotco Here we regularly share tips, tricks, and updates on everything TelePilot.

We hope this tutorial was helpful and has inspired you to explore the other powerful features that TelePilot has to offer. Stay tuned for more tutorials and don’t forget to share your own creative workflows in our Telegram group. Happy automating!