微信机器人

后退,我要开始变身了...

之前在微博上看到网友分享微信网页版协议做的一个机器人的程序,觉得挺有意思,就参照分享作者整理的协议及源码自己也做了个微信机器人

效果如图:

程序下载地址:

文件名称:F:\wxRobot\wxRobot.zip
文件大小:64272819 字节
修改时间:2016年6月10日 16:47:12
MD5 :EDF879C9EB5B1FAD9677C8075C0D3791
SHA1 :839CB1CA2EEDACC7F3DA86D77DB2A803F52E7683
CRC32 :DAB4B517

链接:https://pan.baidu.com/s/1jHCEbcU 密码:bjxh

 

把自己账号变为机器人的主要思路就是在收到想要自动回复的用户信息时,将该信息转发给机器人(也就是小冰),再将回收到的消息转发给原目标用户。

 1 public static void main(String[] args) {
 2 
 3     WxRobotApp wxRobotApp = new WxRobotApp();
 4 
 5     wxRobotApp.putThing("deviceId", "e" + System.currentTimeMillis());
 6 
 7     wxRobotApp.showFrame();
 8 
 9     // 1、2、3步骤合并,获取到最新可用二维码
10     wxRobotApp.prepareQRCode();
11 
12     // 4.确认后登陆,获取后续请求必要参数
13     Map<String, Object> cookieResult = WxFun.getCookie(wxRobotApp.getThing("redirect").toString());
14     Map<String, Object> baseRequest = new HashMap<>();
15     Map<String, Object> bodyInnerParams = new HashMap<>();
16     bodyInnerParams.put("Uin", cookieResult.get("wxuin"));
17     bodyInnerParams.put("Sid", cookieResult.get("wxsid"));
18     bodyInnerParams.put("Skey", cookieResult.get("skey"));
19     bodyInnerParams.put("DeviceId", wxRobotApp.getThing("deviceId"));
20     baseRequest.put("BaseRequest", bodyInnerParams);
21     wxRobotApp.putThing("baseRequest", baseRequest);
22     wxRobotApp.putThing("pass_ticket", cookieResult.get("pass_ticket"));
23     // 5.初始化微信
24     Map<String, Object> initData = WxFun.init((Map<String, Object>) wxRobotApp.getThing("baseRequest"));
25     Map user = (Map) initData.get("User");
26     Map syncKey = (Map) initData.get("SyncKey");
27     wxRobotApp.putThing("user", user);
28     wxRobotApp.putThing("userId", user.get("UserName"));
29     wxRobotApp.putThing("syncKey", syncKey);
30     logger.debug("[欢迎] " + user.get("NickName").toString());
31     wxRobotApp.putThing("curUserName", user.get("NickName").toString());
32     // 6 done 获取联系人,并将联系人信息填入联系人列表
33     Vector vector = WxFun.getContactList((Map<String, Object>) wxRobotApp.getThing("baseRequest"));
34     wxRobotApp.initContactList(vector);
35     for (Object uStr : vector) {
36         String[] uInfo = uStr.toString().split(splitFlag);
37         wxRobotApp.putThing("u_" + uInfo[1], uInfo[0]);
38         if (uInfo[0].equals(xiaoIceName)) {
39             xiaoIceId = uInfo[1];
40         }
41     }
42     wxRobotApp.putThing("u_" + user.get("UserName").toString(), user.get("NickName").toString());
43     // 7 启动消息监听
44     wxRobotApp.messageListener();
45 }

主要代码在启动的main方法中都有体现

1.获取微信网页版登陆的二维码
wxRobotApp.prepareQRCode();
这里采取了超时重试的策略,在二维码请求30秒过期后重新生成一个新的二维码

2.确认登陆后,微信会返回一个redirect url跳转,将需要的参数进行填充后,发送请求到这个跳转url,并同时记录下跳转后返回的cookie,以在后续请求中带上这个cookie值
public class WxLoginCookieJar implements CookieJar {

    // 只保存此地址返回的cookie,用作后续请求的cookie
    private String loginUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage";

    private List<Cookie> cookies;

    @Override
    public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
        if (url.url().toString().contains(loginUrl)) {
            this.cookies = cookies;
        }
    }

    @Override
    public List<Cookie> loadForRequest(HttpUrl url) {
        return cookies != null ? cookies : new ArrayList<>();
    }
}
这里对cookie的处理是,自定义了一个okhttp对cookie处理接口的实现类,在匹配到指定url后保存这个url返回的cookie,并在后续请求中带上这个cookie

3.根据协议带上相关参数请求初始化微信的url

4.根据获取联系人接口获取到账号中的联系人

5.开启监听新消息线程

6.实现发送微信消息的接口

源码地址在:https://git.oschina.net/xiayudashan/wxRobot.git

 

posted @ 2016-06-11 21:18  多彩泰坦  阅读(2935)  评论(17编辑  收藏  举报