SpringBoot整合Telegram机器人

本文最后更新于 2025年11月19日 晚上

环境

java版本:1.8.0_144

SpringBoot版本:2.3.12.RELEASE

申请TelegramBot

在telegram中搜索BotFather,并按图中步骤申请,其中第三步的机器人用户名和第五步token我们后面需要用到

创建完成后,就可以搜索第四步的机器人名称搜索到创建的机器人了

SpringBoot整合

1.pom.xml引入telegrambots

1
2
3
4
5
6
<!--telegrambots-->  
<dependency>  
 <groupId>org.telegram</groupId>  
<artifactId>telegrambots</artifactId>  
<version>6.5.0</version>  
</dependency>

2.项目配置文件application.yml增加配置

1
2
3
4
5
6
7
telegrambot:  
username: 机器人用户名  
 token: 创建机器人获得的token  
 chatId: 对话id(可先不填)
 # 本地调试可以填入本地代理软件的ip和端口(v2ray默认socks端口是10808)  
#  proxyHost: 127.0.0.1  
# proxyPort: 10808

3.创建对应的TelegramBotConfig类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**  
* @author LiuXiaoBai 2023/6/5  
*/
@Configuration  
@ConfigurationProperties("telegrambot")  
@Data  
@Slf4j  
public class TelegramBotConfig {  
private String username;  
private String token;    
private String chatId;
private String proxyHost;  
private String proxyPort;  
 
/**  
* 配置代理信息,本地调试时可将配置文件的proxyHost和proxyPort的注释去掉
*/  
@Bean  
public DefaultBotOptions defaultBotOptions() {  
DefaultBotOptions botOptions = new DefaultBotOptions();  
if (StringUtils.isNotBlank(proxyHost) && StringUtils.isNotBlank(proxyPort)) {  
botOptions.setProxyHost(proxyHost);  
botOptions.setProxyPort(Integer.parseInt(proxyPort));  
botOptions.setProxyType(DefaultBotOptions.ProxyType.SOCKS5);  
}  
return botOptions;  
}
}

4.创建TelegramBot类继承TelegramLongPollingBot

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**  
* @author LiuXiaoBai 2023/6/5  
*/
@Slf4j  
@Component  
public class TelegramBot extends TelegramLongPollingBot {  
 
@Resource  
private TelegramBotConfig telegramBotConfig;  
 
public TelegramBot(DefaultBotOptions options) {  
super(options);  
}  

/**  
* 发送MarkdownMsg消息  
*/  
public void sendMarkdownMsg(String markdownMsg) {  
SendMessage message = new SendMessage();  
message.setParseMode(ParseMode.MARKDOWN);  
message.setChatId(telegramBotConfig.getChatId());  
message.setText(markdownMsg);  
try {  
execute(message);  
} catch (Exception e) {  
log.error(e.getMessage(), e);  
}
}
 
/**  
* 机器人接收到消息后的处理逻辑  
*/  
@Override  
public void onUpdateReceived(Update update) {  
log.info("TelegramBot onUpdateReceived update:{}", JSON.toJSONString(update));  
if (update.hasMessage() && update.getMessage().hasText()) {  
SendMessage message = new SendMessage(); // Create a SendMessage object with mandatory fields  
message.setChatId(update.getMessage().getChatId().toString());  
message.setText(update.getMessage().getText());  
try {  
execute(message); // Call method to send the message  
} catch (Exception e) {  
log.error(e.getMessage(), e);  
}  
}  
}
 
@Override  
public String getBotUsername() {  
return telegramBotConfig.getUsername();  
}  
 
@Override  
public String getBotToken() {  
return telegramBotConfig.getToken();  
}    
}

5.创建TelegramBotInit初始化TelegramBot

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**  
* @author LiuXiaoBai 2023/6/6  
*/
@Component  
@Slf4j  
public class TelegramBotInit implements CommandLineRunner {  
 
@Resource  
private TelegramBot telegramBot;  
 
@Override  
public void run(String... args) {  
try {  
// Create the TelegramBotsApi object to register your bots  
TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class);  
// Register your newly created AbilityBot  
botsApi.registerBot(telegramBot);  
} catch (Exception e) {  
log.error(e.getMessage(), e);  
}  
log.info("TelegramBot register success!");  
}  
}

6.测试onUpdateReceived方法,并获取chatId 我们给机器人发消息,并查看控制台日志,获取chatId

当我们想用机器人给自己做一些定时通知时,就可以在配置文件填入chatId,然后使用TelegramBot类的sendMarkdownMsg方法即可

官方文档:

Bot API Library Examples

Home · rubenlagus/TelegramBots Wiki


SpringBoot整合Telegram机器人
http://blog.baicat.eu.org/2023/06/05/SpringBoot整合Telegram机器人/
作者
liuxiaobai5201314
发布于
2023年6月5日
更新于
2025年11月19日
许可协议