Guide To Building a Powerful Telegram Chat Bot With N8n
Let’s integrate the APIs of Telegram and OpenAI to build a chatbot that serves as your personal assistant in your pocket
Introduction
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
In the figure above, you can see the statistics provided by Salesforce, 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 chatbots in use on Facebook Messenger.
1.4 billion people actively use messaging apps, indicating a vast audience for chatbot engagement.
Types of Chatbots
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 an account on n8n for a 14-day trial. Sign in as shown below:
Now let’s set up the workflow to develop a telegram chatbot.
Create a Workflow
In the dashboard, will click on ‘Workflow’ 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.
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. Follow these steps:
Open the Telegram website or desktop app and sign in to your account.
Navigate to the Telegram BotFather, as shown in the figure below:
Connect Telegram with N8n
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.
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.
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:
Sign up for an account on the OpenAI website.
Once logged in, generate an API key from the dashboard.
- Utilize this API key to authenticate your chatbot’s access to the OpenAI model within N8n credentials, as demonstrated below.
Integrate Telegram & OpenAI to create ChatBot
Step 1
Add a Telegram trigger to the workflow and follow the steps below to configure its settings:
Credential to connect with: Utilize the previously created Telegram credential, as demonstrated above.
Trigger On: Choose “message” to activate the trigger whenever a message is sent to the chatbot on Telegram.
Step 2
Next, incorporate an “IF” node into the workflow and proceed with the following steps to adjust its settings:
- 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
’.
Step 3
Integrate a second “IF” node into the workflow and continue with the following steps to customize its settings:
- 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
’.
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:
Credential to connect with: Utilize the previously created Telegram credential, as demonstrated above.
Resource: Set the resource as ‘message’.
Operation: This should be set as “Send Chat Action”.
Chat ID: Set the expression as
{{ $(‘Telegram Trigger’).item.json.message.chat.id }}.
Action: Specify the action as ‘Typing’.
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:
Credential to connect with: Utilize the previously created OpenAI credential, as demonstrated above.
Resource: Set the resource as ‘Text’.
Operation: Choose “Message a Model” from the options.
Model: Choose “Message a Model” from the options.
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:
Credential to connect with: Utilize the previously created OpenAI credential, as demonstrated above.
Resource: Set the resource as ‘message’.
Operation: This should be set as “Send Message”.
Chat ID: Set id to ‘
{{ $node[‘Check Id of Chat’].json.message.chat.id }}
’Text: Set text to ‘
{{ $node[‘Generate Response’].json.message.content }}
’
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.
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:
Originally published on medium: https://medium.com/@shaikhrayyan123/guide-to-building-a-powerful-telegram-chat-bot-with-n8n-0727d4c1dccf