zoukankan      html  css  js  c++  java
  • 微信公众平台模拟群发技术

    文前 。raspberry FQ路由器文章已跳票。因为准备在gitshell上面先发,带markdown。

    微信公共平台 最近比较火。

    它自身有简单的自动回复功能(判断条件成立则发送相应的文字、素材图像、图文等)。

    感觉有点搞头,于是去找api。官方的不可能有。最简单的方法就是用firebug。

    http request 和 response 都取下来。慢慢看,看懂就不用看下文了,看不懂的也不用看下文了,好吧我又纠结了,那还写个毛线啊。

    首先是,cookies的处理。直接把firebug里面的cookies字符串以header发过去qq服务器就可以了。
    $f = new SaeFetchurl();
    $f->setMethod("get");
    $f->setHeader("Cookie"," YOUR COOKIE STRING ");

    • 取消息timeline:
      http://mp.weixin.qq.com/cgi-bin/getmessage?t=wxm-message&lang=zh_CN&count=50&timeline=1&day=0
      得到的html从

       

      DATA.List.msgList

      })(WXM, window)

      截取下来,就是timeline的json数据了!!!

      单条消息item解析:(一大堆有的没有的,有用的如下)
      item["type"] 1为文本信息 // 3为音频对讲
      item["fakeId"] 不知道含义,但是回复消息的时候就用这个id就行!
      item["content"] 发过来的消息的文本
      item["id"] – 取某条音频消息的mp3数据时候有用

    • 发消息:http://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response&lang=zh_CN

       

      $f = new SaeFetchurl();
      $f->setMethod("post");
      $f->setHeader("Cookie"," YOUR COOKIE STRING ");
      $f->setPostData(
      array(
      "ajax"=> "true" ,
      "content" => "要发送给用户的消息正文" ,
      "error" => "false",
      "tofakeid"=>"用户的fakeid(在上面的接口的json数据里面有)",
      "type"=>"1",
      )
      );
      $ret=$f->fetch("http://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response&lang=zh_CN");
      if($f->errno() == 0) echo $ret;
      else echo $f->errmsg();

       

    • 取消息音频(mp3格式,额,严格来说是mp2.5….)

      $f->fetch("http://mp.weixin.qq.com/cgi-bin/getvoicedata?uin=*******&skey=******&msgid=".消息id(见上文)."&fileid=0")

      直接取,得到的数据直接可以存成mp3文件,不知道为何微信为什么要这么做。。。

    下篇将会介绍如何实现机器人和用户在微信公共平台直接用语音来聊天:

    科大讯飞语音云SDK(听写、识别)(把linux SDK做成服务器,json REST) +
    微信公共平台(本文提到api) +
    Aiml人工智能聊天(中文支持处理)

     

     

    今天初来乍到cnode.js,也应该贡献贡献.看到微信公众平台,开始有点兴奋,能做个机器人玩玩,,随后用Node.js写了一个,觉得其实这没什么意思.很快就觉得腻了,于是有了做发送微信接口的想法.首先要做的我们就要模拟公众平台的登陆.对于微信的这些lib,当然不能直接写在routes里面,,那要怎么办呢?没错,就要封装起来,方便复用.你可以打开控制台看到公众平台的登录请求,还有所需的参数,其中密码它是用它本身的md5进行加密的,那么我们需要做的只是将它copy过来放在一个helpers/wx/md5.js文件里就可以直接用了,以下是微信公众平台解析后格式化的js提交代码

    submit:function(){if(!n())return;var e = d.getVal();
             t.post("/cgi-bin/login?lang=zh_CN",{
                username: e.account,
                pwd1: t.md5(e.password.substr(0,15)),
                pwd2: t.md5(e.password),
                imgcode: f.data("isHide")?"": e.verify,register: e.isRegister,
                f:"json"},

    我们要建立一个login的方法

    request =require'superagent'require __basename +'/helpers/wx/md5'
    config =require __basename +'/config/config'module.exports = 
      login:(fn)->
        wx_usr = config.wx.user
        wx_pwd = md5 config.wx.pwd.substr(0,16)
        request
          .post('http://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN').type('form').send(
            username: wx_usr
            pwd: wx_pwd
            imgcode :''
            f :'json'register:0).end(res)->//在这里你已经成功获取cookie了

    但是经过分析我想你会发现,这里的cookie其实并非你想要的cookie,因为它包含一些没用的信息Path=,我们设置cookie的时候,事实上是不能用直接设置这样的cookie,应该是一个cookie里面不应该有其他的东西,而分号后面的path应该将它去掉,这里是返回的结果:

    ["mp_user=xxxxxx; Path=/","mp_sid=NlJ2Tm5hb1NXRGxOU3V1MzF2a25tSFVWRHhTNkhwek1nMXlEOVZzMnZMUG1lZ29nSkdENGt3WlgwUjBJZnhydndYNkZSd0ZsaHRHdEozSHBIa3QwT3FWTmdXc3RxVFhYUDBCR3dnWkxIRWVvRlZObG15UC83SzU1aEZPZWpocU8=; Path=/"]

    以下是完整的login代码:

    login:(fn)->
        wx_usr = config.wx.user
        wx_pwd = md5 config.wx.pwd
        request
          .post('http://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN').type('form').send(
            username: wx_usr
            pwd1: wx_pwd
            pwd2: wx_pwd
            imgcode :''
            f :'json').end(res)->
            cookie =''for rs in res.header['set-cookie']
              cookie += rs.replace(/Path=\//g,'')
            fn null, cookie

    在这里,我们已经完成登录的操作了,接下来,我们要做的是进行发送,在发送的时候,要把这个cookie设置在请求的地址中,接下来的代码比较简单:(注意:这里面的fakeid是微信公众平台的id,我们可以用控制台去微信公众平台的用户管理中查看,如何获取用户好友的fakeid,接下来一章我会讲)

    sender:(options, fn)->
        msg = options.msg
        fakeid = options.fakeid
    
        unless msg
          fn error:'missing msg'returnunless fakeid
          fn error:'missing fakeid'return
    
        psotParams =
          type:1
          content: msg
          error:false
          tofakeid : fakeid
          ajax :1
    
        request
          .post('http://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response&lang=zh_CN').type('form').send(psotParams).set('Cookie', options.cookie).end(res)->
            fn null, JSON.parse res.text

    这里,我们已经能完全发送了,因为返回的结果是一个json,所要最好先JSON.parse一下,里面的成功判断大家可以加上,返回的接口有个叫ret的参数,0为发送成功

    {
    ret:"0",
    msg:"ok"}

    下一章,我会为大家深入讲解怎么发送信息给好友,及获取微信头像等等技术

     

    sender目前只能实现文字text的发送,但这并不完善,也可以实现图文的发送,但是却必须要上传一张封面图片,上传后需要在发送图文信息的时候把返回结果中的formId拿到,http://mp.weixin.qq.com/cgi-bin/uploadmaterial?cgi=uploadmaterial&type=2&t=iframe-uploadfile&lang=zh_CN&formId=1其中的参数type是上传的类型,语音和视频是0,formId为null.上传封面图片接口如下:

    uploadmaterial:(fn)->@login(err, cookie)->
          request
            .post('http://mp.weixin.qq.com/cgi-bin/uploadmaterial?cgi=uploadmaterial&type=2&t=iframe-uploadfile&lang=zh_CN&formId=1').type('form').set('Cookie', cookie).end(res)->
              results = JSON.parse(res.text).match(/formId, '(\d+)'/)[2]
              fn null, results

    成功后,可以进行图文消息的发送了.

      send_appmsg:(options, fn)->@login(err, cookie)->
          psotParams =
            error       :false
            count       :1AppMsgId:null
            title0      : options.title
            digest0     :'正文内容'# content0    : '<p>te<img src="http://www.e-research-solutions.com/system/cms/themes/default/img/top_l.png" /></p><p><span style="color:red">测试标题</span></p>'
            content0    : options.msg
            fileid0     :'10000001'#此处的id即为封面图片的id
            preusername : options.username
            ajax        :1
          request
            .post('http://mp.weixin.qq.com/cgi-bin/operate_appmsg?sub=preview&t=ajax-appmsg-preview').type('form').set('Cookie', cookie).send(psotParams).end(res)->
              results = JSON.parse res.text
              fn null, results['msg']
  • 相关阅读:
    Java中Collection和Collections的区别
    网站
    window.load 和$(document).ready() 、window.load和body onload区别
    『jQuery』.html(),.text()和.val()的使用
    jQuery选择器总结
    ios开发--编码格式
    iOS开发--基于AFNetWorking3.0的图片缓存分析
    iOS开发--沙盒路径与操作文件
    ios开发--第三方整理
    iOS 网络处理注意点
  • 原文地址:https://www.cnblogs.com/pondbay/p/3486486.html
Copyright © 2011-2022 走看看