Telegram is probably the most developer-friendly mainstream messenger. It has an incredible bot ecosystem that allows developers to create full-scaled products, such as chatbots, group moderators, custom tools, multiplayer games, and much more.

Today we’ll show how n8n helps integrate Telegram with cutting-edge AI tools and guide you through the ins and outs of making an AI chatbot.

Example of an AI-powered Telegram bot
Example of an AI-powered Telegram bot

After reading this tutorial you’ll be able to create a similar Telegram chatbot, powered by GPT-3 or GPT-4. On top of that, this bot is able to send you AI-generated images.

Cool, huh?

Is making a Telegram bot easy?

Absolutely yes! Making a Telegram bot with no-code or low-code tools is very easy nowadays.

Of course, different platforms have different functionality and it’s important to understand the nuances of each method.

For example, most specialized no-code bot builders are rather limited and tend to own the data that is transferred via the bot. Tools such as Zapier, on the other hand, quickly rack up prices if your bot becomes popular.

Contrary to these tools, n8n combines the best of the approaches:

  • It’s easy to make Telegram bots thanks to Telegram trigger and Telegram node
  • Maintaining the Telegram bot with n8n is hassle-free since all hosting topics are covered by n8n
  • In case of complex logic is needed, you are able to tweak the Telegram bot to do whatever you need with custom JS coding and HTTP requests to the Telegram API platform
  • You can even create a multilingual Telegram bot in n8n with minimal redundancy to your workflow
  • Finally, n8n doesn’t lock you in and you are completely in control of the data transferred through your bot.

So, let’s get started and create a simple AI bot for Telegram!

How to build a Telegram bot with n8n?

In this tutorial, we’ll show you how to build a simple chatbot using the GPT3/4 model.

We’ll configure this bot to react more naturally with the help of emojis. We’ll also handle unsupported commands and provide error messages. Finally, the bot will be able to create and send you AI-generated images with DALL-E 2.

Bear in mind that today’s chatbot won’t remember past messages. You’ll need some sort of database, a vital component for storing message history.

Step 0. Add a new bot in Telegram

Telegram allows adding a new bot in just a minute!

The official way is by using BotFather. Start this bot and follow 2 easy steps:

  • First, run /newbot command and give your bot a human-readable name
  • Next, provide a unique bot name. Telegram requires that a name should contain “bot” or “_bot” at the end of a name.

After that, you’ll receive a confirmation with a secret API token.

You can also customize your bot via BotFather later: provide a description, add an avatar, set up a menu with bot commands. This is not essential for the scope of this tutorial.

Register new Telegram bot in just a minute
Register new Telegram bot in just a minute

The next steps of creating a Telegram bot are up to you: program the bot from scratch, use a library from a programming language of your choice, turn to bot-making SaaS or use a low-code platform such as n8n to extend your bot's capacity.

Step 1. Prepare your n8n instance and get the OpenAI API key

After you registered a new bot on the Telegram platform, there are a few preparatory steps to take:

💡
If you decide to self-host n8n, don’t forget about `EXECUTIONS_PROCESS` environment variable, it should be set to main. This tweak ensures a fast response from your bot, because all requests will run in the main process. Otherwise, in its own mode n8n will have a few seconds delay for each Telegram request.

Step 2. Process incoming messages to your Telegram bot

Let’s break down the bot’s workflow into three smaller pieces.

This part receives incoming message from Telegram and performs some preparatory steps
This part receives incoming message from Telegram and performs some preparatory steps

The first part receives incoming messages from Telegram and performs some preparatory steps:

  • Telegram Trigger receives all possible messages by default. That’s how you can ensure that your bot doesn’t look idle even if certain actions are not supported yet.
  • PreProcessing step makes sure that json.message.text key always exists. This will help to avoid error messages in subsequent steps.
This step ensures that a JSON key always exists
This step ensures that a JSON key always exists
  • Now let's take a closer look at the Settings.
Settings store important GPT3/4 configuration parameters
Settings store important GPT3/4 configuration parameters

