用java代码发送qq消息
时间: 2023-05-22 16:05:20 浏览: 256
您可以使用Java开发的QQ机器人框架来发送QQ消息,比如SmartQQBot。以下是一个示例代码:
```java
import cn.edu.hfut.dmic.webcollector.net.HttpRequester;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class QQBot {
private static final String URL_LOGIN = "https://s.web2.qq.com/api/getvfwebqq?ptwebqq=&clientid=53999199&psessionid=&t=0.1";
private static final String URL_QR = "https://ui.ptlogin2.qq.com/cgi-bin/login?daid=164&target=self&style=40&pt_disable_pwd=1&mibao_css=&appid=501004106&enable_qlogin=0&no_verifyimg=1&s_url=https%3A%2F%2Fqun.qq.com%2Fsid%2F1012031483%2Fcall&f_url=loginerroralert&strong_login=1&login_state=10&t=20131024001";
private static final String URL_CHECK = "https://ssl.ptlogin2.qq.com/check?pt_tea=2&uin=%s&appid=501004106&js_ver=10231&js_type=1&login_sig=&u1=https%3A%2F%2Fqun.qq.com%2Fsid%2F1012031483%2Fcall&r=0.9769361521181407";
private static final String URL_AUTH = "https://s.web2.qq.com/api/channel/login2";
private static final String URL_POLL = "https://d1.web2.qq.com/channel/poll2";
private String vfwebqq;
private String psessionid;
public void login(String uin, String password) throws IOException {
//Step 1: Get QR code
HttpRequester.getRequest(URL_QR);
//Step 2: Check verification code
String ptVerifysessionV1 = HttpRequester.getCookie("pt_verifysession_v1");
String checkUrl = String.format(URL_CHECK, uin);
String checkResult = HttpRequester.getRequest(checkUrl);
String verifyCode = checkResult.substring(checkResult.indexOf(",'") + 2, checkResult.lastIndexOf("'"));
String loginSig = HttpRequester.getCookie("pt_login_sig");
//Step 3: Login
Map<String, String> authRequest = new HashMap<>();
authRequest.put("ptwebqq", "");
authRequest.put("clientid", "53999199");
authRequest.put("psessionid", "null");
authRequest.put("status", "online");
Map<String, Object> loginRequest = new HashMap<>();
loginRequest.put("ptwebqq", "");
loginRequest.put("clientid", "53999199");
loginRequest.put("psessionid", "null");
loginRequest.put("status", "online");
Map<String, Object> login2Request = new HashMap<>();
login2Request.put("ptwebqq", "");
login2Request.put("clientid", "53999199");
login2Request.put("psessionid", "null");
login2Request.put("status", "online");
//get vfwebqq
String response1 = HttpRequester.getRequest(URL_LOGIN, true);
JSONObject obj1 = JSON.parseObject(response1);
vfwebqq = obj1.getJSONObject("result").getString("vfwebqq");
//get psessionid
String checkSigUrl = String.format("https://ssl.ptlogin2.qq.com/checksig?pt_tea=2&uin=%s&appid=501004106&js_ver=10243&js_type=1&login_sig=%s&u1=https%%3A%%2F%%2Fqun.qq.com%%2Fsid%%2F1012031483%%2Fcall&r=0.9972207127572275", uin, loginSig);
String result = HttpRequester.getRequest(checkSigUrl);
String skey = JSONPath.eval(JSON.parseObject(result), "$.paras.skey").toString();
String checkSig = JSONPath.eval(JSON.parseObject(result), "$.paras.sig").toString();
String token = "";
String url = URL_AUTH + "?vfwebqq=" + vfwebqq + "&t=" + System.currentTimeMillis() / 1000;
String[] uins = new String[]{uin};
authRequest.put("password", password);
authRequest.put("verifycode", verifyCode);
authRequest.put("pt_verifysession_v1", ptVerifysessionV1);
JSONObject obj = JSON.parseObject(HttpRequester.postRequest(url, JSON.toJSONString(authRequest), token, uins));
JSONObject obj2 = obj.getJSONObject("result");
loginRequest.put("ptwebqq", obj2.getString("psessionid"));
loginRequest.put("psessionid", obj2.getString("psessionid"));
String cookie = "clientid=53999199; " +
"psessionid=" + obj2.getString("psessionid") + "; " +
"skey=" + skey + "; " +
"uin=" + uin + "; " +
"ptwebqq=" + vfwebqq + ";";
login2Request.put("ptwebqq", vfwebqq);
login2Request.put("clientid", "53999199");
login2Request.put("psessionid", obj2.getString("psessionid"));
//get psessionid again
obj = JSON.parseObject(HttpRequester.postRequest(URL_AUTH, JSON.toJSONString(loginRequest), token, uins));
psessionid = obj.getJSONObject("result").getString("psessionid");
}
public void sendMessage(String toUin, String messageContent) throws Exception {
JSONObject r = new JSONObject();
r.put("from_uin", Integer.valueOf(HttpRequester.getCookie("uin").substring(1)));
r.put("to_uin", Long.valueOf(toUin));
r.put("face", 0);
r.put("content", JSONArray.parseJSONArray("[[\"font\",{\"name\":\"宋体\",\"size\":10,\"style\":[0,0,0],\"color\":\"000000\"}],[\"face\",1],\"" + messageContent + "\"]"));
r.put("msg_id", (int) (Math.random() * 0x7FFF + 0x8000));
r.put("clientid", 53999199);
r.put("psessionid", psessionid);
String[] uins = new String[]{String.valueOf(HttpRequester.getCookie("uin").substring(1))};
Package pack = new SendMessagePack(HttpRequester.getCookie("ptwebqq"), vfwebqq, psessionid, token);
HttpRequester.postRequest(URL_SEND_MSG, JSON.toJSONString(r), pack.getPack(), pack.getHeaders(), pack.getParams(), token, uins);
}
public static void main(String[] args) {
try {
QQBot bot = new QQBot();
bot.login("123456789", "mypassword");
bot.sendMessage("987654321", "Hello world!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
阅读全文