golang實現微信聊天機器人

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

主要模組

  • 擷取UUID
  • 根據UUID擷取二維碼
  • 顯示二維碼
  • 掃碼登陸
  • 初始化資訊
  • 開啟狀態同步通知
  • 擷取通訊錄
  • 發送資訊
  • 同步資訊
  • 擷取自動回複內容

源碼地址

https://github.com/qianlnk/gobot

網頁版API

擷取UUID

  • 介面地址
    https://login.weixin.qq.com/jslogin?appid=wx782c26e4c19acffb&fun=new&lang=zh_CN&_={Timestamp}

  • 方法
    GET

  • 結果

window.QRLogin.code = 200; window.QRLogin.uuid = "xxx";
  • 實現
    參考GetUUID方法

擷取二維碼

  • 介面地址
    https://login.weixin.qq.com/qrcode/{UUID}?t=webwx&_={Timestamp}

  • 方法
    GET

  • 實現
    參考GenQrcode方法

  • 將二維碼輸出到終端
    https://github.com/qianlnk/qrcode

登入

  • 介面地址
    https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?tip={1,0}&uuid=(UUID}&_={Timestamp}

tip: 1 未掃碼 0 已掃碼

  • 方法
    GET

  • 結果

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
  • 實現
    參考Login方法

  • 跳轉
    擷取使用者的登入資訊

    Skey       string `xml:"skey"`    Wxsid      string `xml:"wxsid"`    Wxuin      string `xml:"wxuin"`    PassTicket string `xml:"pass_ticket"`

初始化

  • 介面地址
    BASE_URL/webwxinit?pass_ticket={PassTicket}&skey={Skey}&r={Timestamp}

  • 方法
    POST

  • 結果

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"`}
  • 實現
    參考Init方法

狀態通知

  • 介面地址
    BASE_URL/webwxstatusnotify?lang=zh_CN&pass_ticket={PassTicket}

  • 參數

    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())
  • 方法
    POST

-實現
參考StatusNotify方法

擷取通訊錄

  • 介面地址
    BASE_URL/webwxgetcontact?sid={Wxsid}&skey={Skey}&pass_ticket={PassTicket}

  • 參數

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

-方法
POST

  • 實現
    參考GetContact方法

同步資訊

  • 介面地址
    https://host/cgi-bin/mmwebwx-bin/synccheck

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())
  • 方法
    GET

  • 結果

window.synccheck={retcode:"xxx",selector:"xxx"}retcode:    0 正常    1100 手機上退出網頁版    1101在其他地方登入網頁版selector:    0 正常    2 新的訊息    7 進入/離開聊天介面
  • 實現
    參考SyncCheck方法

當selector=2時讀取新的資訊

  • 介面地址
    BASE_URL/webwxsync?sid={Wxsid}&skey={Skey}&pass_ticket={PassTicket}

  • 參數

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

  • 注意

修改synckey,重要

    if msg.BaseResponse.Ret == 0 {        w.syncKey = msg.SyncKey    }
  • 實現
    參考WxSync方法

發送資訊

  • 介面地址
    BASE_URL/webwxsendmsg?pass_ticket={PassTicket}

  • 參數

    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
  • 方法
    POST

  • 實現
    參考SendMessage方法

圖靈API

擷取自動回複內容

  • 介面地址
    http://www.tuling123.com/openapi/api

  • 去圖靈官網註冊並擷取key
    http://www.tuling123.com

  • 參數

    params := make(map[string]interface{})    params["userid"] = uid    params["key"] = w.cfg.Tuling.Keys[w.user.NickName].Key    params["info"] = msg
  • 方法
    POST

  • 結果

    Code int         `json:"code"`    Text string      `json:"text"` //100000    URL  string      `json:"url"`  //200000    List interface{} `json:"list"` //302000 []News 308000 []Menu
  • 實現
    參考getReply方法

run.png

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.