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