当前位置: 首页 > ai >正文

java集成telegram机器人

java集成telegram机器人

最近项目需要集成telegram机器人,来实现消息推送功能,以此记录下。

1.创建telegram账号

没有账号的可以去某宝上买一个,千万不要用自己的手机号+86去注册,你懂得。

2. 打开@BotFather对话创建机器人获取token

在搜索框里的输入@BotFather后进入对话框会看到如下界面

在这里插入图片描述

然后发送/start开始对话

在这里插入图片描述

发送/newbot创建机器人

在这里插入图片描述

发送机器人的name和 username,其中username有要求,需要以xxx_bot命名

在这里插入图片描述

以下就拿到机器人对应的token,后面我们将通过token给机器人发送消息,token不需要泄露出去了。

在这里插入图片描述

3.获取chatId会话ID

telegram中每个用户、群组、频道都是一个会话,会话ID就是chatId,通过会话ID和token就可以给机器人发送消息了。
通过搜索@getidsbot用户然后发送/start获取会话id

在这里插入图片描述

在这里插入图片描述

当然目前获取的为当前用户的会话ID,如果需要将消息通知到群里面,操作也是一样的,我们只需要将机器人和@getidsbot都拉进群里进行,这样拿到群的会话id。

4.发送请求

telegram官方有对应的api点击查看,将下方token、chatId、message替换即可。

https://api.telegram.org/bot${token}/sendMessage?chat_id=[chatId]&text=${message}

或者直接通过浏览器访问,用于测试

{"ok": true,"result": {"message_id": 9,"from": {"id": 7662234082,"is_bot": true,"first_name": "test01","username": "pitiless223_bot"},"chat": {"id": -4614960807,"title": "z & test01","type": "group","all_members_are_administrators": true,"accepted_gift_types": {"unlimited_gifts": false,"limited_gifts": false,"unique_gifts": false,"premium_subscription": false}},"date": 1746695605,"text": "hello"}
}

5.java代码实现

github上直接有集成好了的插件java-telegram-bot-api

配置maven依赖

<dependency><groupId>com.github.pengrad</groupId><artifactId>java-telegram-bot-api</artifactId><version>8.3.0</version>
</dependency>

以下是我封装好的serve

package com.ruoyi.system.service.impl;import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.UpdatesListener;
import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.request.SendMessage;
import com.pengrad.telegrambot.request.SendPhoto;
import com.pengrad.telegrambot.response.SendResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import javax.annotation.PostConstruct;
import java.util.List;@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TelegramBotServiceImpl implements UpdatesListener {/*** token*/@Value("${custom.telegram_bot.token}")private String telegramBotToken;/*** bot*/private TelegramBot bot;@Overridepublic int process(List<Update> updates) {updates.forEach(update -> {log.info("机器人收到消息 -> {}", update);});return UpdatesListener.CONFIRMED_UPDATES_ALL;}@PostConstructpublic void run() {// Create your bot passing the token received from @BotFatherthis.bot = new TelegramBot(this.telegramBotToken);// Register for updatesthis.bot.setUpdatesListener(this);}/*** 发送消息** @param type   消息类型* @param chatId 会话ID* @param text   消息内容*/public void sendMessage(Byte type, long chatId, String text) {SendResponse response;if (type == 1) {// 图片response = bot.execute(new SendPhoto(chatId, text));} else {// 文本response = bot.execute(new SendMessage(chatId, text));}log.info("发送消息 -> {}", response);}public void close() {this.bot.removeGetUpdatesListener();}}

使用时直接调用即可

TelegramBotServiceImpl.sendMessage(Byte.valueOf("2"), 6576654010L, "请求测试")

在这里插入图片描述

http://www.xdnf.cn/news/4614.html

相关文章:

  • [特殊字符]【实战教程】用大模型LLM查询Neo4j图数据库(附完整代码)
  • 赋能金融科技创新,Telerik打造高效、安全的金融应用解决方案!
  • Linux58 ssh服务配置 jumpserver 测试双网卡 为何不能ping通ip地址
  • 从ellisys空口分析蓝牙耳机回连手机失败案例
  • 正则表达式(Regular Expression)详解
  • 关于ubuntu下交叉编译arrch64下的gtsam报错问题,boost中boost_regex.so中连接libicui18n.so.55报错的问题
  • 【Python 字符串】
  • Java常用API:深度解析与实践应用
  • 【Spring Boot 多模块项目】@MapperScan失效、MapperScannerConfigurer 报错终极解决方案
  • 安装 Docker
  • ZC706开发板教程:windows下编译ADRV9009
  • vue 中如何使用region?
  • PyTorch 实战:从 0 开始搭建 Transformer
  • 解决word里插入公式后打不开的问题
  • Linux-openeuler更换yum镜像源
  • uniapp + vue3 + 京东Nut动作面板组件:实现登录弹框组件(含代码、案例、小程序截图)
  • 村田与RohdeSchwarz联合开发用于测量Digital ET省电效果的RF系统
  • 网络化:DevOps 工程的必要基础(Networking: The Essential Foundation for DevOps Engineering)
  • 幂等的几种解决方案以及实践
  • STM32G070xx将Flash页分块方式存储,固定数据块存储,实现一次擦除多次写入
  • 【C语言】文件操作(续)
  • 一个用C#开发的记事本Notepads开源编辑器
  • Python实现中文数字与阿拉伯数字映射生成器(支持0-9999)
  • WebFlux与HttpStreamable关系解析
  • HuggingFace与自然语言处理(从框架学习到经典项目实践)[ 01 API操作 ]
  • 极简远程革命:节点小宝 — 无公网IP的极速内网穿透远程解决方案
  • 《开源先锋Apache软件基金会:历史沿革、顶级项目与行业影响》
  • 新能源汽车赛道变局:传统车企子品牌私有化背后的战略逻辑
  • java 破解aspose.words 18.6 使用
  • 如何使用 QuickAPI 推动医院数据共享 —— 基于数据仓库场景的实践