Guide To Building a Powerful Telegram Chat Bot With N8n
Tags: automation, opensource, chatbots, chatgpt
Published: 2024-04-16 10:25:53

## Introduction

![Guide To Building a Powerful Telegram Chat Bot With N8n
](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mw6mcf1cdrd26iujsvb5.jpg)

Welcome to my guide on creating powerful Telegram chatbots with n8n! In this tutorial, I’ll walk you through building a chatbot from scratch using the user-friendly n8n platform. Whether you’re a beginner or intermediate developer or just starting out, this guide is designed to make chatbot creation easy and accessible.

With messaging apps like Telegram gaining popularity. They streamline communication, automate tasks, and provide instant responses, making interactions smoother and more efficient.

Let’s integrate the APIs of Telegram and OpenAI to build a chatbot that serves as your personal assistant in your pocket. Whenever you have a question, the bot is ready to provide answers and help you find solutions to your problems. Additionally, I’ll explore how to create an awesome chatbot using no code.

Let’s dive right into it!


## ChatBot Usages & Insights

![ChatBot Usages & Insights](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7axb349cc6bkt3t95woc.jpg)

In the figure above, you can see the statistics provided by [Salesforce](https://www.salesforce.com/blog/chatbot-statistics/), which show the increased usage of chatbots in today’s world Chatbots are incredibly useful and make tasks or communication processes easier for anyone needing automation or problem-solving solutions.

## Chatbot Adoption Stats

Chatbots are getting really popular because they make things easier for people and improve how users interact with different platforms. Let’s check out some stats to see how much they’re being used.

- There are over [300,000](https://venturebeat.com/ai/facebook-messenger-passes-300000-bots/) chatbots in use on Facebook Messenger.
- [1.4 billion ](https://backlinko.com/chatbot-stats)people actively use messaging apps, indicating a vast audience for chatbot engagement.

## Types of Chatbots

![Types of Chatbots](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89cbzeye4k9fn9ifk0dq.jpg)
A chatbot is a conversational tool that seeks to understand customer queries and respond automatically, simulating written or spoken human conversations. As you’ll discover below, some chatbots are rudimentary, presenting simple menu options for users to click on. However, more advanced chatbots can leverage artificial intelligence (AI) and natural language processing (NLP) to understand a user’s input and navigate complex human conversations with ease.

**Rule-Based Chatbots:** These chatbots operate based on predefined rules and keywords. They follow a set of instructions to respond to user queries and are ideal for simple tasks like answering FAQs.

**AI-Powered Chatbots:** AI-driven chatbots leverage machine learning algorithms to understand and respond to user queries more intelligently. They can handle complex conversations, learn from interactions, and provide personalized responses.

**Voice chatbots:** Voice chatbots, utilizing advanced speech recognition technology, actively engage users in natural conversations, offering instantaneous assistance and tailored interactions to meet their needs.

**Generative AI chatbots:** Generative AI chatbots dynamically generate responses, leveraging cutting-edge artificial intelligence to provide personalized interactions and innovative solutions.

## Top software to build a chatbot

When it comes to building chatbots, there are several top software options available, catering to different needs and preferences. Here are some of the most popular choices:

**N8n:** No-code platform for building powerful chatbots.
Chatfuel: User-friendly tool for creating chatbots on Facebook Messenger.

**ManyChat:** Platform for building interactive chatbots for Facebook Messenger.

**Dialogflow:** Google’s AI platform for creating intelligent chatbots.

**Botsify:** Chatbot platform offering both no-code and custom coding options.

**Voiceflow:** Platform for designing voice-based chatbots and applications.

**Chatbase:** Analytics platform for optimizing chatbot performance.

**DanteAI:** AI-powered chatbot platform for various use cases.

**CodyAI:** No-code chatbot platform with pre-built templates.

**Botpress:** Open-source platform for building customizable chatbots.

**StackAI:** Chatbot platform specializing in sales and marketing automation.

Explore these software options to find the right tool for building your chatbot, whether you prefer a no-code solution, an AI-powered platform, or an open-source solution.

## Make a chatbot with n8n

Creating a chatbot with N8n is easy and requires no coding experience. Follow these simple steps to get started:

### Sign Up for n8n

If you aren’t already signed up, [create](https://app.n8n.cloud/register) an account on n8n for a 14-day trial. Sign in as shown below:

![Sign Up for n8n](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rfm62bciub8wgdrrvtzg.png)

Now let’s set up the workflow to develop a telegram chatbot.

### Create a Workflow

In the dashboard, will click on ‘[Workflow](https://rayyan.app.n8n.cloud/workflows)’ to create a first no-code chatbot. Then, selecting the workflow that I’ve created to make edits for the chatbot, as shown in the figure below.


![Create a Workflow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ybiouej5rurl5y269zg1.png)

After creating a workflow, the next step is to set up the credentials required for integrating with Telegram and OpenAI.

### Create Credentials

**Connect Telegram with N8n**

To integrate your chatbot with Telegram, you’ll need to create [credentials](https://rayyan.app.n8n.cloud/credentials). Follow these steps:

1. Open the Telegram [website](https://web.telegram.org/) or [desktop](https://desktop.telegram.org/) app and sign in to your account.

2. Navigate to the Telegram BotFather, as shown in the figure below:

![Connect Telegram with N8n
](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i8tblcbsyx75kue42iwz.png)

The first step is to initiate a conversation with BotFather. To do this, type “/newbot” in the chat with BotFather. Following this, you’ll be prompted to provide a name for your chatbot. It’s important to note that the word “bot” must be included in the name you choose, as shown in the figure. This ensures that your chatbot is properly recognized and categorized within the Telegram ecosystem.

After providing the name, BotFather will generate an HTTP API token, which you’ll need to use in the N8n credentials for Telegram. This token acts as a unique identifier for your chatbot and allows it to communicate with Telegram’s API seamlessly.


![HTTP Access API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hbb0qgkxaz9woir1tiex.png)

To establish a connection between your Telegram account and N8n, simply incorporate the generated HTTP API token into the N8n Telegram credential. This integration enables seamless communication between Telegram and N8n, facilitating the operation of your chatbot with ease.


![Establish a connection between your Telegram account and N8n](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5b2ix904tnf0zwh1amoj.png)

Telegram account successfully connected, it’s time to integrate OpenAI for enhanced chatbot functionality. Let’s seamlessly link OpenAI to further enhance the capabilities of your chatbot.

**Link OpenAI with N8n**

To connect your chatbot to the OpenAI model, you’ll need API credentials. Here’s how to get started:

1. Sign up for an account on the [OpenAI](https://platform.openai.com/) website.
2. Once logged in, generate an [API key](https://platform.openai.com/api-keys) from the dashboard.


![API Key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n3x0go7c6aqhrjku4u6j.png)

Utilize this API key to authenticate your chatbot’s access to the OpenAI model within N8n credentials, as demonstrated below.


![Open AI connection with workflow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/82n5oj0faqaysb22tjsa.png)

## Integrate Telegram & OpenAI to create ChatBot

### Step 1

Add a Telegram trigger to the workflow and follow the steps below to configure its settings:

1. **Credential to connect with:** Utilize the previously created Telegram credential, as demonstrated above.
2. **Trigger On:** Choose “_message_” to activate the trigger whenever a message is sent to the chatbot on Telegram.


![Telegram Trigger](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tnrxhibu4wbb5x5by3in.png)

### Step 2
Next, incorporate an “_IF_” node into the workflow and proceed with the following steps to adjust its settings:

1. **Conditions:** Establish the condition as ‘`{{ $json.message.text }}`’ “is equal to” to retrieve the text of the message triggered from Telegram. Set the value2 as ‘/start’.


![Check Started or Not](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l1e2hetdlt01xls33btw.png)

### Step 3
Integrate a second “_IF_” node into the workflow and continue with the following steps to customize its settings:

1. **Condition:** Define the condition as ‘`{{ $json.message.chat.id }}`’ “is equal to” to retrieve the identifier of the chat from the Telegram message. Set the value2 as ‘your_chat.id’, for example, ‘121883958’.


![Check Id of Chat](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iktytkj6uf56e5wukpg3.png)

### Step 4

Now, when the “_IF_” node condition evaluates to true, it should be linked with the “Telegram SendChatAction” node. Let’s configure the “Telegram SendChatAction” node with the following steps:

1. **Credential to connect with:** Utilize the previously created Telegram credential, as demonstrated above.
2. **Resource:** Set the resource as ‘message’.
3. **Operation:** This should be set as “Send Chat Action”.
4. **Chat ID:** Set the expression as `{{ $(‘Telegram Trigger’).item.json.message.chat.id }}`.
5. **Action:** Specify the action as ‘Typing’.


![Telegram Typing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m84d4vjmr9am71ngvrlv.png)

### Step 5
Let’s create a node for OpenAI, it should be connected with the OpenAI node to generate a response for the query. Let’s configure the “OpenAI” node with the following steps:

1. **Credential to connect with:** Utilize the previously created OpenAI credential, as demonstrated above.
2. **Resource:** Set the resource as ‘Text’.
3. **Operation:** Choose “Message a Model” from the options.
4. **Model:** Choose “Message a Model” from the options.


![Generate Response](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kg7d6tjdg49gvid645ig.png)

### Step 6
Now, the Telegram Action is used to send the response to the chatbot user. Let’s configure the “Telegram Action” with the following steps:

1. **Credential to connect with:** Utilize the previously created OpenAI credential, as demonstrated above.
2. **Resource:** Set the resource as ‘message’.
3. **Operation:** This should be set as “Send Message”.
4. **Chat ID:** Set id to ‘`{{ $node[‘Check Id of Chat’].json.message.chat.id }}`’
5. **Text:** Set text to ‘`{{ $node[‘Generate Response’].json.message.content }}`’


![Send Message](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6wv40h2k8dk3xik2x4kp.png)

Finally, develop a workflow for a chatbot that seamlessly integrates with the OpenAI model, enabling it to provide real-time responses to user queries. Below, you’ll find a visual representation of the template.


![Guide To Building a Powerful Telegram Chat Bot With N8n
](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6oc2635oj9mtrokdragk.jpg)

## Wrap Up
Integrating Telegram and OpenAI within N8n allows for the creation of a sophisticated chatbot capable of providing intelligent responses to user queries in real time. By following the step-by-step instructions outlined in this guide, you can seamlessly connect to Telegram, leverage the power of OpenAI’s natural language processing, and enhance the functionality of your chatbot. Whether you’re building a chatbot for customer support, engagement, or any other purpose, N8n provides a versatile and user-friendly platform to bring your chatbot ideas to life. Embrace the potential of chatbot technology and embark on your journey to create powerful and efficient chatbots with N8n.

## What’s next?
Now that you’ve learned how to build a powerful Telegram chatbot with N8n, it’s time to explore further possibilities.

You can expand your chatbot’s capabilities by integrating additional services and APIs, such as databases for storing user information or external APIs for accessing real-time data.

Experiment with different workflows and automation features in N8n to optimize your chatbot’s performance and enhance user experiences.

Remember, the journey of building a chatbot is ongoing, and there’s always room for innovation and improvement. Keep exploring, experimenting, and evolving your chatbot to create a truly powerful and engaging experience for your users.

Here are some further readings:

- [How to connect Google Sheets and MySQL database.](https://blog.n8n.io/google-sheets-to-mysql/)
- [An overview of pre-built nodes for Google Apps.](https://blog.n8n.io/automate-google-apps-for-productivity/)

Originally published on medium: [Guide To Building a Powerful Telegram Chat Bot With N8n
](https://medium.com/@shaikhrayyan123/guide-to-building-a-powerful-telegram-chat-bot-with-n8n-0727d4c1dccf)