Golang语言社区互联网科技Go语言

golang实现微信聊天机器人

2018-01-03  本文已影响1204人  qianlnk

主要模块

源码地址

https://github.com/qianlnk/gobot

网页版微信API

获取UUID

window.QRLogin.code = 200; window.QRLogin.uuid = "xxx";

获取二维码

登录

tip: 1 未扫码 0 已扫码

window.code=xxx;

xxx:
    408 登陆超时
    201 扫描成功
    200 确认登录

当返回200时,还会有
window.redirect_uri="https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage?ticket=xxx&uuid=xxx&lang=xxx&scan=xxx";

通过正则获取后面需要的URL, 后面统一用BASE_URL表示。

BASE_URL = https://wx.qq.com
    Skey       string `xml:"skey"`
    Wxsid      string `xml:"wxsid"`
    Wxuin      string `xml:"wxuin"`
    PassTicket string `xml:"pass_ticket"`

微信初始化

type InitResult struct {
    BaseResponse        BaseResponse     `json:"BaseResponse"`
    Count               int              `json:"Count"`
    ContactList         []Contact        `json:"ContactList"`
    SyncKey             SyncKey          `json:"SyncKey"`
    User                User             `json:"User"`
    ChatSet             string           `json:"ChatSet"`
    SKey                string           `json:"SKey"`
    ClientVersion       int              `json:"ClientVersion"`
    SystemTime          int              `json:"SystemTime"`
    GrayScale           int              `json:"GrayScale"`
    InviteStartCount    int              `json:"InviteStartCount"`
    MPSubscribeMsgCount int              `json:"MPSubscribeMsgCount"`
    MPSubscribeMsgList  []MPSubscribeMsg `json:"MPSubscribeMsgList"`
    ClickReportInterval int              `json:"ClickReportInterval"`
}

状态通知

    params := make(map[string]interface{})
    params["BaseRequest"] = w.baseRequest
    params["Code"] = 3
    params["FromUserName"] = w.user.UserName
    params["ToUserName"] = w.user.UserName
    params["ClientMsgId"] = int(time.Now().Unix())

-实现
参考StatusNotify方法

获取通讯录

    params := make(map[string]interface{})
    params["BaseRequest"] = w.baseRequest

-方法
POST

同步信息

host:

    Hosts = []string{
        "webpush.wx.qq.com",
        "webpush2.wx.qq.com",
        "webpush.wechat.com",
        "webpush1.wechat.com",
        "webpush2.wechat.com",
        "webpush1.wechatapp.com",
    }

-参数

        v := url.Values{}
        v.Add("r", w.timestamp())
        v.Add("sid", w.loginRes.Wxsid)
        v.Add("uin", w.loginRes.Wxuin)
        v.Add("skey", w.loginRes.Skey)
        v.Add("deviceid", w.deviceID)
        v.Add("synckey", w.strSyncKey())
        v.Add("_", w.timestamp())
window.synccheck={retcode:"xxx",selector:"xxx"}

retcode:
    0 正常
    1100 手机上退出网页版微信
    1101在其他地方登录网页版微信
selector:
    0 正常
    2 新的消息
    7 进入/离开聊天界面

selector=2时读取新的信息

    params := make(map[string]interface{})
    params["BaseRequest"] = w.baseRequest
    params["SyncKey"] = w.syncKey
    params["rr"] = ^int(time.Now().Unix())

修改synckey,重要

    if msg.BaseResponse.Ret == 0 {
        w.syncKey = msg.SyncKey
    }

发送信息

    params := make(map[string]interface{})
    params["BaseRequest"] = w.baseRequest
    msg := make(map[string]interface{})
    msg["Type"] = 1
    msg["Content"] = message
    msg["FromUserName"] = w.user.UserName
    msg["ToUserName"] = to
    msg["LocalID"] = clientMsgID
    msg["ClientMsgId"] = clientMsgID
    params["Msg"] = msg

图灵API

获取自动回复内容

    params := make(map[string]interface{})
    params["userid"] = uid
    params["key"] = w.cfg.Tuling.Keys[w.user.NickName].Key
    params["info"] = msg
    Code int         `json:"code"`
    Text string      `json:"text"` //100000
    URL  string      `json:"url"`  //200000
    List interface{} `json:"list"` //302000 []News 308000 []Menu

效果图

run.png
上一篇下一篇

猜你喜欢

热点阅读