You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 3.3.4集成org.telegramtelegrambots-spring-boot-starter6.9.7.1遇阻求助

在Spring Boot 3中手动初始化任意版本Telegram Bot的解决方案

1. 替换依赖,解耦starter与核心Bot库

移除org.telegram:telegrambots-spring-boot-starter依赖,直接引入你需要的任意版本的telegrambots核心库,完全摆脱starter对Bot版本的绑定限制。

以Maven配置为例:

<dependencies>
    <!-- 移除原starter依赖 -->
    <!-- <dependency>
        <groupId>org.telegram</groupId>
        <artifactId>telegrambots-spring-boot-starter</artifactId>
        <version>6.9.7.1</version>
    </dependency> -->

    <!-- 引入指定版本的核心Telegram Bot库 -->
    <dependency>
        <groupId>org.telegram</groupId>
        <artifactId>telegrambots</artifactId>
        <version>你的目标版本,比如7.0.0</version>
    </dependency>

    <!-- Webhook模式需添加Spring Boot Web依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

2. 手动实现Bot类与配置

2.1 定义自定义Bot类

根据需求选择继承TelegramLongPollingBot(长轮询模式)或TelegramWebhookBot(Webhook模式),实现核心方法:

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;

public class CustomTelegramBot extends TelegramLongPollingBot {
    private final String botToken;
    private final String botUsername;

    public CustomTelegramBot(String botToken, String botUsername) {
        this.botToken = botToken;
        this.botUsername = botUsername;
    }

    @Override
    public String getBotToken() {
        return botToken;
    }

    @Override
    public String getBotUsername() {
        return botUsername;
    }

    @Override
    public void onUpdateReceived(Update update) {
        // 自定义消息处理逻辑
        if (update.hasMessage() && update.getMessage().hasText()) {
            String chatId = update.getMessage().getChatId().toString();
            String messageText = update.getMessage().getText();

            SendMessage reply = new SendMessage();
            reply.setChatId(chatId);
            reply.setText("已收到消息:" + messageText);

            try {
                execute(reply);
            } catch (TelegramApiException e) {
                e.printStackTrace();
            }
        }
    }
}

2.2 创建Spring配置类,注册Bot与会话

通过@Configuration类手动初始化Bot,并完成Telegram Bot API的注册:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;

@Configuration
public class TelegramBotConfig {

    @Value("${telegram.bot.token}")
    private String botToken;

    @Value("${telegram.bot.username}")
    private String botUsername;

    @Bean
    public CustomTelegramBot customTelegramBot() {
        return new CustomTelegramBot(botToken, botUsername);
    }

    @Bean
    public TelegramBotsApi telegramBotsApi(CustomTelegramBot customTelegramBot) throws TelegramApiException {
        TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class);
        botsApi.registerBot(customTelegramBot);
        return botsApi;
    }
}

2.3 配置Bot参数

在application.properties中添加Bot的身份参数:

telegram.bot.token=你的Bot专属Token
telegram.bot.username=你的Bot用户名

3. 处理Spring Boot 3兼容性问题

Spring Boot 3基于Jakarta EE API,替代了传统Java EE:

  • 版本6.0.0+的telegrambots已适配Jakarta,无需额外配置;
  • 若使用低于6.0.0的旧版本,需手动排除Java EE依赖并引入Jakarta兼容包,但不推荐,建议优先升级到支持Jakarta的Bot版本。

内容的提问来源于stack exchange,提问作者Bexruz izzatullayev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 09:20:09