213 lines
6.6 KiB
Java
213 lines
6.6 KiB
Java
package com.yutou.bilibili.QQBot;
|
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
import com.yutou.napcat.QQDatabase;
|
|
import com.yutou.napcat.QQNumberManager;
|
|
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;
|
|
import com.yutou.napcat.model.GroupBean;
|
|
import com.yutou.napcat.model.SendMessageResponse;
|
|
import com.yutou.okhttp.HttpBody;
|
|
import com.yutou.okhttp.HttpCallback;
|
|
import com.yutou.utils.ConfigTools;
|
|
import okhttp3.Headers;
|
|
import retrofit2.Response;
|
|
|
|
|
|
import java.io.File;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
|
|
public class QQBotManager {
|
|
|
|
public static Long defGroup = 891655174L;
|
|
public static Long defQQ = 583819556L;
|
|
|
|
|
|
private static QQBotManager botManager = null;
|
|
private static final long qqGroup = 891655174L;
|
|
private boolean isLogin = false;
|
|
private static boolean isInit = false;
|
|
|
|
|
|
private QQBotManager() {
|
|
Boolean isRun = ConfigTools.load(ConfigTools.CONFIG, "qq_bot", Boolean.class);
|
|
if (isRun != null && isRun) {
|
|
isLogin = true;
|
|
init();
|
|
}
|
|
}
|
|
|
|
private void init() {
|
|
NapCatApi.getGroupApi().getGroupList().enqueue(new HttpCallback<List<GroupBean>>() {
|
|
@Override
|
|
public void onResponse(Headers headers,int code, String status, List<GroupBean> response, String rawResponse) {
|
|
for (GroupBean groupBean : response) {
|
|
QQDatabase.addGroup(groupBean.getGroupId(), groupBean);
|
|
QQNumberManager.getManager().addNumber(groupBean.getGroupId(), true);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(Throwable throwable) {
|
|
|
|
}
|
|
});
|
|
NapCatApi.getFriendApi().getFriendList().enqueue(new HttpCallback<List<FriendBean>>() {
|
|
@Override
|
|
public void onResponse(Headers headers,int code, String status, List<FriendBean> response, String rawResponse) {
|
|
for (FriendBean friendBean : response) {
|
|
QQDatabase.addUser(friendBean.getUserId(), friendBean);
|
|
QQNumberManager.getManager().addNumber(friendBean.getUserId(), false);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(Throwable throwable) {
|
|
|
|
}
|
|
});
|
|
NapCatApi.getUtilsApi().getLoginInfo().enqueue(new HttpCallback<FriendBean>() {
|
|
@Override
|
|
public void onResponse(Headers headers,int code, String status, FriendBean response, String rawResponse) {
|
|
QQDatabase.setMe(response);
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(Throwable throwable) {
|
|
|
|
}
|
|
});
|
|
isInit = true;
|
|
}
|
|
|
|
public synchronized static QQBotManager getInstance() {
|
|
if (botManager == null && !isInit) {
|
|
botManager = new QQBotManager();
|
|
}
|
|
return botManager;
|
|
}
|
|
|
|
|
|
private String getNotLoginQQ() {
|
|
return "没有登录QQ";
|
|
}
|
|
|
|
public SendMessageResponse sendPrivateMessage(Long qq, BaseHandle<?>... items) {
|
|
return sendMessage(true, qq, items);
|
|
}
|
|
|
|
private SendMessageResponse sendGroupMessage(Long group, BaseHandle<?>... items) {
|
|
return sendMessage(false, group, items);
|
|
}
|
|
|
|
public SendMessageResponse sendMessage(boolean user, Long qq, String msg) {
|
|
return sendMessage(user, qq, new Text(msg));
|
|
}
|
|
|
|
public SendMessageResponse sendMessage(Long qq, BaseHandle<?>... items) {
|
|
return sendMessage(QQDatabase.checkFriend(qq), qq, Arrays.asList(items));
|
|
}
|
|
|
|
public SendMessageResponse sendMessage(boolean isUser, Long qq, BaseHandle<?>... items) {
|
|
return sendMessage(isUser, qq, Arrays.asList(items));
|
|
}
|
|
|
|
public SendMessageResponse sendMessage(boolean isUser, Long qq, List<BaseHandle<?>> items) {
|
|
try {
|
|
if(!ConfigTools.load(ConfigTools.CONFIG,ConfigTools.QQ, Boolean.class)){
|
|
return null;
|
|
}
|
|
MessageHandleBuild handleBuild = MessageHandleBuild
|
|
.create()
|
|
.setGroup(!isUser)
|
|
.setQQNumber(qq);
|
|
for (BaseHandle<?> item : items) {
|
|
if (item.getData() == null) {
|
|
continue;
|
|
}
|
|
if (item instanceof Reply) {
|
|
if (((Reply) item).getData().getId() == -1) {
|
|
continue;
|
|
}
|
|
}
|
|
handleBuild.add(item);
|
|
}
|
|
Response<HttpBody<SendMessageResponse>> response;
|
|
if (isUser) {
|
|
response = NapCatApi.getMessageApi().sendPrivateMsg(
|
|
handleBuild.build()
|
|
).execute();
|
|
} else {
|
|
response = NapCatApi.getMessageApi().sendGroupMsg(
|
|
handleBuild.build()
|
|
).execute();
|
|
}
|
|
if (response.body() != null) {
|
|
return response.body().getData();
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
SendMessageResponse response = new SendMessageResponse();
|
|
response.setId(-1);
|
|
response.setE(e.getMessage());
|
|
return response;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public String sendMessage(String text) {
|
|
|
|
return getNotLoginQQ();
|
|
}
|
|
|
|
public SendMessageResponse sendMessage(Long group, String text) {
|
|
return sendMessage(QQDatabase.checkFriend(group), group, new Text(text));
|
|
}
|
|
|
|
public void sendMessage(Long group, StringBuilder builder) {
|
|
if (QQNumberManager.getManager().isGroup(group)) {
|
|
System.out.println("发群");
|
|
} else {
|
|
System.out.println("发个人");
|
|
}
|
|
}
|
|
|
|
public void sendMessage(File imageFile, Long qq, String text) {
|
|
sendMessage(imageFile, qq, null, text);
|
|
}
|
|
|
|
public void sendMessage(File imageFile, Long qq, String replyMessageId, String text) {
|
|
|
|
}
|
|
|
|
public void sendMessage(File imageFile, String text) {
|
|
}
|
|
|
|
public void sendMessage(List<File> imgs, Long qq, String text) {
|
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) {
|
|
JSONObject json = new JSONObject();
|
|
json.put("t1", 3234567890L);
|
|
System.out.println("json = " + json);
|
|
|
|
String tmp = json.toString();
|
|
JSONObject json2 = JSONObject.parseObject(tmp);
|
|
System.out.println("json2 = " + json2);
|
|
}
|
|
|
|
|
|
public boolean isLogin() {
|
|
return true;
|
|
}
|
|
|
|
|
|
}
|