Module: WechatGate::SendMessage

Included in:
Config
Defined in:
lib/wechat_gate/send_message.rb

Instance Method Summary collapse

Instance Method Details

#mass_send(open_ids, msg_options = {}) ⇒ Object

mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN 这个接口有发送限制,服务号每月只能发送4次,订阅号每天一次



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/wechat_gate/send_message.rb', line 11

def mass_send open_ids, msg_options = {}
  payload = {
     "touser": [open_ids].flatten,
      "msgtype": "text",
      "text": { "content": "hello from boxer."}
  }.merge(msg_options)

  WechatGate::Request.send(
    "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=#{self.access_token}",
    :post,
    payload.to_json
  )
end

#single_send(open_id, content) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/wechat_gate/send_message.rb', line 67

def single_send open_id, content
  single_send_if_need_refresh_cookie

  opts = {
    method: :post,
    url: "https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response&f=json&token=#{self.single_send_token}&lang=zh_CN",
    verify_ssl: false,
    payload: {
      token: self.single_send_token,
      lang: "zh_CN",
      f: "json",
      ajax: 1,
      random: Random.rand,
      type: 1, # 文本消息
      content: content,
      tofakeid: open_id,
      imgcode: ""
    }.to_query,
    headers: {
      'User-Agent': self.single_send_ua,
      'Cookie': self.single_send_cookies,
      'Referer': "https://mp.weixin.qq.com/cgi-bin/singlesendpage?t=message/send&action=index&tofakeid=#{open_id}&token=#{self.single_send_token}&lang=zh_CN"
    }
  }

  response = RestClient::Request.execute(opts)
  data = JSON.parse(response)
  raise response.to_s if data['errmsg'] and data['errmsg'] != 'ok'
  data
end