一、概要
本文主要介绍WhatsApp Java api的主要接口和使用方法。
二、基本流程。
1、设置全局IP,可以设置http代理,socks代理等
public static String buildHttpProxy(String ip,int port,String user,String pwd){
JSONObject json = new JSONObject();
json.put("type",Constant.ProxyType.HTTP);
JSONObject data = new JSONObject();
data.put("user",user);
data.put("pwd",pwd);
data.put("ip",ip);
data.put("port",port);
json.put("proxy",data.toJSONString());
return json.toJSONString();
}
2、设置账号
账号格式:号码,公钥,私钥
3、创建Task对象,指定发送方式http,socket。调用Task对象的send方法
public void execByHttp(IApi api,ICallback callback,String ip){
if(StringUtils.isEmpty(ip)){
ip = globalIp;
}
String id = UUID.randomUUID().toString().replace("-","");
Cmd cmd = new TaskExecCmd(id,api.buildType(),api.buildRaw(),ip).bind();
final String pid = cmd.getMessageBody().pid;
int error = sendCommandByHttp(cmd);
if(error!=0){
if(callback!=null){
callback.onError(api,error,"");
}
}else{
synchronized (https){
https.put(pid,Pair.of(api,callback));
}
}
}
4、等待数据返回。解析结果
JSONObject json = JSON.parseObject(raw);
int code = json.getIntValue("code");
String msg = json.getString("msg");
if(code!=0){
if(callback!=null){
int subCode = callback.onExecFail(api,code,msg);
callback.onError(api,subCode,code+" "+msg);
}
}else{
JSONObject data = json.getJSONObject("data");
if(callback!=null){
callback.onApiResult(api,data);
}
}
三、代理例子。
1、分析受众是否是whatsapp的有效账号
CheckFilter task = new CheckFilter(channel, Collections.singletonList("6285852321611"));
List<CheckFilter.Contact> contacts = task.send();
2、分析受众的最近一次上线时间
List<CheckPresence.Last> lasts = new CheckPresence(
channel,
Collections.singletonList("6285852321611")
).send();
3、分析受众的头像,判断性别和年龄
List<CheckFace.Face> faces = new CheckFace(
channel,
Collections.singletonList("6285852321611")
).send();
4、向受众发送消息
SendMessage.Message message = SendMessage.MessageBuilder.newBuilder().
setTarget("6285852321611").
setText("hi").
build();
SendMessage send = new SendMessage(channel, Arrays.asList(message));
List<SendMessage.Ack> acks=send.send();
5、设置账号的基本信息
boolean success = new ModifyStatus(channel,"this is a test").send();
四、其他例子。
1、创建群组。
2、添加群成员
3、群组禁言
4、移除群成员
5、群消息监听
6、私信自动回复
7、扫码登录网页端
8、语音电话呼出