【问题标题】:Trying to implement telegrambots-spring-boot-starter and create Telegram bot, but it doesn't work尝试实施 telegrambots-spring-boot-starter 并创建 Telegram 机器人,但它不起作用
【发布时间】:2023-02-02 23:17:37
【问题描述】:

我使用 Spring boot 和 telegrambots-spring-boot-starter 依赖项。我做了这个存储库中显示的所有事情: https://github.com/rubenlagus/TelegramBots/tree/master/telegrambots-spring-boot-starter。 但它没有用。

我的 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>

我的机器人类:

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) {

    }
}

我的主要 Spring 引导类:

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);
    }

}

注意:根据存储库,Spring Boot 的电报机器人启动器会自动注册您的机器人,无需注册码。和 ApiContextInitializer.init();方法也不再存在。

【问题讨论】:

  • 如果您查看该机器人的代码,它使用的是 Spring Boot 2.5 而不是 Spring Boot 2.6。现在它并不意味着它不适用于 2.6,但它绝对没有经过测试。所以我建议降级到 2.5.x(无论最新版本是什么)。

标签: java spring-boot telegram-bot


【解决方案1】:

尝试手动注册您的机器人。

  1. 为 TelegramBotsAPI 创建 bean:

     @Bean  
     public TelegramBotsApi telegramBotsApi() {  
         return new TelegramBotsApi(DefaultBotSession.class);  
     }
    
  2. 在构造函数中注册您的机器人:

     @Component
     public class MySecretaryBot extends TelegramLongPollingBot {
    
         @Autowired
         public MySecretaryBot(TelegramBotsApi telegramBotsApi) {
             telegramBotsApi.registerBot(this);
         }
    
         ...
    
     }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 2020-05-21
    • 2014-07-05
    • 2018-12-24
    • 1970-01-01
    • 2019-10-28
    相关资源
    最近更新 更多