Telegram Bot

Creating a Telegram Bot with Node-Red

Hello everyone. We all engage in various activities to make use of our free time. I've been spending my time with fun applications I've come across since I started working with Node-Red. We've probably all chatted with Bot virtual persons that we encounter in almost all applications. Have you ever wondered how they are controlled? I wondered and decided to make my own Bot. For this, I used Node-Red's chatbot modules. When we first run Node-Red, there are standard modules in the Module Palette. I want to add the Telegram bot I want to use to the palette with a specific search. For this;


I downloaded the telegram bot module from the Palette tab:

node-red-contrib-telegrambot

After the download is complete, we will see that the Telegram module has been added to the Palette menu.


After downloading the Telegram module, we can start creating our own flow with the standard modules we have. First, we start with the Inject module. In the flow diagram, Inject is used first to transmit commands to the program. Then we will use the Function module where we write the process and finally the Telegram Sender module that sends the message we transmit to the Telegram address.


We don't need to make any changes to the content of the Inject module. In Function, there will be some information we need to obtain. Let's get the Chat Id as I show below and place our code in the relevant place.


msg.payload = {}
msg.payload.chatId = xxxxxxxxx
msg.payload.type = 'message'
msg.payload.content = 'Hello, my name is Seher!'
return msg;

Here, to find the Chat Id, I got the User Id from UserInfoBot, which is Telegram's own Bot:


After filling the inside of the Function, it's time for the Telegram Sender module. We will enter the module and fill in the necessary places. Let's enter a bot name and say edit.


When we say edit, the Bot-Name and Token fields that we have to fill will appear.


To be able to fill these fields, I need a Token from BotFather, which is Telegram's own Bot. To get a Token, I need to create a Bot. I'm creating a test_bot for my application and getting the Token belonging to the Bot I created. The necessary commands for this are as follows:


After filling all the necessary fields, we can run our application. First, I find the test_bot I created above and write the /start command. Then I start the application with the Inject module. In every button event, the message I wrote in the function will be transmitted to the test_bot.


I got a result as you can see below. This was, of course, the first step in creating a complex bot. We can develop the next steps with broader functions. I started this way, to be continued later 😊


← Back to HOBBYLOG