Blog · · 4 min read

How to build an AI Telegram bot with n8n (step by step)

A guide to building a Telegram bot in n8n that replies with AI and remembers the conversation: creating the bot with BotFather, the Telegram Trigger node, an AI agent with memory and the automatic reply.

Telegram is one of the fastest ways to try out a real AI assistant: creating a bot is free, nobody has to approve anything, and within ten minutes you can be chatting with it from your phone. It works for customer support, for a community group, or as a personal assistant that answers questions or takes notes for you.

In this guide we build it with n8n: Telegram tells n8n about every new message, an AI agent answers it with the earlier conversation in mind, and n8n sends the reply back to the same chat.

How it works, in one sentence

Someone messages your bot on Telegram; n8n receives the message, passes it to an AI model along with your instructions and the previous conversation, and sends the answer back to the chat.

What you need

  • A Telegram account.
  • n8n running and reachable from the internet over HTTPS, because Telegram only delivers messages to secure addresses (if you don't have it yet, see how to install n8n for free with Docker).
  • An API key for an AI model, for example from OpenAI.

Step 1: create the bot with BotFather

In Telegram, search for @BotFather (the official account, with the verified badge) and send it /newbot. It will ask for two things:

  • A display name, for example "Your Business Assistant".
  • A username that must end in bot, for example YourBusinessBot.

When you're done it gives you a token, a long string of numbers and letters. It's your bot's key: keep it safe and don't share it, because anyone who has it can control the bot.

Step 2: connect Telegram to n8n

In n8n, create a new workflow and add the Telegram Trigger node. Create a new Telegram credential and paste the token into Access Token.

Under Trigger On, choose Message. From now on, every message the bot receives enters the workflow here, with the text, who sent it and the chat ID.

Step 3: the AI agent

Connect an AI Agent node to the trigger and set it up like this:

  • Source for Prompt: choose Define below and put the expression {{ $json.message.text }} in the prompt field, which is the text the person wrote.
  • Chat Model: add OpenAI Chat Model (or another provider) with your API key.
  • Memory: add Simple Memory. Because the message doesn't come from n8n's own chat, you need to tell it how to keep conversations apart: under Session Key, choose to define it yourself and use {{ $json.message.chat.id }}. That way each person (or each group) gets its own memory.

Step 4: the bot's instructions

In the agent's options, under System Message, explain who the bot is and what it can do. A short example for a business:

You are the Telegram assistant for [BUSINESS NAME].
Reply in the user's language, in 2-4 sentences, in a friendly tone.
Use only this information:
- Opening hours: [HOURS]
- Services and indicative prices: [LIST]
- Contact: [PHONE / EMAIL]
If you don't know something, or they want to book or get a quote,
ask for a phone number or email and say the team will get back to them.
Never make up prices, timelines or availability.

If you want to fine-tune it, the prompt for an AI customer service agent explains how to write these instructions in more detail.

Step 5: send the reply

After the agent, add a Telegram node with the Send a text message action (Resource: Message, Operation: Send Message) and the same credential:

  • Chat ID: {{ $('Telegram Trigger').item.json.message.chat.id }}, so it replies in the same chat the message came from.
  • Text: {{ $json.output }}, which is the answer the agent generated.
  • Under Additional Fields, add Append n8n Attribution and switch it off; otherwise every message ends with a line saying it was sent with n8n.

Step 6: test and activate

Click to test the workflow and send your bot a message from Telegram: you should see the message arrive in n8n and the reply show up in the chat. Once it works, save and activate the workflow so it always replies, even with the editor closed.

Common mistakes

  • The bot receives nothing. Most often n8n isn't reachable from the internet or doesn't have HTTPS. Telegram can't deliver messages to a local address.
  • It works in testing but not when active (or the other way round). A Telegram bot can only send its messages to one address at a time. If the same bot is used in two active workflows, or you test while the workflow is active, one steals the messages from the other. Use a separate bot for testing.
  • The bot remembers nothing. Check that the memory's Session Key uses the chat ID and not a fixed value.
  • Messages without text. If someone sends a photo, a voice note or a sticker, message.text arrives empty. You can add an If node before the agent to reply with a friendly notice when there's no text.
  • Personal data. If the bot collects phone numbers or emails, mention it in your privacy policy and only ask for what you need.

Ideas to go further

Shortcut: agents that are already built

The WhatsApp support agent in the AgentKiosk pack comes with a ready-written customer service prompt, with rules to avoid making things up and to hand over to a person when needed; you can reuse that same prompt in your Telegram bot. Everything comes as an n8n template you can import.

If you'd like to start with something free, download the WhatsApp agent and take a look at 7 AI automations for small businesses.