How To: Create a Java Telegram Bot – Telegram Group

How To: Create a Java Telegram Bot

Hey there! I will walk you through how to create a telegram bot made purely out of Java. There are many guides available that uses Python or Node.js but I couldn’t seem to find a good guide for Java. So without further ado, let’s begin.

Arguably the most important step, download telegram onto your PC. Using telegram on PC makes the development and testing phase so much easier as you can see the changes made instantly.

Download Telegram for Desktop

While on Telegram application, click onto the Search bar or enter CTRL F. Type “BotFather” and click on the first result.

Here, you will type “/start” to create a new bot. This is the first step of creating your own telegram bot.

After completing the instructions, you should have obtained the HTTP API Token for your new bot. This API Token is very important and you will hide it from anyone. If anyone gains access to this API Token, he/she gains access to your bot.

Now, let’s download IntelliJ IDE. This is the preferred IDE for Java Developers as it can automatically set up the initial configurations for any Java project. Click here to download.

Upon successful download, let’s open IntelliJ and create a new project. Here, do make sure you’re selecting Maven as it is important in injecting dependencies into your Java project.

Select Maven when Creating a new Java Project

Now, create a meaningful name for your project and store it in a location you can remember. Also, GroupID and ArtifactID are not crucial for now.

Nice, you have successfully created a java project. Under src/main/java/ folder, create a Main class and a separate class for your bot e.g. BoLuiBot.java. This can be done by right-clicking the folder you would like your java file to be in and select New > Java File.

After creating those two important classes, we need to inject the Java Telegram API library into our project. The library can be found with this link: here. This library is created by Ruben Lagus. So, let’s go to the pom.xml file and add the new dependency. Save the file.

Dependency Injection into Your Java Project

At this point, you can start the development of your Java bot. Let’s go back to your bot class file. We need the bot to extend TelegramLongPollingBot for its auto updates and messaging features.

Methods to Override Upon Extending to TelegramLongPollingBot

Replace the null in getBotUsername() and getBotToken() with your bot’s username and API Token. Make sure to replace them in String notation.

Now, let’s jump into the Main.java file and make an edit. Add these lines of code into your Main.java file in its main method.

Be sure to replace BoLuiBot with your bot file name. Next, Save and Run Main.java

5. Testing

Upon running Main.java, your bot should be live and listening to messages. Let’s add a simple echo feature to this bot. The bot will respond with the same message you send it. Copy this code below into the onUpdateReceived() method.

This code is to be added into the onUpdateReceived() method.

Woohoo! Now, start your bot and send a text message to it. It will echo your message.

Thanks for reading my guide on how to create a Java Telegram Bot. I hope this helps you in your journey to build a java telegram bot.

An important thing to note is that your telegram can only run when you are running the Main.java file. In order to have your bot active 24/7, it needs to be deployed onto a server. The server will then run your program continuously so you do not have to keep your computer running for the bot.

Ten articles before and after

How To: Deploy Java Telegram Bot to Heroku – Telegram Group

Serverless Telegram bot with Kotlin, Firebase and Google Cloud Functions – Telegram Group

TRX1 Dev Blog #11 (November 2021) – Telegram Group

Telegram Bot 跟我想的不一樣. 第一天進公司時,Slack就被邀進同事們訂便當的群組,在上面會宣布今天訂那一家便… – Telegram Group

How to Create a Telegram Chatbot in 2022 – Telegram Group

How I Use A Telegram Bot To Track My Expenses – Telegram Group

CryptoChat: Un chatbot hecho con rasa sobre criptomonedas – Telegram Group

data-rh=”true”>Share your Ref link to your friends:
t.me/metamusk_airdrop_bot?start=21FF1F54D851B44DBAB310ABA260150F
? Metamusk is a native token issued to support Elon Musk and his great projects on the way to… – Loiscwai – Medium – Telegram Group

The Indra Capital Ecosystem — Part II— IndraX, YieldWallet.io and Airavat – Telegram Group

What is telegram messenger?. Telegram is a messenger application… – Telegram Group