Step 1: the bot in Telegram
A bot is a Telegram account run by a program, not a person. The official bot @BotFather issues it for free.
n8n will run the bot: a builder where an agent is assembled from blocks on a diagram. Its fair-code license (Sustainable Use License) lets you install n8n on your own server and change it, but forbids reselling it as a cloud service.
1. Open a chat with @BotFather in Telegram and send the /newbot command (new bot).
2. Enter the bot’s name. Customers see it, and you can change it later.
3. Enter a username in Latin letters; it must end in bot. You cannot change it later, so pick a clear one: the company name plus bot.
4. Copy the token that @BotFather sends. The token is the key to the bot: the Telegram documentation asks you to keep it like a password and show it to nobody. If the key leaks, you revoke it in the same place, in @BotFather.
5. In n8n, create Telegram credentials and paste the token into the Access Token field.
Telegram passes messages to n8n through a webhook: an address to which Telegram itself sends each new message. According to the n8n documentation, the address must work over HTTPS, and a bot can have only one such address. So n8n needs a server with a domain, and for testing it helps to create a second, test bot. How to install n8n is covered on the page on the n8n agent.
Step 2: model and instructions
A model is a neural network that reads a message and writes an answer. You can run an open model on your own computer or server through Ollama, a program under the MIT license. Then chats with customers do not go to a foreign vendor.
1. Install Ollama from ollama.com for macOS, Windows or Linux and download a model with the ollama pull command and its name from the Ollama library, for example ollama pull gemma4:e2b from the quickstart in the documentation.
2. In n8n, create a new workflow and make the Telegram Trigger node (start on a Telegram event) with the Message event the first node. The workflow will start on each incoming message.
3. Add the AI Agent node. In the Prompt (User Message) field, choose Define below and use an expression to point to the message text from the Telegram Trigger node.
4. Connect a model to the agent through the Ollama Chat Model node. In the Ollama credentials, the Base URL field defaults to http://localhost:11434. If n8n and Ollama run in different Docker containers, the n8n documentation advises setting the OLLAMA_HOST or OLLAMA_ORIGINS variable.
5. In the agent’s parameters, open System Message and write the instruction: who the agent is, what it answers about, what it keeps quiet about and when it calls a person. The quality of answers depends on this text more than on the choice of model.
6. Make the last node Telegram with the Send Message operation. In Chat ID, pass the chat number from the incoming message, and in Text, the agent’s answer. In the additional fields, turn off Append n8n Attribution, or an n8n signature will be added to each answer.
According to the n8n documentation, at least one tool is connected to the AI Agent node. In the next step, the base of your documents becomes that tool. A short path to a first agent without Telegram is described in the guide to a first agent in an evening.
Step 3: documents
For the agent to answer by your prices and terms, it gets a base of documents. Before answering, it searches the base for suitable fragments and answers from them, and the model is not trained. This approach is called RAG; it is covered step by step on the page on how to build RAG.
1. Make a separate loading workflow: a node that takes your files and the Simple Vector Store node in Insert Documents mode. A vector store is a database where text is stored as numbers for search by meaning.
2. Connect an embedding model to the store, for example the Embeddings Ollama node with the nomic-embed-text model, and the Default Data Loader node, which cuts the text into fragments. For most tasks, the n8n documentation advises the Recursive Character Text Splitter.
3. In the agent workflow, connect the same store in Retrieve Documents (As Tool for AI Agent) mode and describe when the agent should use it, for example: prices, terms and delivery conditions. The embedding model must be the same as at loading.
A limit: Simple Vector Store keeps data in n8n’s memory, the data is lost on restart, and the documentation advises it only for development. Before working with customers, move the base to a persistent vector database.
Step 4: testing
Testing takes no less time than building. An agent that confidently names a price that does not exist does more harm than a bot with no answer.
1. Connect the workflow to the test bot and ask questions from real customer chats of the last month.
2. Sort the answers into three groups: correct, wrong and made up. A made-up answer names a price, term or condition that is not in the documents.
3. Add to the System Message what to answer if the documents have no answer and when to hand the conversation to a manager. Then run the same questions again.
4. If the agent goes in circles, limit the number of attempts with the Max Iterations parameter: by default the model tries up to 10 times.
5. After testing, publish the workflow and connect the main bot. Remember that a bot has one webhook: the test and production n8n addresses replace each other.
Keep the list of questions. Use it to check the agent after each change to the instruction or switch of model; more on the page on agent testing.
When you need an agent in the CRM
An agent built in these four steps answers from documents but does not see your customers, deals or stock. When an answer depends on the CRM or 1C, a Russian accounting system, the agent needs tools with access to these systems, permissions and an action log. This is already a developer’s work.
The second sign: the agent collects personal data. Under 152-FZ, Russia’s personal data law, the data of Russian citizens is collected and stored in databases in Russia, and before a transfer abroad the operator files a notice with Roskomnadzor, the Russian data protection regulator. A model on your own server in Russia removes the transfer question. Our storage rules are described on the page on data security.
You can take the same path in Flowise, a visual agent builder under the Apache 2.0 license. How a Telegram agent brings requests into the CRM we showed on the page on the Telegram integration, and the 14-day launch plan is covered in a blog article.