system_command variable is the initial GPT prompt that instructs how your new chatbot should behave. We ask the bot to be friendly, reply in the user’s language and add emojis to ensure more natural replies. We also provide some additional context by adding the user's name and the Telegram app language. This information will help a chatbot to greet users by their names and take into account their preferred language.

bot_typing variable is needed to provide typing notification while the bot is working on the reply. Notification depends on whether a user started their message with /image command or not.

An example of bot typing notification
An example of a bot typing notification

model_temperature stores the temperature value: the higher, the more “creative” chat completions the model will provide. Read more about GPT parameters on the API reference page.

token_length value limits the length of the answer from the GPT model.

  • Send typing action simply uses the value from the bot_typing JSON parameter.
  • Finally, why do we have a Merge node here? This node is configured in the ChoseBranch mode, which passes through the original JSON from Settings. This is a simple technique to ensure that Send typing action finishes before the next steps begin.

Step 3. Make API calls to OpenAI services

The next part of the bot routes the user data into one of three AI models.

This part of the bot decides which model should run depending on the user input
This part of the bot decides which model should run depending on the user input
  • CheckCommand is a Switch node configured in the following way:
Switch node routes the requests to the corresponding AI models
Switch node routes the requests to the corresponding AI models

The first three Routing Rules are valid requests, they are processed by either GPT model or DALL-E 2. The last rule is a fallback rule for unsupported commands.

  • Simple text and a /start command (outputs 0 and 1) are passed to the OpenAI node for GPT chat completion.
OpenAI node configured in chat completion mode via GPT model
OpenAI node configured in chat completion mode via GPT model
  • Output 2 goes to a different OpenAI model. DALL-E 2 model is a text-to-image neural network that can generate images from text inputs.
Example of OpenAI node configured for image generation
Example of OpenAI node configured for image generation

Step 4. Send the response back to a user

After OpenAI models return some response (text message or an image), we need to pass this information back to the user. Also, we need to prepare a generic response for unsupported commands.

This part demonstrates how response nodes are connected with the previous parts
This part demonstrates how response nodes are connected with the previous parts
  • You can see that Text Reply is connected to both OpenAI nodes. This is a small trick in n8n to reduce workflow redundancy.
  • Send Image passes through an URL that is returned from Create an Image.
  • Finally, Send error message returns a generic reply. This way we don’t call any OpenAI models and provide an immediate response.

That’s it! We’ve created a functional AI-powered chatbot in Telegram.

How much does it cost to create a Telegram bot?

Making a Telegram bot involves three distinct stages:

  1. Registering a new bot on the Telegram platform
  2. Creating a bot logic
  3. Hosting the bot

Telegram allows registering a new bot for free within just a couple of minutes.

After registering a new bot you can create its functionality. If you’re building the bot yourself, the prices could be negligible and the main investment is your time. If you hire a developer (classical, no-code or low-code) then the price depends on many factors and can be rather steep for a project with complex functionality.

Finally, the bot should be available 24/7, which means you need to host it somewhere. If you decide to create the bot using tools such as Python or self-hosted Community version of n8n, then the hosting costs will be modest for a simple bot: around 5$/mo for a low-tier VPS that can handle several bots simultaneously. Telegram bot builders will charge you more depending on the tariffs. In case your bot becomes extremely popular, you may need to buy extra computing power or pay significantly more to the commercial bot-building platform.

Wrap up

Creating bots in Telegram is not as daunting as it may seem.

In this tutorial, we've shown you how to create a Telegram AI bot using n8n as an impressive low-code bot builder. The bot is highly customizable, it’s able to respond to your messages depending on the role, and it’s also capable of transferring AI-generated images effortlessly.

The versatility and functionality of such bots are boundless, and with n8n, the process of creation becomes a breeze.

Create your own Telegram workflows

Build complex automations 10x faster, without fighting APIs

What’s next?

Remember, creating a Telegram AI bot is not just about building; it's about creating an engaging, interactive bot experience. So, roll up those sleeves and start crafting your very own Telegram bot!

Join the community forum and tell us what are the best Telegram bots you know!