How to Secure your telegram and n8n Agents for Authorized Users Only

One of the common ways to build agents lately is by having a telegram bot that talks to an agent on N8N and replies back and forth.

How to Secure your telegram and n8n Agents for Authorized Users Only
Photo by FlyD / Unsplash

One of the more popular ways to build agents lately is by having a telegram bot that talks to an agent on n8n and replies back and forth.

This works fantastic and I've built a lot of agents for both myself and clients that work that way and handle a number of tasks, but here's a big issue with Telegram bots:

They are not private!

If anyone finds the name of your bot in telegram then they can message it and thus connect to your agent.

How do I fix this?

There are several methods to lock this down but the simplest method to do this is the one I'll cover here today.

  1. In your agent work flow in n8n, add an if block right after a message is received from telegram that checks that the user id is a permitted user and if so, continue, if not, stop or even return a message about being unauthorized.
Add the if block
  1. In the if block, check that the user ID the message is from is your user's ID, you can also change this to contains if an array of authorized user IDs as well.
Check if the user id is authorized or not
  1. Save the flow and that's it, your agent now only allows specific users to talk to it.

Conclusion

This is a simple low code fix and it works, as I said there are other methods that can be used as well but this is a simple one to start with.

This isn't just for n8n but for any agents that talk to telegram.