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

基于Spring Boot与telegrambots的Telegram Bot无响应问题求助

Telegram Bot无响应排查与解决

问题概述

使用Java 17、Spring Boot 3.1.0和telegrambots-spring-boot-starter 5.6.0开发Telegram Bot,已通过BotFather完成注册,但Bot完全无响应,断点未触发,程序未接收到指令,调整依赖版本后问题仍存在。

BotController类

package com.menkov.klim.weatherforecasttelegrambot.controller;

import org.springframework.beans.factory.annotation.Value;
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;

@Component
public class BotController extends TelegramLongPollingBot {

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

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

    @Override
    public void onUpdateReceived(Update update) {
        try {
            String chatId = update.getMessage().getChatId().toString();

            SendMessage message = new SendMessage();
            message.setChatId(chatId);
            message.setText("Hi!");

            execute(message);
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }
}

POM文件

<?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>3.1.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.menkov.klim</groupId>
    <artifactId>WeatherForecastTelegramBot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>WeatherForecastTelegramBot</name>
    <description>WeatherForecastTelegramBot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.telegram</groupId>
            <artifactId>telegrambots-spring-boot-starter</artifactId>
            <version>5.6.0</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>

        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-mysql</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

主类

package com.menkov.klim.weatherforecasttelegrambot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = {
        "com.menkov.klim.weatherforecasttelegrambot.controller"
})
public class WeatherForecastTelegramBotApplication {

    public static void main(String[] args) {
        SpringApplication.run(WeatherForecastTelegramBotApplication.class, args);
    }
}

排查与解决步骤

1. 修复依赖兼容性问题

Spring Boot 3.x 基于Jakarta EE API,而telegrambots-spring-boot-starter 5.6.0依赖旧版Java EE(javax.*),存在兼容性冲突。升级到支持Spring Boot 3的版本:

<dependency>
    <groupId>org.telegram</groupId>
    <artifactId>telegrambots-spring-boot-starter</artifactId>
    <version>6.9.7.1</version>
</dependency>

2. 确保Bot被正确注册

新版本starter需要通过@TelegramBot注解标记Bot类,让Spring自动注册到Telegram API服务:

@Component
@TelegramBot // 新增注解
public class BotController extends TelegramLongPollingBot {
    // 原有代码不变
}

3. 避免空指针异常导致Bot崩溃

onUpdateReceived方法直接调用update.getMessage(),若update不是消息类型(如回调查询)会抛出空指针,导致Bot停止响应。添加空判断:

@Override
public void onUpdateReceived(Update update) {
    // 仅处理文本消息类型
    if (update.hasMessage() && update.getMessage().hasText()) {
        try {
            String chatId = update.getMessage().getChatId().toString();
            SendMessage message = SendMessage.builder()
                    .chatId(chatId)
                    .text("Hi!")
                    .build();
            execute(message);
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }
}

4. 验证配置文件正确性

确保application.properties/application.yml中配置的Bot用户名和token与BotFather提供的一致:

telegram.bot.username=你的Bot用户名
telegram.bot.token=你的BotToken

5. 简化主类组件扫描配置

@SpringBootApplication默认扫描主类所在包及其子包,你的BotController属于主类的子包,因此可以省略@ComponentScan:

@SpringBootApplication
public class WeatherForecastTelegramBotApplication {
    public static void main(String[] args) {
        SpringApplication.run(WeatherForecastTelegramBotApplication.class, args);
    }
}

6. 检查启动日志

启动应用后,查看日志中是否存在Registered Bot: [你的Bot用户名]的提示,确认Bot已成功注册。若没有该日志,说明Spring未检测到Bot实例,需检查组件扫描和注解配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 17:53:17