Module: WechatPublicApi::Kf
- Included in:
- WechatPublicApi
- Defined in:
- lib/wechat_public_api/kf_message.rb
Instance Method Summary collapse
-
#kf_image_message(openid, media_id, kf_account = nil) ⇒ Object
发送图片消息.
-
#kf_mpnews_message(openid, media_id, kf_account = nil) ⇒ Object
发送图文消息(点击跳转到图文消息页面).
-
#kf_news_message(openid, articles, kf_account = nil) ⇒ Object
> example articles = [ { “title”:“Happy Day”, “description”:“Is Really A Happy Day”, “url”:“URL”, “picurl”:“PIC_URL” }, { “title”:“Happy Day”, “description”:“Is Really A Happy Day”, “url”:“URL”, “picurl”:“PIC_URL” } ].
-
#kf_text_message(openid, content, kf_account = nil) ⇒ Object
发送文字消息.
-
#kf_voice_message(openid, media_id, kf_account = nil) ⇒ Object
发送语音消息.
-
#post_customer_message(message) ⇒ Object
execute post => example message = { touser: openid, msgtype: ‘text’, text: content, customservice:{ kf_account: ‘xxxxxxxx’ } }.
Instance Method Details
#kf_image_message(openid, media_id, kf_account = nil) ⇒ Object
发送图片消息
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/wechat_public_api/kf_message.rb', line 64 def (openid, media_id, kf_account=nil) = { touser: openid, msgtype: 'image', image: {media_id: media_id} } if kf_account .merge({customservice:{account: kf_account}}) end end |
#kf_mpnews_message(openid, media_id, kf_account = nil) ⇒ Object
发送图文消息(点击跳转到图文消息页面)
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/wechat_public_api/kf_message.rb', line 80 def (openid, media_id, kf_account=nil) = { touser: openid, msgtype: 'mpnews', mpnews: {media_id: media_id} } if kf_account .merge({customservice:{account: kf_account}}) end end |
#kf_news_message(openid, articles, kf_account = nil) ⇒ Object
> example
articles = [
{
"title":"Happy Day",
"description":"Is Really A Happy Day",
"url":"URL",
"picurl":"PIC_URL"
},
{
"title":"Happy Day",
"description":"Is Really A Happy Day",
"url":"URL",
"picurl":"PIC_URL"
}
]
发送图文消息(点击跳转到外链)
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/wechat_public_api/kf_message.rb', line 112 def (openid, articles, kf_account=nil) = { touser: openid, msgtype: 'news', news: { articles: articles } } if kf_account .merge({customservice:{account: kf_account}}) end end |
#kf_text_message(openid, content, kf_account = nil) ⇒ Object
发送文字消息
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/wechat_public_api/kf_message.rb', line 45 def (openid, content, kf_account=nil) = { touser: openid, msgtype: 'text', text: {content: content} } if kf_account .merge({customservice:{account: kf_account}}) end end |
#kf_voice_message(openid, media_id, kf_account = nil) ⇒ Object
发送语音消息
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/wechat_public_api/kf_message.rb', line 128 def (openid, media_id, kf_account=nil) = { touser: openid, msgtype: 'voice', voice: { media_id: media_id } } if kf_account .merge({customservice:{account: kf_account}}) end end |
#post_customer_message(message) ⇒ Object
execute post
> example
message =
touser: openid,
msgtype: 'text',
text: {content: content,
customservice:{
kf_account: 'xxxxxxxx'
}
}
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/wechat_public_api/kf_message.rb', line 25 def () # get access_token access_token = get_access_token() uri = URI.parse("https://api.weixin.qq.com/cgi-bin/message/custom/send?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/custom/send?access_token=#{access_token}") request.add_field('Content-Type', 'application/json') 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 |