I use Spring boot and telegrambots-spring-boot-starter dependency. I did all things as it showed in this repository:
https://github.com/rubenlagus/TelegramBots/tree/master/telegrambots-spring-boot-starter.
But it didn’t work.
My pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.telegram</groupId>
<artifactId>secretary.bot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>secretary.bot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots-spring-boot-starter</artifactId>
<version>5.7.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.6.3</version>
</plugin>
</plugins>
</build>
</project>
My Bot class:
package com.telegram.secretary.bot;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import java.util.List;
@Component
public class MySecretaryBot extends TelegramLongPollingBot {
@Override
public String getBotUsername() {
return "Oyaqbot";
}
@Override
public String getBotToken() {
return "5240178378:AAHA6GVOT2fGp_pFMyXD75LEBlms6iEVtSs";
}
@Override
public void onRegister() {
}
@Override
public void onUpdateReceived(Update update) {
String command = update.getMessage().getText();
if(command.equals("/hello")){
String message = "Hello, dear friend!";
SendMessage response = new SendMessage();
response.setChatId(update.getMessage().getChatId().toString());
response.setText(message);
try {
execute(response);
}
catch (TelegramApiException e){
e.printStackTrace();
}
}
}
@Override
public void onUpdatesReceived(List<Update> updates) {
}
}
My Main Spring boot class:
package com.telegram.secretary.bot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Note: According to the repository telegram bot starter for Spring boot automatically registers your bot and no need registration code. And ApiContextInitializer.init(); method also doesn’t exist anymore.
2
Answers
Try to register your bot manually.
Create bean for TelegramBotsAPI:
Register your bot in the constructor:
You should set scanning directive.
Try this: