Skip to main content
  1. My Blog Posts and Stories/

Exploring n8n for automation

·1587 words·8 mins

Introduction #

With work getting busier and side projects piling up, I started looking for a more efficient way to automate repetitive setups. That’s when I came across n8n (thanks to this youtube video from Fireship), and decided to give it a try. Unlike rigid automation tools, n8n offers a flexible, visual, and open-source platform that connects all your favorite apps, analytics tools, and social media — all without writing code.

The main goal is to solve some issues that I have with straight up writing the program that I need. I have some telegram bots / web scrapers that I am running in the background to collect information.

Every time I needed to create a new Telegram bot or web scraper, this was my usual process:

  1. Write boilerplate code for Telegram
  2. Look up the relevant APIs
  3. Write and debug logic
  4. Set up GitHub Actions or hosting

Over time, maintaining or modifying these projects became tedious. If there are new flows that I want to update, there is also overhead spent on understanding the code and updating it to fit into the new use case that I want. These use cases may span months or even years between each modification.

There are the main issues in my workflow

  1. I need to find a place to store the results to review later
  2. A lot of time is spent debugging my code and the API to telegram / Scraping logic
  3. The time required to modify the code is high

This is where n8n comes in to hopefully alleviate some of the issues. By the end, you’ll know how to host n8n and build a working Telegram bot using n8n.

What is n8n #

n8n is a flexible workflow automation system. It makes use of interconnected drag and drop blocks and link them together to form a workflow.

n8n workflow example
n8n Workflow example

If there are any changes required from the workflow in the future, we can simply add another link to the n8n node and branch off from there.

There are more than 400 integrations and it is constantly being updated (Check out their Github).

If the integrations are not able to solve your template directly. You can browse through some of the automation templates on their official website.

A bonus is that you can self-host your n8n instance and get a free community license to use it. This is what caught my eye initially and made me start using it eventually.

Setting up n8n #

Before we can use n8n, we have to deploy it first.

Coolifyn8n InstanceCloudflare Tunneln8n InstanceCloudflare TunnelMeRequest (HTTPS)Request (HTTP)Response (HTTP)Response (HTTPS)MeArchitecture of My n8n Setup

I plan to be able to access my n8n instance from everywhere without the need to port-forward it.

Install with Coolify #

Coolify n8n Template
Coolify n8n Template

As I previously had Coolify installed, I made use of the n8n coolify template to install my n8n instance.

Webhook Environment Variable #

Set Webhook URL to use HTTPS
Set Webhook URL to use HTTPs

As the main URL entry point to the n8n instance is through HTTPS, please remember to change your WEBHOOK_URL environment variable to use the HTTPS url instead of the HTTP url setup by default.

This setting is similar for those who are running this directly using Docker or a Docker-Compose file.

Coolify Domain Configuration #

Coolify Configuration
Coolify domain configuration

Set the domain for your n8n stack to http://<domain>:<n8n port> and restart your instance.

Note: If you changed any configuration, remember to restart your instance after for the changes to take place.

If all of the above is set up correctly, you should be able to see your n8n page on your domain.

Config Cloudflare Tunnel #

Copy token
Copy the Token from Cloudflare

Visit cloudflare and create a tunnel. Click on the copy button to get the token,

Create Cloudflared
Create Cloudflared

Create a cloudflared instance using coolify to allow cloudflare to proxy the traffic through cloudflared

Insert token
Insert token in the env variable and start the instance

Paste the token into the environment variable and start the container.

Set the tunnel to use HTTP
Set Cloudflare tunnel to use HTTP

As the connection from the tunnel to the n8n instance is through HTTP, you have to configure your cloudflare tunnel to use HTTP to connect to your n8n instance (Using the host ip address)

If everything is set up correctly, you should be able to access your n8n instance from the subdomain that you have configured.

Testing the feasibility of n8n #

To ensure that n8n is right for me, I decided to make a telegram bot that can create Polls based on the provided dates. If it can create this bot, it should be able to do most of the other telegram related workflows that I have. This bot can also help me create polls more efficiently when I want to meet up with my friends instead of manually needing to type in the dates for the meetup 1 by 1.

This telegram bot should be able to:

  1. Listen to messages on Bot PMs and Groups
  2. Parse Commands directed to the bot
  3. Create a Poll with the given description / options given by the users

Without further ado, let’s dive into the implementation

Parts of a n8n program #

A n8n program has the following nodes to play around with.

Node TypeDescription
TriggersThis triggers your program and starts the n8n workflows
ActionsThis are the actions that takes in an input and completes an action
Data TransformersTransform data from 1 format to another
FlowsBranching / Looping and reducing logic
CoresRun code, make http requests & listen to webhooks

With these building blocks, we can start creating the Telegram bot.

Writing the Telegram Bot #

Creating the trigger #

For a start, we should create a Telegram Trigger to listen for messages from the Telegram Server for our bot.

Note: As of the time of writing this blog post, the Telegram trigger node only supports Webhook mode. This requires an exposed HTTPS endpoint to receive the webhook.

As a workaround, you can use a schedule trigger to poll the Telegram API for new messages.

Telegram Trigger
Telegram Trigger

There are also other triggers which we can use but for this case, the Telegram Message trigger is the most appropriate.

Fleshing out the logic #

In this section, instead of going step by step, I will just go through the different components which are used.

Check if bot is mentioned
Check out if bot is mentioned

To check out if the bot is mentioned, we can check if it is either a direct message from a user (Positive chat id) OR a mention (Tagged the bot)

Check Command logic
Check command logic

For cases like this where we need to have custom logic for checking commands, we can always escape into Python/Javascript to implement our logic. This can be used as a fallback for cases where we n8n does not have a dedicated block for the logic.

Check if there are errors
Error check

The next step is to check for errors and return a message to the user if there are any wrong format and return a help message

Command Switch Statement
Command Switch

We can also make use of the switch command to differentiate between the different commands to implement each of the logic differently.

Reference other workflows
Reference other workflows

Last but not least, we can reference other workflows that we have made earlier to may reuse more convenient. This will make future iterations of n8n workflows more simpler by referencing similar logic from other workflows.

With the information you have now, you can create any form of bot you want based on the available nodes that we have done so far.

Final output #

Final n8n Workflow
Final n8n Workflow for my Telegram Bot

This is what the final workflow looks like in n8n. We have successfully created a telegram bot that can:

  1. Listen to telegram to see if there are messages received from users OR messages in a group tagging it.
  2. Parse the commands and reply to the messages based on the inputs.

This is definitely a huge success and shows that most workflows can be implemented in n8n.

For cases which are not natively supported, we can simply use the escape hatch to implement them in Python/Javascript.

If you want to use the Bot to create your own schedules, feel free to check it out at: @when_are_you_free_bot to create your own polls for scheduling.

Limitations #

Despite the ease of implementation for the telegram bot, using n8n for your implementation also comes with downsides.

  1. The response time of the bot is longer than using a programming language directly.
  2. For cases where there are multiple branches, managing the branches can become a chore very quickly.
  3. Overkill for cases where you only have very few use cases, n8n uses more compute compared to a simple program that you write up yourself.

These trade-offs are worth considering depending on your project size and needs.

Conclusion #

In this blog post, we have played with n8n and showed off the different features that it can handle. Overall, n8n proved capable of handling complex workflows like Telegram bots while significantly reducing setup and maintenance time. Moving forward, hopefully I can migrate my other telegram bot flows over and make them easier to maintain.

I will continue using n8n and write a follow up post after some time to take a look at its efficacy and other tips/tricks I have discovered while I am using it.