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

[TG开发]简单的回声机器人

你好! 如果你想了解如何在Java上编写Telegram机器人,你来对地方了!

准备启动

机器人API基于HTTP请求,但在本书中我将使用Rubenlagus的Java库

安装库

你可以使用不同的方法安装TelegramBots库, 我这里使用Maven

<dependency><groupId>org.telegram</groupId><artifactId>telegrambots</artifactId><version>Latest</version>
</dependency>

让我们开始编码吧

在本节课中,我们将编写一个简单的机器人,它会回显我们发送给它的所有内容。现在,打开inteliidea,创建一个新项目。你可以随意给它起个名字。

  1. 现在,当你在该项目中后,在src目录下创建文件MyAmazingBot.java和Main,java。打开MyAmazingBot.java,并开始编写我们的实际机器人!
  2. 记住! 类必须继承TelegramLongPollingBot并实现必要的方法。
import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.exceptions.TelegramApiException;public class MyAmazingBot extends TelegramLongPollingBot {@Overridepublic void onUpdateReceived(Update update) {// TODO}@Overridepublic String getBotUsername() {// TODOreturn null;}@Overridepublic String getBotToken() {// TODOreturn null;}
}
  1. 正如您所理解的,

`getBotUsername()'和`getBotToken ()`必须返回从 @BotFather获取的机器人的用户名和令牌。

import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.exceptions.TelegramApiException;public class MyAmazingBot extends TelegramLongPollingBot {@Overridepublic void onUpdateReceived(Update update) {// TODO}@Overridepublic String getBotUsername() {// Return bot username// If bot username is @MyAmazingBot, it must return 'MyAmazingBot'return "MyAmazingBot";}@Overridepublic String getBotToken() {// Return bot token from BotFatherreturn "12345:qwertyuiopASDGFHKMK";}
}
  1. 现在,让我们转到我们机器人的逻辑部分。

如前所述,我们希望它能够回复我们发送给它的每条文本。`onUpdateReceived(Updateupdate)`方法就是为此而设的。当接收到一条更新时,该方法会被调用。

@Override
public void onUpdateReceived(Update update) {// We check if the update has a message and the message has textif (update.hasMessage() && update.getMessage().hasText()) {// Set variablesString message_text = update.getMessage().getText();long chat_id = update.getMessage().getChatId();SendMessage message = new SendMessage() // Create a message object object.setChatId(chat_id).setText(message_text);try {execute(message); // Sending our message object to user} catch (TelegramApiException e) {e.printStackTrace();}}
}
  1. 该如何运行这个机器人呢? 保存该文件并打开Mainjava。这个文件将实例化TelegramBotsApi并注册我们的新机器人。
import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.TelegramBotsApi;
import org.telegram.telegrambots.exceptions.TelegramApiException;
public class Main {public static void main(String[] args) {// TODO Initialize Api Context// TODO Instantiate Telegram Bots API// TODO Register our bot}
}
  1. 现在,让我们初始化API上下文
import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.TelegramBotsApi;
import org.telegram.telegrambots.exceptions.TelegramApiException;
public class Main {public static void main(String[] args) {// Initialize Api ContextApiContextInitializer.init();// TODO Instantiate Telegram Bots API// TODO Register our bot}
}
  1. 实例化Telegram机器人API:
import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.TelegramBotsApi;
import org.telegram.telegrambots.exceptions.TelegramApiException;
public class Main {public static void main(String[] args) {// Initialize Api ContextApiContextInitializer.init();// Instantiate Telegram Bots APITelegramBotsApi botsApi = new TelegramBotsApi();// TODO Register our bot}
}
  1. 并注册我们的机器人:
import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.TelegramBotsApi;
import org.telegram.telegrambots.exceptions.TelegramApiException;
public class Main {public static void main(String[] args) {// Initialize Api ContextApiContextInitializer.init();// Instantiate Telegram Bots APITelegramBotsApi botsApi = new TelegramBotsApi();// Register our bottry {botsApi.registerBot(new MyAmazingBot());} catch (TelegramApiException e) {e.printStackTrace();}}
}
  1. 这是我们的所有文件:
import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.TelegramBotsApi;
import org.telegram.telegrambots.exceptions.TelegramApiException;
public class Main {public static void main(String[] args) {// Initialize Api ContextApiContextInitializer.init();// Instantiate Telegram Bots APITelegramBotsApi botsApi = new TelegramBotsApi();// Register our bottry {botsApi.registerBot(new MyAmazingBot());} catch (TelegramApiException e) {e.printStackTrace();}}
}
import org.telegram.telegrambots.api.methods.send.SendMessage;import org.telegram.telegrambots.api.objects.Update;import org.telegram.telegrambots.bots.TelegramLongPollingBot;import org.telegram.telegrambots.exceptions.TelegramApiException;public class MyAmazingBot extends TelegramLongPollingBot {@Overridepublic void onUpdateReceived(Update update) {// We check if the update has a message and the message has textif (update.hasMessage() && update.getMessage().hasText()) {// Set variablesString message_text = update.getMessage().getText();long chat_id = update.getMessage().getChatId();SendMessage message = new SendMessage() // Create a message object object.setChatId(chat_id).setText(message_text);try {execute(message); // Sending our message object to user} catch (TelegramApiException e) {e.printStackTrace();}}}@Overridepublic String getBotUsername() {// Return bot username// If bot username is @MyAmazingBot, it must return 'MyAmazingBot'return "MyAmazingBot";}@Overridepublic String getBotToken() {// Return bot token from BotFatherreturn "12345:qwertyuiopASDGFHKMK";}
}
  1. 现在我们可以将项目打包成可运行的jar文件,并在我们的计算机/服务器上运行它!
java -jar MyAmazingBot.jar

http://www.dtcms.com/a/330213.html

相关文章:

  • Linux信号量和信号
  • 淘汰人工巡检!企业配电室无线测温实战:0布线+240点位同步监控
  • @进程管理工具 - Glances工具详细指南
  • 20250813测试开发岗(凉)面
  • 《探索C++ set与multiset容器:深入有序唯一性集合的实现与应用》
  • 网络存储技术:数据存储架构的演进与全景解析
  • 计算机网络——协议
  • 基于SpringBoot+Vue的智能消费记账系统(AI问答、WebSocket即时通讯、Echarts图形化分析)
  • Python 类元编程(元类基础知识)
  • 推荐系统论文分享之多任务模型--PLE(二)
  • python与JavaScript的区别
  • MoviiGen1.1模型脚本调用
  • C语言队列的实现
  • AUTOSAR进阶图解==>AUTOSAR_SWS_TTCANInterface
  • 开发避坑指南(25):MySQL不支持带有limit语句的子查询的解决方案
  • 【学习嵌入式day23-Linux编程-文件IO】
  • imx6ull-驱动开发篇22——Linux 时间管理和内核定时器
  • 力扣top100(day02-04)--二叉树 01
  • 18.10 SQuAD数据集实战:5步高效获取与预处理,BERT微调避坑指南
  • 数据分析可视化学习总结(美妆2)
  • Python解包技巧全解析
  • Python 基础语法(一)
  • 多处理器技术:并行计算的基石与架构演进
  • 疯狂星期四文案网第38天运营日记
  • 继《念念有词》后又一作品《双刃》开播 马来西亚新人演员业文Kevin挑战多面角色引期待
  • CF每日3题(1600)
  • element-ui 时间线(timeLine)内容分成左右两侧
  • npm run dev 的作用
  • Unity_2D动画
  • 游戏盾的安全作用