新增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

41
pom.xml
View File

@ -10,18 +10,23 @@
</parent>
<groupId>com.yutou</groupId>
<artifactId>tools</artifactId>
<version>1.0.9.5</version>
<version>1.0.10</version>
<name>tools</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<kotlin.version>1.4.10</kotlin.version>
</properties>
<repositories>
<repository>
<id>jaudiotagger-repository</id>
<url>https://dl.bintray.com/ijabz/maven</url>
</repository>
<repository>
<id>jcenter</id>
<url>https://jcenter.bintray.com/</url>
</repository>
</repositories>
<dependencies>
<dependency>
@ -101,14 +106,48 @@
<artifactId>ealvatag</artifactId>
<version>0.4.3</version>
</dependency>
<dependency>
<groupId>net.mamoe</groupId>
<artifactId>mirai-core-qqandroid</artifactId>
<version>1.3.2</version> <!-- 替换版本为最新版本 -->
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>

View File

@ -3,27 +3,29 @@ package com.yutou.tools;
import com.yutou.tools.home.nas.MusicController;
import com.yutou.tools.utils.APIFilter;
import com.yutou.tools.utils.MusicTools;
import com.yutou.tools.utils.QQBotManager;
import com.yutou.tools.utils.RedisTools;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import java.io.IOException;
@SpringBootApplication
public class ToolsApplication {
public static void main(String[] args) {
System.out.println("当前版本号:1.0.9.5");
System.out.println("当前版本号:1.0.10");
SpringApplication.run(ToolsApplication.class, args);
RedisTools.initRedisPoolSub();
if(APIFilter.isDebug) {
MusicController.defaultMusicPath="C:\\Users\\admin\\Music\\";
if (APIFilter.isDebug) {
MusicController.defaultMusicPath = "C:\\Users\\admin\\Music\\";
MusicTools.getInstance().setMusicPath("C:\\Users\\admin\\Music\\");
}
else{
MusicController.defaultMusicPath="/media/yutou/4t/public/音乐";
} else {
MusicController.defaultMusicPath = "/media/yutou/4t/public/音乐";
MusicTools.getInstance().setMusicPath("/media/yutou/4t/public/音乐");
}
QQBotManager.getInstance();
}
}

View File

@ -3,10 +3,15 @@ package com.yutou.tools.home.nas;
import com.alibaba.fastjson.JSONObject;
import com.yutou.tools.interfaces.DownloadInterface;
import com.yutou.tools.utils.OSSTools;
import com.yutou.tools.utils.QQBotManager;
import com.yutou.tools.utils.RedisTools;
import com.yutou.tools.utils.Tools;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import java.io.File;
@ -14,16 +19,38 @@ import java.util.Base64;
import java.util.UUID;
@Controller
@RequestMapping("/localhome/bot")
@RequestMapping("/qq/bot")
public class QQBot {
@RequestMapping("test.do")
@ResponseBody
public String test(){
return "123";
}
@RequestMapping("send.do")
@ResponseBody
public String send(String msg){
public String send(String msg,String imgUrl){
JSONObject json=new JSONObject();
try {
boolean ret= RedisTools.set(1,"msg_"+System.currentTimeMillis(),msg);
String ret;
if(StringUtils.isEmpty(imgUrl)){
ret= QQBotManager.getInstance().sendMessage(msg);
}else{
Tools.download(imgUrl, new DownloadInterface() {
@Override
public void onDownload(String file) {
super.onDownload(file);
QQBotManager.getInstance().sendMessage(new File(file),msg);
}
@Override
public void onError(Exception e) {
super.onError(e);
QQBotManager.getInstance().sendMessage(imgUrl+"\n"+msg);
}
});
ret="图片下载中";
}
json.put("code",0);
json.put("msg",ret);
}catch (Exception e){
@ -33,51 +60,25 @@ public class QQBot {
}
return json.toJSONString();
}
@RequestMapping("msg.do")
@RequestMapping("sendAndImg.do")
@ResponseBody
public String botMsg(String msg,String type){
System.out.println(type);
if(type.trim().equals("download")){
JSONObject json=JSONObject.parseObject(new String(Base64.getDecoder().decode(msg)));
String url=json.getString("url");
if(url.endsWith(".apk")){
String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
Jedis redisTools = RedisTools.getRedis();
redisTools.select(1);
redisTools.setex("downloadApk_" + uuid, RedisTools.TOKEN_TIMEOUT_DEFAULT, msg);
redisTools.close();
String packageName=null;
String version=null;
packageName=RedisTools.get("csj_packageName",1);
version=RedisTools.get("csj_version",1);
if(packageName==null){
packageName="穿山甲包名";
}
if(version==null){
version="穿山甲版本号";
}
RedisTools.set(1, "msg_" + System.currentTimeMillis(), "检测到上传apk请选择处理方式" +
"\n1)生成穿山甲资源包 回复 #csj#"+packageName+"#"+version+"#downloadApk_" + uuid + "" +
"\n2)不处理 (360s后自动取消)" +
"\n输入 *set csj_packageName XXX 可设置默认包名" +
"\n输入 *set csj_version XXX 可设置默认版本号");
}
public String botMsg(String msg,@RequestParam("file") MultipartFile file){
JSONObject json=new JSONObject();
if(file==null){
json.put("code",-1);
json.put("msg","图片为空");
return json.toJSONString();
}
return "OK~";
}
@RequestMapping("csj/assets.do")
@ResponseBody
public String csjAssets(String data,String packagename,String version){
new Thread(new Runnable() {
@Override
public void run() {
String msg = RedisTools.get(data, 1);
JSONObject json=JSONObject.parseObject(new String(Base64.getDecoder().decode(msg)));
json.put("package",packagename);
json.put("version",version);
BotTools.download(json);
}
}).start();
return "ok";
String ret;
try {
String path = Tools.createFile("qq_image",file, file.getName());
ret=QQBotManager.getInstance().sendMessage(new File(path),msg);
} catch (Exception e) {
e.printStackTrace();
ret=e.getLocalizedMessage();
}
json.put("code",0);
json.put("msg",ret);
return json.toJSONString();
}
}

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");