From 7e9fa60f6aa4a871236ac7ed77477b1f4f0cd1bb Mon Sep 17 00:00:00 2001 From: Yutou <583819556@qq.com> Date: Sun, 5 May 2024 16:50:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=9B=BE=E7=89=87=E6=94=AF?= =?UTF-8?q?=E6=8C=81File=20=E8=B0=83=E6=95=B4=E6=B6=A9=E5=9B=BE=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E4=B8=BA=E5=85=88=E8=87=AA=E5=B7=B1=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=EF=BC=8C=E6=97=A0=E6=B3=95=E4=B8=8B=E8=BD=BD=E5=86=8D=E4=B8=A2?= =?UTF-8?q?url=E7=BB=99qq=E6=9C=BA=E5=99=A8=E4=BA=BA=20=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E9=83=A8=E5=88=86=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/yutou/napcat/NapCatQQ.java | 23 +++++++++++- .../java/com/yutou/napcat/handle/Image.java | 7 ++++ .../napcat/handle/MessageHandleBuild.java | 1 - .../java/com/yutou/napcat/handle/Text.java | 4 +- .../java/com/yutou/qqbot/QQBotManager.java | 6 +++ .../com/yutou/qqbot/models/setu/GetSeTu.java | 37 ++++++++++++++++--- .../com/yutou/qqbot/utlis/Base64Tools.java | 4 -- 7 files changed, 67 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/yutou/napcat/NapCatQQ.java b/src/main/java/com/yutou/napcat/NapCatQQ.java index a8b0b17..3cb9ebf 100644 --- a/src/main/java/com/yutou/napcat/NapCatQQ.java +++ b/src/main/java/com/yutou/napcat/NapCatQQ.java @@ -8,8 +8,10 @@ import com.yutou.napcat.model.*; import com.yutou.okhttp.HttpCallback; import com.yutou.napcat.http.NapCatApi; import com.yutou.qqbot.QQBotManager; +import com.yutou.qqbot.utlis.Base64Tools; import lombok.val; +import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -30,8 +32,25 @@ public class NapCatQQ { list.add(new At(583819556L)); list.add(new Text("5")); QQBotManager.getInstance().sendMessage(false, 891655174L, list);*/ - val at = new At(); - System.out.println("at = " + at); + NapCatApi.setLog(false); + File file = new File("C:\\Users\\58381\\Downloads\\0074TT8Yly1hp5mqidwqeg30g20f27wh.gif"); + NapCatApi.getMessageApi().sendPrivateMsg( + MessageHandleBuild.create() + .setQQNumber(583819556L) + //.add(new Image(file)) + .add(new Text("abc")) + .build() + ).enqueue(new HttpCallback() { + @Override + public void onResponse(int code, String status, SendMessageResponse response, String rawResponse) { + System.out.println("code = " + code + ", status = " + status + ", response = " + response + ", rawResponse = " + rawResponse); + } + + @Override + public void onFailure(Throwable throwable) { + throwable.printStackTrace(); + } + }); } /** * 私聊 diff --git a/src/main/java/com/yutou/napcat/handle/Image.java b/src/main/java/com/yutou/napcat/handle/Image.java index 6efb542..95b00b5 100644 --- a/src/main/java/com/yutou/napcat/handle/Image.java +++ b/src/main/java/com/yutou/napcat/handle/Image.java @@ -1,8 +1,11 @@ package com.yutou.napcat.handle; import com.alibaba.fastjson2.annotation.JSONField; +import com.yutou.qqbot.utlis.Base64Tools; import lombok.Data; +import java.io.File; + public class Image extends BaseHandle { public Image() { @@ -13,6 +16,10 @@ public class Image extends BaseHandle { super("image"); data = new ImageInfo(imageUrl); } + public Image(File imageFile){ + super("image"); + data=new ImageInfo("base64://"+ Base64Tools.encode(imageFile)); + } @Data public static class ImageInfo { diff --git a/src/main/java/com/yutou/napcat/handle/MessageHandleBuild.java b/src/main/java/com/yutou/napcat/handle/MessageHandleBuild.java index b10257f..ff782fd 100644 --- a/src/main/java/com/yutou/napcat/handle/MessageHandleBuild.java +++ b/src/main/java/com/yutou/napcat/handle/MessageHandleBuild.java @@ -51,7 +51,6 @@ public class MessageHandleBuild { } json.put("auto_escape", autoEscape); json.put("message", msgList); - System.out.println(json); return json; } } diff --git a/src/main/java/com/yutou/napcat/handle/Text.java b/src/main/java/com/yutou/napcat/handle/Text.java index b45b814..f820c24 100644 --- a/src/main/java/com/yutou/napcat/handle/Text.java +++ b/src/main/java/com/yutou/napcat/handle/Text.java @@ -39,7 +39,7 @@ public class Text extends BaseHandle { @Override public String toString() { - return data.text; + return data.text.trim(); } @Data @@ -47,7 +47,7 @@ public class Text extends BaseHandle { String text; public TextInfo(String text) { - this.text = text; + this.text = text.trim(); } } } diff --git a/src/main/java/com/yutou/qqbot/QQBotManager.java b/src/main/java/com/yutou/qqbot/QQBotManager.java index f966fcd..98636f0 100644 --- a/src/main/java/com/yutou/qqbot/QQBotManager.java +++ b/src/main/java/com/yutou/qqbot/QQBotManager.java @@ -5,6 +5,7 @@ import com.alibaba.fastjson2.JSONObject; import com.yutou.napcat.QQDatabase; import com.yutou.napcat.handle.BaseHandle; import com.yutou.napcat.handle.MessageHandleBuild; +import com.yutou.napcat.handle.Reply; import com.yutou.napcat.handle.Text; import com.yutou.napcat.http.NapCatApi; import com.yutou.napcat.model.FriendBean; @@ -127,6 +128,11 @@ public class QQBotManager { if (item.getData() == null) { continue; } + if (item instanceof Reply) { + if (((Reply) item).getData().getId() == -1) { + continue; + } + } handleBuild.add(item); } Response> response; diff --git a/src/main/java/com/yutou/qqbot/models/setu/GetSeTu.java b/src/main/java/com/yutou/qqbot/models/setu/GetSeTu.java index d915101..f0265f4 100644 --- a/src/main/java/com/yutou/qqbot/models/setu/GetSeTu.java +++ b/src/main/java/com/yutou/qqbot/models/setu/GetSeTu.java @@ -219,14 +219,36 @@ public class GetSeTu extends Model { builder.append(tags).append("、"); } builder.append("\n看不到图?点这里:").append(item.getJSONObject("urls").getString("regular")); - QQBotManager.getInstance().sendMessage(false, qq, - new Image(item.getJSONObject("urls").getString("regular")), - new Reply(event.getMessageId()) - ); QQBotManager.getInstance().sendMessage(false, qq, new Reply(event.getMessageId()), new Text(builder.toString()) ); + HttpTools.download(item.getJSONObject("urls").getString("regular"), + System.currentTimeMillis() + ".png", + true, + new DownloadInterface() { + @Override + public void onDownload(File file) { + super.onDownload(file); + Log.i("下载完成"); + QQBotManager.getInstance().sendMessage(false, qq, + new Image(file), + new Reply(event.getMessageId()) + ); + } + + @Override + public void onError(Exception e) { + super.onError(e); + QQBotManager.getInstance().sendMessage(false, qq, + new Image(item.getJSONObject("urls").getString("regular")), + new Reply(event.getMessageId()) + ); + } + } + ); + + return true; } @@ -236,9 +258,12 @@ public class GetSeTu extends Model { Matcher matcher = pattern.matcher(msg); MessageEvent event = new MessageEvent(); SourceFrom sourceFrom = new SourceFrom(); - sourceFrom.setUserId(583819556L); + sourceFrom.setUserId(891655174L); + event.setMessageType("private"); event.setSource(sourceFrom); + event.setGroupId(891655174L); + event.setMessageId(-1); event.setRawMessage(msg); - new GetSeTu().onMessage(583819556L, event, false); + new GetSeTu().onMessage(891655174L, event, true); } } diff --git a/src/main/java/com/yutou/qqbot/utlis/Base64Tools.java b/src/main/java/com/yutou/qqbot/utlis/Base64Tools.java index 14c32fc..c2fb37f 100644 --- a/src/main/java/com/yutou/qqbot/utlis/Base64Tools.java +++ b/src/main/java/com/yutou/qqbot/utlis/Base64Tools.java @@ -30,9 +30,5 @@ public class Base64Tools { } public static void main(String[] args) { - File file=new File("遥遥领先.mp3"); - - String base64 = encode(file); - System.out.println(base64); } }