事情是这样的,拿家里的树莓派跑了个tgBot,这个bot会获取一些咨询然后自动发到频道里,但是这出租房里的网络不怎么理想,总是会有断连的情况。科学上网你懂的。树莓派的性能不是那么好,装个teamviewer会卡死。为了能够远程处理一些问题,就再做了个机器人,思路是这样的,在tg上下发linux指令,机器人收到指令并执行。比如杀掉某个进程然后重启。也借此机会给不知道怎么创建机器人的小伙伴一些参考。
撸代码前要知道的事:
1、保证网络正常,如果使用的某些梯子软件,一般可以在梯子的设置里找到代理的一些配置,如下图会有http和socks 5 两种。后面我们只会用其中一种。这是使用梯子,后面代码里ProxyType我们会使用代理的情况。还有就是家里有软路由,就不需要在乎这些了,虽然软路由也可以配置这些。你开心就好。。
图里是我的邮箱和QQ,可以一起交流,QQ基本不用了,邮箱还是用的比较多
2、在tg上找到@BotFather创建你的bot
下面是一个最普通的maven项目,首先导入telegrambots依赖
<!-- telegrambots -->
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>5.7.1</version>
</dependency>
几个类也很简单
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ExecBot extends TelegramLongPollingBot {
//填你自己的token和username
private String token ="bot_token";
private String username ="xxx_bot";
public ExecBot() {
this( new DefaultBotOptions());
}
public ExecBot(DefaultBotOptions options) {
super(options);
}
@Override
public String getBotToken() {
return this.token;
}
@Override
public String getBotUsername() {
return this.username;
}
@Override
public void onUpdateReceived(Update update) {
if (update.hasMessage()) {
Message message = update.getMessage();
Long chatId = message.getChatId();
String text = message.getText();
String[] split = text.split("=");
if (split.length == 1){
sendMsg("不处理该类指令",chatId);
return;
}
String s = executeLinuxCmd(split[1]);
sendMsg(s,chatId);
}
}
//回复普通文本消息
public void sendMsg(String text,Long chatId){
SendMessage response = new SendMessage();
response.setChatId(String.valueOf(chatId));
response.setText(text);
try {
execute(response);
} catch (TelegramApiException e) {
}
}
public String executeLinuxCmd(String cmd) {
System.out.println("执行命令[ " + cmd + " ]");
Runtime run = Runtime.getRuntime();
try {
Process process = run.exec(cmd);
String line;
BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuffer out = new StringBuffer();
while ((line = stdoutReader.readLine()) != null ) {
out.append(line+"\n");
}
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
process.destroy();
return out.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
import org.telegram.telegrambots.bots.DefaultBotOptions;
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) {
//梯子在自己电脑上就写127.0.0.1 软路由就写路由器的地址
String proxyHost = "127.0.0.1";
//端口根据实际情况填写,说明在上面,自己看
int proxyPort = 1080;
DefaultBotOptions botOptions = new DefaultBotOptions();
botOptions.setProxyHost(proxyHost);
botOptions.setProxyPort(proxyPort);
//注意一下这里,ProxyType是个枚举,看源码你就知道有NO_PROXY,HTTP,SOCKS4,SOCKS5;
botOptions.setProxyType(DefaultBotOptions.ProxyType.SOCKS5);
DefaultBotSession defaultBotSession = new DefaultBotSession();
defaultBotSession.setOptions(botOptions);
try {
TelegramBotsApi telegramBotsApi = new TelegramBotsApi(defaultBotSession.getClass());
//需要代理
// ExecBot bot = new ExecBot(botOptions);
// telegramBotsApi.registerBot(bot);
//不需代理
ExecBot bot2 = new ExecBot();
telegramBotsApi.registerBot(bot2);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
}
效果还是符合我的预期的。
另外,机器人也可以发送图片,视频等。有问题可交流。
API地址
想了解更多,评论区见吧!