從碼雲上看到的,地址:https://gitee.com/hotlcc/wechat4j
導入本地後寫一個主方法:
package com.hotlcc.wechat4j;
import com.alibaba.fastjson.JSONObject;
import com.hotlcc.wechat4j.handler.ReceivedMsgHandler;
import com.hotlcc.wechat4j.model.ReceivedMsg;
import com.hotlcc.wechat4j.model.UserInfo;
import com.hotlcc.wechat4j.util.StringUtil;
import cn.hutool.http.HttpUtil;
public class Demo {
public static void main(String[] args) {
// 實例化微信客戶端
Wechat wechat = new Wechat();
wechat.addReceivedMsgHandler(new ReceivedMsgHandler() {
@Override
public void handleAllType(Wechat wechat, ReceivedMsg msg) {
UserInfo contact = wechat.getContactByUserName(false, msg.getFromUserName());
String name = StringUtil.isEmpty(contact.getRemarkName()) ? contact.getNickName()
: contact.getRemarkName();
System.out.println(name + ": " + msg.getContent());
String returnMsg = HttpUtil.get(
"http://i.itpk.cn/api.php?api_key=50c6721e14b6857c6548a23ad8&api_secret=f6gs0k2&question="
+ msg.getContent());
JSONObject result = wechat.sendTextToUserName(contact.getUserName(), returnMsg);
System.out.println(result);
}
});
// 自動登錄
wechat.autoLogin();
// 配置消息處理器
}
}
直接允許main方法就可以了。具體可以看碼雲上的文檔