Module: WechatPublicApi::Tp
- Included in:
- WechatPublicApi
- Defined in:
- lib/wechat_public_api/templet_message.rb
Instance Method Summary collapse
-
#post_template_message(message, url_params) ⇒ Object
发送模板消息接口.
-
#tp_delete_template(templet_id) ⇒ Object
删除模板.
-
#tp_general_message(openid, templet_id, url, data) ⇒ Object
普通的模板消息,不跳转小程序.
-
#tp_get_all ⇒ Object
获得模板列表.
-
#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” } } 小程序模板消息.
Instance Method Details
#post_template_message(message, url_params) ⇒ Object
发送模板消息接口
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 (, url_params) # get access_token access_token = get_access_token() @access_token = Wxutils.get_access_token wx_account 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 = .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) = { 'template_id': template_id } , '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 (openid, templet_id, url, data) = { 'touser': openid, 'template_id': templet_id, 'url': url, 'data': data } , 'send' end |
#tp_get_all ⇒ Object
获得模板列表
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"
}
}
小程序模板消息
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 (openid, templet_id, url, appid, pagepath, data) = { 'touser': openid, 'template_id': templet_id, 'url': url, "miniprogram":{ "appid": appid, "pagepath": pagepath }, 'data': data } , 'send' end |