Sitemap
Isakovinc

Isakovinc

Deploy Java Telegram Bot on Heroku Server

3 min readJun 22, 2022

--

Today we are going to step through all the important elements in deploying the bot on heroku servers.

First and foremost, we need a telegram bot. Let’s use the simple echoBot example.

Creating a new project.

Setting maven library and adding telegrambots library.

Bot Library

<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>6.0.1</version>
</dependency>

Main class

public class Main {
public static void main(String[] args) {
try {
TelegramBotsApi telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class);
telegramBotsApi.registerBot(new Bot());
} catch (TelegramApiException e) {
throw new RuntimeException(e);
}

}
}

Bot class

To get a botToken you need to go to botFather and take a unique token for your new bot.public class Bot extends TelegramLongPollingBot {
@Override
public String getBotUsername() { return "@prayt_bot";}
@Override
public String getBotToken() {return "5355244356:AAGrTnB72LFlyxqA-4ncij3ZiKj3BA2a5a0";}

@Override
public void onUpdateReceived(Update update) {
User user = update.getMessage().getFrom();
if (update.hasMessage()){
Message message = update.getMessage();
if(message.hasText()){
SendMessage sendMessage = new SendMessage();
sendMessage.setText(update.getMessage().getText());
try {
execute(sendMessage);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
}
}
}

Now that we have a bot which is running locally, let’s go to heroku.com

Press enter or click to view image in full size

Register at heroku

This will be the window of your heroku settings. For now leave it like this.

Press enter or click to view image in full size

Open your pom.xml file from your java bot project, after dependencies tag. Enter the below code.

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.1.1</version>
<configuration>
<assembleDirectory>target</assembleDirectory>
<programs>
<program>
<mainClass>com.home.server.TelegramBot</mainClass>
<name>workerBot</name>
</program>
</programs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Press enter or click to view image in full size

Then, specify the exact path of your Main class.

Press enter or click to view image in full size
Press enter or click to view image in full size

Note that what you wrote in groupId is also the beginning of your project path.

If everything went well.

Get Isakovinc’s stories in your inbox

Join Medium for free to get updates from this writer.

In the main project path create a file called:

  • “Procfile” inside it.
  • Write down → [ worker: sh target/bin/workerBot ]
  • Open your terminal (Windows powershell, gitBash)
  • Go to your project path
  • Run these commands:

1. git init

2. heroku login [login / password]

Press enter or click to view image in full size
Press enter or click to view image in full size

If you previously entered to your heroku account you dont need to enter anything. Simply press log in and close browser. Then in the terminal it will automatically enter to your heroku account.

3. git add .

4. git commit -m ‘Initial commit’

5. heroku create → get the name of the app | Here you will get the name of the app will look like this

Press enter or click to view image in full size

6. heroku git:remote -a |name of your app| → pure-river-05130

7. git push heroku master

8. heroku ps:scale worker=1 → To run the bot.

That’s it your bot should work now.))

P/S: If you are having problems with

[ Maven error — invalid target release: 1.11 ]

Go to pom.xml in the property section

update the code with →

<properties> <java.version>11</java.version> </properties>

Then git add, commit, push and dont forget to run it with

heroku ps:scale worker=1

Good Luck!!

For more visit: java_stuff

--

--