一、gitHub克隆代码:
Simple-robot Demo
二、配置QQ账号密码
三、编写私聊监控
package love.simbot.example.listener;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import love.forte.common.ioc.annotation.Beans;
import love.forte.simbot.annotation.OnPrivate;
import love.forte.simbot.api.message.events.PrivateMsg;
import love.forte.simbot.api.sender.Sender;
import love.simbot.example.entity.Xmeta;
import java.util.*;
@Beans
public class Test {
@OnPrivate
public void fudu(PrivateMsg msg, Sender sender){
String text = msg.getText();
if((text.charAt(0)=='x'&&text.charAt(1)=='m')||(text.substring(0,2).equals("查询"))){
String name = text.substring(2);
sender.sendPrivateMsg(msg, query(name));
}
}
public static String query(String name){
Map<String, String > heads = new HashMap<>();
heads.put("Content-Type", "application/json;charset=UTF-8");
heads.put("Authorization", "##这里输入token");
JSONObject jsonObject = new JSONObject();
HttpResponse response = HttpRequest.post();
String body = response.body();
com.alibaba.fastjson.JSONObject res = JSON.parseObject(body);
List<Xmeta> msgList = JSON.parseArray(res.getString("data"), Xmeta.class);
Collections.sort(msgList, new Comparator<Xmeta>() {
public int compare(Xmeta o1, Xmeta o2) {
return o1.getAmount().compareTo(o2.getAmount());
}
});
String end = "价格列表:(升序)\n";
System.out.println(end.length());
for (int i = 0; i < msgList.size(); i++) {
end = end+("【"+(i+1)+ "、"+msgList.get(i).getName()+"-¥"+msgList.get(i).getAmount()+"】\n");
}
if(end.length()<20){
return "未查询到结果";
}
return end+"xm查询裙:966286230";
}
}
查询格式:xm+名字