Automation

How to Build an n8n Telegram Bot with Trigger Functionality

Step-by-step guide to creating a Telegram bot using n8n with trigger and memory capabilities.

6 min read

This guide walks you through building a Telegram bot using n8n that includes trigger functionality, AI capabilities, memory for context-aware conversations, and optional web search features. By the end of this tutorial, you’ll have a fully functioning, context-aware Telegram bot that can interact dynamically with its users.

Prerequisites for Building an n8n Telegram Bot

Before getting started, ensure that you have the following:

prerequisites

  • An n8n instance installed and accessible.
  • Access to the Telegram desktop or web app to interact with BotFather for bot creation.
  • Credentials for OpenAI API and optionally SERP API for web search capabilities.
  • A basic understanding of workflow creation in n8n.

Creating a Telegram Bot with BotFather

To interact with Telegram via n8n, you first need to create a Telegram bot and obtain an API access token.

steps

  1. Open Telegram and initiate a conversation with BotFather by searching for @BotFather.
  2. Use the /newbot command to create a new bot.
  3. Follow the prompts to configure your bot's name and username (the username must end with "bot").
  4. Once created, you will receive an API access token. Make sure to copy it for use in the next steps.

Setting Up the Telegram Trigger Node in n8n

Next, configure n8n to receive messages from your Telegram bot using a trigger node.

steps

  1. Log in to your n8n instance and create a new workflow.
  2. Add the Telegram Trigger node to the workflow.
  3. Under the credentials section, create a new credential and paste the API token obtained from BotFather.
  4. Set up the trigger to listen for new messages.
  5. Test the trigger by sending a message (like /start) to your bot and verify the trigger is activated.

Integrating an AI Model with the Workflow

To add intelligence to your bot, integrate it with an AI model like OpenAI’s API for generating dynamic responses.

steps

  1. Add an AI Agent node to your workflow.
  2. Use the incoming user message from the Telegram Trigger node as the input prompt.
  3. In the AI Agent node, configure the Large Language Model (LLM) you want to use (e.g., OpenAI's GPT model).
  4. Obtain an API key from OpenAI and add it to your n8n credentials.
  5. Test the AI outputs by executing the node and verifying it generates valid responses.

Sending Responses Back to Telegram Users

Once the AI generates a response, configure the workflow to send it back to the user.

steps

  1. Add the Telegram Send Message node to the workflow.
  2. Map the chat ID dynamically from the Telegram Trigger node's output to ensure responses go to the correct user.
  3. Map the response text from the AI Agent's output to the Send Message node's message field.
  4. Disable n8n's message attribution feature by setting the Disable Attribution field to true.
  5. Test the workflow to ensure the messages are being sent back correctly.

Adding Typing Indicators to Enhance User Experience

You can add typing indicators to make the bot feel more interactive and human-like.

steps

  1. Insert a Telegram Send Chat Action node between the Telegram Trigger node and AI Agent node.
  2. Configure the action type as typing.
  3. Map the chat ID dynamically from the Telegram Trigger node's output to ensure proper context.
  4. Run the node and verify that the typing indicator appears in Telegram before the bot sends its reply.

Implementing Memory for Context-Aware Conversations

To enable your bot to remember past conversations, integrate a Memory node.

steps

  1. Add a Memory node to your workflow.
  2. Configure the node to use the chat ID from the Telegram Trigger as the key for conversation tracking.
  3. Set the Context Window Length to define how many past messages should be retained (e.g., 20).
  4. Execute a multi-step query on Telegram to test the functionality and verify the bot retains context across multiple messages.

Adding Web Search Capabilities

Integrate real-time web search functionality using the SERP API.

steps

  1. Sign up and obtain a free API key from the SERP API platform.
  2. Add a SERP API node to your workflow.
  3. Configure the node using your SERP API key in the credentials section.
  4. Map the user’s search query dynamically from the Telegram Trigger node.
  5. Test with a query like “What is the weather in New York?” and ensure the bot returns real-time data.

Personalizing Responses with Custom Prompts

You can tweak your AI model's behavior by modifying the prompts in the AI Agent node.

steps

  1. Add specific instructions to the prompt, such as, “You are a helpful agent that replies with jokes and emojis.”
  2. Execute the node and test responses. Adjust the prompt style based on your needs.
  3. Save your workflow after applying customizations.

Activating and Testing the Workflow

Finally, it’s time to save and activate your workflow for real-time usage.

steps

  1. Save and activate the workflow using n8n's activation options.
  2. Test the bot in Telegram by sending various queries.
  3. Verify the workflow’s end-to-end functionality, including triggers, AI responses, memory, and web search capabilities.

FAQ

FAQ

How do I get a Telegram bot token?

To get a Telegram bot token, go to Telegram, search for @BotFather, and start a chat. Use the /newbot command to create a bot, and you will receive the API token after completing the setup.

Why is my Telegram trigger not working in n8n?

Ensure that the token you received from BotFather is correctly configured in the Telegram Trigger node credentials. Also, verify that the workflow is activated so it can receive real-time messages from Telegram.

Can I use other AI models instead of OpenAI?

Yes, n8n workflows support other AI models. You can integrate alternatives like Llama or Grok if you have local instances of these models or credentials for them.

What is the SERP API, and is it free to use?

SERP API enables web searches and real-time data retrieval. It offers a free tier with a limited number of monthly searches, which is great for testing but may require a paid plan for higher usage.


Official reference: n8n documentation.