1、简要说明
Telegram官方文档地址:https://core.telegram.org/bots
Telegram Bot Java CodeExample:https://github.com/rubenlagus/TelegramBots
Telegram Bot 各语言版本CodeExample:https://core.telegram.org/bots/samples
2、申请创建一个Bot
2.1、去往Telegram,搜索框输入“@BotFather”
2.2、创建一个Bot
2.3、BotFather会提示你:给自己的Bot起一个名字
2.4、然后会让你起一个用户名,需要用bot结尾
2.5、保存好对应的数据
长的就是token,自己起的username也要保存记住
3、创建一个Maven工程,并引入TelegramBot依赖
代码如下:
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>6.5.0</version>
</dependency>
4、编写一个消息处理类
代码如下:
package com.saotolls.upstream;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText;
import org.telegram.telegrambots.meta.api.objects.CallbackQuery;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.User;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import java.util.ArrayList;
import java.util.List;
// 我自己的群组机器人回调
public class MyChannelHandler extends TelegramLongPollingBot
{
/**
* 消息事件回调
* [url=home.php?mod=space&uid=952169]@Param[/url] update
*/
public void onUpdateReceived(Update update)
{
Message message = update.getMessage();
System.out.println("update = " + update);
// 判断是否是来自消息点击回调
if (update.hasCallbackQuery())
{
CallbackQuery callbackQuery = update.getCallbackQuery();
String data = callbackQuery.getData();
System.out.println("回调参数:" + data);
// 创建更新信息实体
EditMessageText editMessageText = new EditMessageText();
// 设置更新文本内容
editMessageText.setText(genContent(Integer.parseInt(data.split(":")[2])));
// 设置跟踪会话消息内容的id等信息
editMessageText.setChatId(callbackQuery.getMessage().getChatId().toString());
editMessageText.setInlineMessageId(callbackQuery.getInlineMessageId());
editMessageText.setMessageId(callbackQuery.getMessage().getMessageId());
// 创建按钮
InlineKeyboardMarkup button = getButton(data);
// 设置按钮到消息
editMessageText.setReplyMarkup(button);
try
{
// 异步发送消息
executeAsync(editMessageText);
}
catch (TelegramApiException e)
{
e.printStackTrace();
}
}
else
{
// 如果不是来自原有消息的回调,那就是新会话
SendMessage sendMessage = new SendMessage();
// 判断是否是机器人会话采集数据
User from = message.getFrom();
if (from.getIsBot()) {
System.out.println("是机器人来访问,取消会话");
return;
}
// 包裹对应会话内容
sendMessage.enableMarkdown(true);
sendMessage.setChatId(message.getChatId());
sendMessage.setReplyToMessageId(message.getMessageId());
// 发送回复消息内容
sendMessage.enableMarkdown(true);
sendMessage.setText(genContent(1));
sendMessage.disableWebPagePreview();
// 创建按钮信息以及绑定到消息
InlineKeyboardMarkup button = getButton(null);
sendMessage.setReplyMarkup(button);
// 发送消息
try
{
// 异步发送消息
executeAsync(sendMessage);
}
catch (TelegramApiException e)
{
e.printStackTrace();
}
}
}
// 模拟数据
public String genContent(int index)
{
int start = (index - 1) * 10 + 1;
int end = index * 10;
StringBuilder stringBuilder = new StringBuilder();
for (int i = start; i <= end; i++)
{
stringBuilder.append(i).append(".").append(System.currentTimeMillis()).append("\n");
}
return stringBuilder.toString();
}
// 获取机器人名称
public String getBotUsername()
{
return "demoOf52PojieBot";
}
@Override
public String getBotToken()
{
return "5954487876:AAEzodMSlpCjKJ8E0Y1IAEdCj-5h-_ABzhA";
}
// 生成消息底部按钮
public InlineKeyboardMarkup getButton(String callbackData)
{
int index = 1;
if (callbackData != null)
{
String[] split = callbackData.split(":");
index = Integer.parseInt(split[2]);
}
// 设置按钮
InlineKeyboardMarkup markupInline = new InlineKeyboardMarkup();
List<List<InlineKeyboardButton>> keyboards = new ArrayList<>();
List<InlineKeyboardButton> links = new ArrayList<>();
InlineKeyboardButton link = new InlineKeyboardButton();
link.setText("吾爱破解论坛");
link.setUrl("https://www.52pojie.cn/");
links.add(link);
keyboards.add(links);
List<InlineKeyboardButton> buttons = new ArrayList<>();
if (index > 1)
{
InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton();
inlineKeyboardButton.setText("上一页");
inlineKeyboardButton.setCallbackData("callback::" + (index - 1));
buttons.add(inlineKeyboardButton);
}
InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton();
inlineKeyboardButton.setText("下一页");
inlineKeyboardButton.setCallbackData("callback::" + (index + 1));
buttons.add(inlineKeyboardButton);
keyboards.add(buttons);
markupInline.setKeyboard(keyboards);
return markupInline;
}
}
5、创建一个启动类
代码如下:
package com.saotolls;
import com.saotolls.upstream.MyChannelHandler;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
public class Main
{
public static void main(String[] args)
{
// 设置socks5代{过}{滤}理
System.setProperty("socksProxyHost", "127.0.0.1");
System.setProperty("socksProxyPort", "7890");
// 注册Tg
try {
TelegramBotsApi telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class);
telegramBotsApi.registerBot(new MyChannelHandler());
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
}
技术宅小伙
- 粉丝: 376
- 资源: 1799
最新资源
- 塑胶结构设计-螺丝柱设计
- Android开发中使用的google定位的总结:主要有四种方式:有需要自行寻找对应的方式方法
- 程序员专用的HTML5个人简历模版源代码+手机端
- 禾川HCQ1系列PAC脉冲控制步进驱动器测试程序
- 8255 并行接口实验-微机原理与接口技术课程设计
- 小程序快速实现大模型聊天机器人
- 金属、有机物、非有机物检测67-YOLO(v7至v9)、COCO、CreateML、Darknet、Paligemma数据集合集.rar
- 8254 定时计数器应用实验-微机原理与接口技术课程设计
- CSP竞赛全方位攻略:备赛指南与应考技巧
- IB Specification Vol 1-Release-1.9-Draft-2024-08-31
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