Module: WechatPublicApi::Tp

Included in:
WechatPublicApi
Defined in:
lib/wechat_public_api/templet_message.rb

Instance Method Summary collapse

Instance Method Details

#post_template_message(message, url_params) ⇒ Object

发送模板消息接口

Parameters:

  • message (JSON)
  • url_params (string)

    – 消息路径



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/wechat_public_api/templet_message.rb', line 14

def post_template_message(message, url_params)
  # get access_token
  access_token = get_access_token()

  @access_token = Wxutils.get_access_token 
  uri = URI.parse("https://api.weixin.qq.com/cgi-bin/message/template/#{url_params}?access_token=#{@access_token}")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Post.new("/cgi-bin/message/template/#{url_params}?access_token=#{@access_token}")
  request.add_field('Content-Type', 'application/json')
  # 部分字符转换为json后  成为unicode编码
  request.body = message.to_json.gsub(/\\u([0-9a-z]{4})/) {|s| [$1.to_i(16)].pack("U")}
  response = http.request(request)
  JSON.parse(response.body)
end

#tp_delete_template(templet_id) ⇒ Object

删除模板



89
90
91
92
93
94
# File 'lib/wechat_public_api/templet_message.rb', line 89

def tp_delete_template(templet_id)
  message = {
    'template_id': template_id
  }
  post_template_message message, 'del_private_template'
end

#tp_general_message(openid, templet_id, url, data) ⇒ Object

普通的模板消息,不跳转小程序



78
79
80
81
82
83
84
85
86
# File 'lib/wechat_public_api/templet_message.rb', line 78

def tp_general_message(openid, templet_id, url, data)
  message = {
      'touser': openid,
      'template_id': templet_id,
      'url': url,
      'data': data
  }
  post_template_message message, 'send'
end

#tp_get_allObject

获得模板列表



97
98
99
100
101
102
# File 'lib/wechat_public_api/templet_message.rb', line 97

def tp_get_all()
  access_token = get_access_token()
  response = HTTParty.get("https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=#{access_token}").body
  response_body = (JSON.parse response)
  response_body
end

#tp_miniprogram_message(openid, templet_id, url, appid, pagepath, data) ⇒ Object

> data example

data = {

  "first": {
      "value":"恭喜你购买成功!",
      "color":"#173177"
  },
  "keyword1":{
      "value":"巧克力",
      "color":"#173177"
  },
  "keyword2": {
      "value":"39.8元",
      "color":"#173177"
  },
  "keyword3": {
      "value":"2014年9月22日",
      "color":"#173177"
  },
  "remark":{
      "value":"欢迎再次购买!",
      "color":"#173177"
  }
}

小程序模板消息

Parameters:

  • openid (string)

    – 用户的openid

  • templet_id (string)

    – 微信公众号后台提供的模板ID

  • url (string)

    – 模板跳转链接

  • appid (string)

    – 小程序的appid

  • pagepath (string)

    – 小程序页面路径 (eq: /index/index)

  • data (JSON)

    – 模板数据



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/wechat_public_api/templet_message.rb', line 62

def tp_miniprogram_message(openid, templet_id, url, appid, pagepath, data)
  message = {
      'touser': openid,
      'template_id': templet_id,
      'url': url,
      "miniprogram":{
         "appid": appid,
         "pagepath": pagepath
       },
      'data': data
  }

  post_template_message message, 'send'
end