新增QQ机器人

恢复酷Q机器人功能
This commit is contained in:
yutou
2020-10-27 17:20:31 +08:00
parent 023008b609
commit 1b21930013
9 changed files with 305 additions and 84 deletions

View File

@@ -31,10 +31,10 @@ public class APIFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
/* if(isDebug){
if(isDebug){
filterChain.doFilter(servletRequest, servletResponse);
return;
}*/
}
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
String token = request.getParameter("token");

View File

@@ -28,6 +28,7 @@ public class ConfigTools {
}
public static Object load(String type,String key){
File file=new File(type);
System.out.println(type+"配置文件地址:"+file.getAbsolutePath());
String src=readFile(file);
if(src!=null){
JSONObject json=JSONObject.parseObject(src);
@@ -37,7 +38,7 @@ public class ConfigTools {
}
return json.getOrDefault(key, "");
}
return null;
return "";
}
public static boolean save(String type,String key,Object data){
File file=new File(type);

View File

@@ -19,7 +19,7 @@ public class MusicTools {
public static final int FIND_ARTIST = 2;
private static MusicTools tools;
private String musicPath = "C:\\Users\\admin\\Music";
private String musicPath = "/media/yutou/4t/public/音乐";
private final List<MusicData> musicList = new ArrayList<>();
private HashMap<String, List<MusicData>> musicMap = new HashMap<String, List<MusicData>>();
private boolean isScan = false;
@@ -51,6 +51,7 @@ public class MusicTools {
}
System.out.println("执行扫描");
musicList.clear();
musicMap.clear();
new Thread(() -> {
isScan = true;
scan(new File(musicPath));

View File

@@ -0,0 +1,125 @@
package com.yutou.tools.utils;
import kotlin.coroutines.CoroutineContext;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.BotFactoryJvm;
import net.mamoe.mirai.contact.Contact;
import net.mamoe.mirai.event.EventHandler;
import net.mamoe.mirai.event.Events;
import net.mamoe.mirai.event.ListeningStatus;
import net.mamoe.mirai.event.SimpleListenerHost;
import net.mamoe.mirai.message.GroupMessageEvent;
import net.mamoe.mirai.message.MessageReceipt;
import net.mamoe.mirai.message.data.Image;
import net.mamoe.mirai.message.data.MessageChain;
import net.mamoe.mirai.message.data.MessageUtils;
import net.mamoe.mirai.utils.BotConfiguration;
import net.mamoe.mirai.utils.MiraiLogger;
import org.jetbrains.annotations.NotNull;
import java.io.File;
public class QQBotManager {
private static QQBotManager botManager = null;
private Bot bot;
private static final long qqGroup = 891655174L;
private boolean isLogin=false;
private QQBotManager() {
Object isRun = ConfigTools.load(ConfigTools.CONFIG, "qq_bot");
if (isRun != null && (boolean) isRun) {
init();
isLogin=true;
}
}
private void init() {
new Thread(new Runnable() {
@Override
public void run() {
long qq=2476945931L;
String password="zhang34864394";
if(true) {
qq = 3620756944L;
password = "UAs6YBYMyxJU";
}
bot = BotFactoryJvm.newBot(qq, password, new BotConfiguration() {
{
fileBasedDeviceInfo("qq_bot_devices_info.json");
}
});
Events.registerEvents(bot, new MessageListener());
bot.login();
bot.join();
}
}).start();
}
public static QQBotManager getInstance() {
if (botManager == null) {
botManager = new QQBotManager();
}
return botManager;
}
public boolean isLogin(){
return isLogin;
}
private String getNotLoginQQ(){
return "没有登录QQ";
}
public String sendMessage(String text) {
if (bot != null) {
MessageReceipt<Contact> receipt= bot.getGroup(qqGroup).sendMessage(text);
return receipt.toString();
}
return getNotLoginQQ();
}
public String sendMessage(File imageFile, String text) {
if (bot != null) {
Image image = bot.getGroup(qqGroup).uploadImage(imageFile);
MessageChain chain = MessageUtils.newImage(image.getImageId()).plus(text);
MessageReceipt<Contact> receipt=bot.getGroup(qqGroup).sendMessage(chain);
return receipt.toString();
}
return getNotLoginQQ();
}
public static void main(String[] args) {
getInstance();
}
private static class MessageListener extends SimpleListenerHost {
@Override
public void handleException(@NotNull CoroutineContext context, @NotNull Throwable exception) {
super.handleException(context, exception);
System.out.println("error: "+exception.getLocalizedMessage());
}
@EventHandler
public ListeningStatus onGroupMessage(GroupMessageEvent event) {
if(event.getGroup().getId()==qqGroup){
String msg=event.getMessage().contentToString();
msg=msg.replace("","!");
switch (msg){
case "!开机":
RedisTools.Consumer.system("openPC",null);
break;
case "!更新IP":
RedisTools.Consumer.system("updateIP",null);
break;
case "!ip":
RedisTools.Consumer.bot("getip");
break;
default:
if(msg.startsWith("!cmd")){
RedisTools.Consumer.system("cmd",msg.replace("!cmd",""));
}
}
}
return ListeningStatus.LISTENING;
}
}
}

View File

@@ -204,7 +204,7 @@ public class RedisTools {
}
private static class Consumer extends JedisPubSub {
protected static class Consumer extends JedisPubSub {
@Override
public void onPMessage(String pattern, String channel, String message) {
super.onPMessage(pattern, channel, message);
@@ -238,7 +238,7 @@ public class RedisTools {
System.out.println("onMessage: channel=" + channel + " msg=" + message);
}
private void system(String type, String value) {
public static void system(String type, String value) {
try {
String exec = null;
switch (type) {
@@ -265,23 +265,24 @@ public class RedisTools {
private void bot(String value) {
public static void bot(String value) {
switch (value) {
case "getip":
JSONObject json = JSONObject.parseObject(Tools.get("https://api.asilu.com/ip/"));
String ip = json.getString("ip");
RedisTools.set(1, "msg_" + System.currentTimeMillis(), "服务器IP:\n" + ip);
QQBotManager.getInstance().sendMessage("服务器IP:\n" + ip);
break;
}
}
}
public static void processOut(InputStream inputStream) {
String tmp, str = "null";
String tmp;
StringBuilder str = new StringBuilder("null");
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
while ((tmp = reader.readLine()) != null) {
str += tmp + "\n";
str.append(tmp).append("\n");
}
reader.close();
inputStream.close();
@@ -289,7 +290,7 @@ public class RedisTools {
e.printStackTrace();
}
System.out.println("cmd > " + str);
RedisTools.set(1, "msg_" + System.currentTimeMillis(), str);
QQBotManager.getInstance().sendMessage(str.toString());
System.out.println("线程结束");
}
public static void main(String[] args) {

View File

@@ -7,6 +7,7 @@ import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
@@ -92,13 +93,62 @@ public class Tools {
int i = inputStream.read();
inputStream.close();
connection.disconnect();
if (UpdateIp.nas_ip != null) {
String url="http://" + UpdateIp.nas_ip + ":8000/localhome/bot/send.do?msg=" + URLEncoder.encode((title + "\n" + msg), "UTF-8") + "&token=zIrsh9TUZP2lfRW753PannG49E7VJvor";
if (ConfigTools.load(ConfigTools.CONFIG, "model").equals("nas")) {
String img = null;
msg = msg.replace("<br/>", "\n");
if (msg.contains("![logo]")) {
try {
img = msg.split("!\\[logo\\]\\(")[1].split("\\)")[0];
msg = msg.replace("![logo](" + img + ")", "");
} catch (Exception e) {
e.printStackTrace();
}
}
if (img == null) {
QQBotManager.getInstance().sendMessage(title + "\n" + msg);
} else {
String finalMsg = msg;
String finalImg = img;
if (QQBotManager.getInstance().isLogin()) {
download(img, new DownloadInterface() {
@Override
public void onDownload(String file) {
super.onDownload(file);
QQBotManager.getInstance().sendMessage(new File(file), title + "\n" + finalMsg);
}
@Override
public void onError(Exception e) {
super.onError(e);
QQBotManager.getInstance().sendMessage(title + "\n" + finalMsg + "\n" + finalImg);
}
});
}
}
} else if (!StringUtils.isEmpty(UpdateIp.nas_ip)) {
String img = null;
msg = msg.replace("<br/>", "\n");
if (msg.contains("![logo]")) {
try {
img = msg.split("!\\[logo\\]\\(")[1].split("\\)")[0];
msg = msg.replace("![logo](" + img + ")", "");
} catch (Exception e) {
e.printStackTrace();
}
}
String url;
if (img == null) {
url = "http://" + UpdateIp.nas_ip + ":8000/qq/bot/send.do?msg=" + URLEncoder.encode((title + "\n" + msg), "UTF-8") + "&token=zIrsh9TUZP2lfRW753PannG49E7VJvor";
} else {
url = "http://" + UpdateIp.nas_ip + ":8000/qq/bot/send.do?msg=" + URLEncoder.encode((title + "\n" + msg), "UTF-8")
+ "&imgUrl=" + img
+ "&token=zIrsh9TUZP2lfRW753PannG49E7VJvor";
}
System.out.println(url);
connection = (HttpURLConnection) new URL(url).openConnection();
connection.connect();
inputStream=connection.getInputStream();
i=inputStream.read();
inputStream = connection.getInputStream();
i = inputStream.read();
inputStream.close();
connection.disconnect();
}
@@ -191,7 +241,7 @@ public class Tools {
public static String get(String url) {
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36");
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder str = new StringBuilder();
String tmp;
@@ -239,19 +289,19 @@ public class Tools {
public static void download(String url, DownloadInterface downloadInterface) {
try {
HttpURLConnection connection= (HttpURLConnection) new URL(url).openConnection();
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.disconnect();
new File("tmp").mkdirs();
File file=new File("tmp"+File.separator+url.trim().split("/")[url.trim().split("/").length-1]);
if(file.exists()){
File file = new File("tmp" + File.separator + url.trim().split("/")[url.trim().split("/").length - 1]);
if (file.exists()) {
file.delete();
}
FileOutputStream outputStream=new FileOutputStream(file);
InputStream inputStream=connection.getInputStream();
byte[] bytes=new byte[4096];
FileOutputStream outputStream = new FileOutputStream(file);
InputStream inputStream = connection.getInputStream();
byte[] bytes = new byte[4096];
int len;
while ((len=inputStream.read(bytes))!=-1){
outputStream.write(bytes,0,len);
while ((len = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, len);
outputStream.flush();
}
outputStream.close();
@@ -265,16 +315,17 @@ public class Tools {
/**
* 构造给前端的文件
*
* @param file 文件路径
* @return 前端获取的文件
*/
public static ResponseEntity<FileSystemResource> getFile(File file){
public static ResponseEntity<FileSystemResource> getFile(File file) {
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
try {
headers.add("Content-Disposition", "attachment; filename=" +URLEncoder.encode(file.getName(),"UTF-8"));
headers.add("Content-Disposition", "attachment; filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
headers.add("Content-Disposition", "attachment; filename=" +file.getName());
headers.add("Content-Disposition", "attachment; filename=" + file.getName());
}
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");