wechat_public_api 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +6 -162
- data/lib/wechat_public_api/access_token.rb +33 -35
- data/lib/wechat_public_api/account.rb +89 -96
- data/lib/wechat_public_api/aes.rb +28 -31
- data/lib/wechat_public_api/kf_message.rb +117 -125
- data/lib/wechat_public_api/material.rb +36 -41
- data/lib/wechat_public_api/menu.rb +58 -63
- data/lib/wechat_public_api/templet_message.rb +87 -92
- data/lib/wechat_public_api/user.rb +30 -35
- data/lib/wechat_public_api/utils.rb +2 -5
- data/lib/wechat_public_api/version.rb +2 -2
- data/lib/wechat_public_api.rb +25 -5
- data/wechat_public_api-0.1.3.gem +0 -0
- data/wechat_public_api-0.1.4.gem +0 -0
- metadata +4 -2
@@ -5,147 +5,139 @@
|
|
5
5
|
# Wechat number: zmx119966
|
6
6
|
####################################################
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
module WechatPublicApi
|
8
|
+
class WechatPublicApi
|
11
9
|
module Kf
|
12
|
-
class << self
|
13
|
-
|
14
|
-
# 是否设置使用指定客服发送客服消息,如果不指定可以不填写
|
15
|
-
# @param <string> kf_account
|
16
|
-
attr_accessor :kf_account
|
17
|
-
|
18
|
-
###
|
19
|
-
# execute post
|
20
|
-
# => example
|
21
|
-
# message = {
|
22
|
-
# touser: openid,
|
23
|
-
# msgtype: 'text',
|
24
|
-
# text: {content: content},
|
25
|
-
# customservice:{
|
26
|
-
# kf_account: 'xxxxxxxx'
|
27
|
-
# }
|
28
|
-
# }
|
29
|
-
#
|
30
|
-
# @param <JSON> message
|
31
|
-
#
|
32
|
-
def post_customer_message(message)
|
33
|
-
# get access_token
|
34
|
-
access_token = AccessToken.get()
|
35
|
-
|
36
|
-
uri = URI.parse("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=#{access_token}")
|
37
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
38
|
-
http.use_ssl = true
|
39
|
-
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
40
|
-
request = Net::HTTP::Post.new("/cgi-bin/message/custom/send?access_token=#{access_token}")
|
41
|
-
request.add_field('Content-Type', 'application/json')
|
42
|
-
request.body = message.to_json.gsub(/\\u([0-9a-z]{4})/) {|s| [$1.to_i(16)].pack("U")}
|
43
|
-
response = http.request(request)
|
44
|
-
JSON.parse(response.body)
|
45
|
-
end
|
46
10
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
11
|
+
###
|
12
|
+
# execute post
|
13
|
+
# => example
|
14
|
+
# message = {
|
15
|
+
# touser: openid,
|
16
|
+
# msgtype: 'text',
|
17
|
+
# text: {content: content},
|
18
|
+
# customservice:{
|
19
|
+
# kf_account: 'xxxxxxxx'
|
20
|
+
# }
|
21
|
+
# }
|
22
|
+
#
|
23
|
+
# @param <JSON> message
|
24
|
+
#
|
25
|
+
def post_customer_message(message)
|
26
|
+
# get access_token
|
27
|
+
access_token = get_access_token()
|
28
|
+
|
29
|
+
uri = URI.parse("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=#{access_token}")
|
30
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
31
|
+
http.use_ssl = true
|
32
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
33
|
+
request = Net::HTTP::Post.new("/cgi-bin/message/custom/send?access_token=#{access_token}")
|
34
|
+
request.add_field('Content-Type', 'application/json')
|
35
|
+
request.body = message.to_json.gsub(/\\u([0-9a-z]{4})/) {|s| [$1.to_i(16)].pack("U")}
|
36
|
+
response = http.request(request)
|
37
|
+
JSON.parse(response.body)
|
38
|
+
end
|
53
39
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
40
|
+
###
|
41
|
+
# @param <string> openid -- 用户的openid
|
42
|
+
# @param <string> content -- 需要发送的客服消息内容
|
43
|
+
#
|
44
|
+
# 发送文字消息
|
45
|
+
def kf_text_message(openid, content, kf_account=nil)
|
46
|
+
|
47
|
+
custom_message = {
|
48
|
+
touser: openid,
|
49
|
+
msgtype: 'text',
|
50
|
+
text: {content: content}
|
51
|
+
}
|
52
|
+
|
53
|
+
if kf_account
|
54
|
+
custom_message.merge({customservice:{account: kf_account}})
|
55
|
+
end
|
59
56
|
|
60
|
-
|
61
|
-
|
62
|
-
end
|
57
|
+
post_customer_message custom_message
|
58
|
+
end
|
63
59
|
|
64
|
-
|
65
|
-
|
60
|
+
###
|
61
|
+
# @param <string> media_id -- 发送的图片/语音/视频/图文消息(点击跳转到图文消息页)的媒体ID
|
62
|
+
#
|
63
|
+
# 发送图片消息
|
64
|
+
def kf_image_message(openid, media_id, kf_account=nil)
|
66
65
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
66
|
+
custom_message = {
|
67
|
+
touser: openid,
|
68
|
+
msgtype: 'image',
|
69
|
+
image: {media_id: media_id}
|
70
|
+
}
|
72
71
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
image: {media_id: media_id}
|
77
|
-
}
|
72
|
+
if kf_account
|
73
|
+
custom_message.merge({customservice:{account: kf_account}})
|
74
|
+
end
|
78
75
|
|
79
|
-
|
80
|
-
|
81
|
-
end
|
76
|
+
post_customer_message custom_message
|
77
|
+
end
|
82
78
|
|
83
|
-
|
79
|
+
# 发送图文消息(点击跳转到图文消息页面)
|
80
|
+
def kf_mpnews_message(openid, media_id, kf_account=nil)
|
81
|
+
custom_message = {
|
82
|
+
touser: openid,
|
83
|
+
msgtype: 'mpnews',
|
84
|
+
mpnews: {media_id: media_id}
|
85
|
+
}
|
86
|
+
if kf_account
|
87
|
+
custom_message.merge({customservice:{account: kf_account}})
|
84
88
|
end
|
85
89
|
|
86
|
-
|
87
|
-
|
88
|
-
|
90
|
+
post_customer_message custom_message
|
91
|
+
end
|
92
|
+
|
93
|
+
###
|
94
|
+
# @param <JSON> articles -- 图文消息列表
|
95
|
+
# => example
|
96
|
+
# articles = [
|
97
|
+
# {
|
98
|
+
# "title":"Happy Day",
|
99
|
+
# "description":"Is Really A Happy Day",
|
100
|
+
# "url":"URL",
|
101
|
+
# "picurl":"PIC_URL"
|
102
|
+
# },
|
103
|
+
# {
|
104
|
+
# "title":"Happy Day",
|
105
|
+
# "description":"Is Really A Happy Day",
|
106
|
+
# "url":"URL",
|
107
|
+
# "picurl":"PIC_URL"
|
108
|
+
# }
|
109
|
+
# ]
|
110
|
+
#
|
111
|
+
# 发送图文消息(点击跳转到外链)
|
112
|
+
def kf_news_message(openid, articles, kf_account=nil)
|
113
|
+
custom_message = {
|
89
114
|
touser: openid,
|
90
|
-
msgtype: '
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
115
|
+
msgtype: 'news',
|
116
|
+
news: {
|
117
|
+
articles: articles
|
118
|
+
}
|
119
|
+
}
|
120
|
+
|
121
|
+
if kf_account
|
122
|
+
custom_message.merge({customservice:{account: kf_account}})
|
98
123
|
end
|
124
|
+
post_customer_message custom_message
|
125
|
+
end
|
99
126
|
|
100
|
-
|
101
|
-
|
102
|
-
# => example
|
103
|
-
# articles = [
|
104
|
-
# {
|
105
|
-
# "title":"Happy Day",
|
106
|
-
# "description":"Is Really A Happy Day",
|
107
|
-
# "url":"URL",
|
108
|
-
# "picurl":"PIC_URL"
|
109
|
-
# },
|
110
|
-
# {
|
111
|
-
# "title":"Happy Day",
|
112
|
-
# "description":"Is Really A Happy Day",
|
113
|
-
# "url":"URL",
|
114
|
-
# "picurl":"PIC_URL"
|
115
|
-
# }
|
116
|
-
# ]
|
117
|
-
#
|
118
|
-
# 发送图文消息(点击跳转到外链)
|
119
|
-
def news_message(openid, articles)
|
120
|
-
custom_message = {
|
121
|
-
touser: openid,
|
122
|
-
msgtype: 'news',
|
123
|
-
news: {
|
124
|
-
articles: articles
|
125
|
-
}
|
126
|
-
}
|
127
|
-
|
128
|
-
if kf_account
|
129
|
-
custom_message.merge({customservice:{account: kf_account}})
|
130
|
-
end
|
131
|
-
self.post_customer_message custom_message
|
132
|
-
end
|
127
|
+
# 发送语音消息
|
128
|
+
def kf_voice_message(openid, media_id, kf_account=nil)
|
133
129
|
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
}
|
144
|
-
if kf_account
|
145
|
-
custom_message.merge({customservice:{account: kf_account}})
|
146
|
-
end
|
147
|
-
self.post_customer_message custom_message
|
130
|
+
custom_message = {
|
131
|
+
touser: openid,
|
132
|
+
msgtype: 'voice',
|
133
|
+
voice: {
|
134
|
+
media_id: media_id
|
135
|
+
}
|
136
|
+
}
|
137
|
+
if kf_account
|
138
|
+
custom_message.merge({customservice:{account: kf_account}})
|
148
139
|
end
|
140
|
+
post_customer_message custom_message
|
149
141
|
end
|
150
142
|
end
|
151
143
|
end
|
@@ -5,49 +5,44 @@
|
|
5
5
|
# Wechat number: zmx119966
|
6
6
|
####################################################
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
module WechatPublicApi
|
8
|
+
class WechatPublicApi
|
11
9
|
module Material
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
#
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
JSON.parse(response)
|
31
|
-
end
|
10
|
+
###
|
11
|
+
# 获得临时图片素材的 media_id
|
12
|
+
# @param <string> file_path -- 图片素材的路径
|
13
|
+
#
|
14
|
+
# @return <json> {"type":"TYPE","media_id":"MEDIA_ID","created_at":123456789}
|
15
|
+
# if false
|
16
|
+
# @return <json> {"errcode":40004,"errmsg":"invalid media type"}
|
17
|
+
#
|
18
|
+
def upload_image_media(file_path)
|
19
|
+
# request access_token
|
20
|
+
access_token = get_access_token()
|
21
|
+
response = RestClient.post('https://api.weixin.qq.com/cgi-bin/media/upload',
|
22
|
+
{
|
23
|
+
access_token: access_token,
|
24
|
+
type: 'image',
|
25
|
+
media: File.new(file_path, 'rb')})
|
26
|
+
JSON.parse(response)
|
27
|
+
end
|
32
28
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
end
|
29
|
+
###
|
30
|
+
# 新增永久图片素材
|
31
|
+
# @param <string> file_path -- 图片素材的路径
|
32
|
+
#
|
33
|
+
# @return <json> {"type":"TYPE","media_id":"MEDIA_ID","created_at":123456789}
|
34
|
+
# if false
|
35
|
+
# @return <json> {"errcode":40004,"errmsg":"invalid media type"}
|
36
|
+
#
|
37
|
+
def upload_image_material(file_path)
|
38
|
+
# request access_token
|
39
|
+
access_token = get_access_token()
|
40
|
+
response = RestClient.post('https://api.weixin.qq.com/cgi-bin/material/add_material',
|
41
|
+
{
|
42
|
+
access_token: access_token,
|
43
|
+
type: 'image',
|
44
|
+
media: File.new(file_path, 'rb')})
|
45
|
+
JSON.parse(response)
|
51
46
|
end
|
52
47
|
end
|
53
48
|
end
|
@@ -5,73 +5,68 @@
|
|
5
5
|
# Wechat number: zmx119966
|
6
6
|
####################################################
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
module WechatPublicApi
|
8
|
+
class WechatPublicApi
|
11
9
|
module Menu
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
#
|
41
|
-
|
42
|
-
|
43
|
-
access_token = AccessToken.get()
|
44
|
-
post_data = post_data.to_json.gsub(/\\u([0-9a-z]{4})/) {|s| [$1.to_i(16)].pack("U")}
|
10
|
+
###
|
11
|
+
# create wechat public menu
|
12
|
+
# @param <json> post_data
|
13
|
+
#
|
14
|
+
# => post_data example
|
15
|
+
# {
|
16
|
+
# "button": [
|
17
|
+
# {
|
18
|
+
# "type": "view",
|
19
|
+
# "name": "",
|
20
|
+
# "url": "",
|
21
|
+
# "sub_button": []
|
22
|
+
# },
|
23
|
+
# {
|
24
|
+
# "type": "click",
|
25
|
+
# "name": "",
|
26
|
+
# "key": "menu_3",
|
27
|
+
# "sub_button": []
|
28
|
+
# }
|
29
|
+
# ]
|
30
|
+
# }
|
31
|
+
#
|
32
|
+
# if success
|
33
|
+
# @return <JSON> {"errcode"=>0, "errmsg"=>"ok"}
|
34
|
+
# if failed
|
35
|
+
# @return <JSON> {"errcode"=>40166, "errmsg"=>"...."}
|
36
|
+
#
|
37
|
+
def create_menu(post_data)
|
38
|
+
# request access_token
|
39
|
+
access_token = get_access_token()
|
40
|
+
post_data = post_data.to_json.gsub(/\\u([0-9a-z]{4})/) {|s| [$1.to_i(16)].pack("U")}
|
45
41
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
42
|
+
uri = URI.parse("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=#{access_token}")
|
43
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
44
|
+
http.use_ssl = true
|
45
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
46
|
+
request = Net::HTTP::Post.new("/cgi-bin/menu/create?access_token=#{access_token}")
|
47
|
+
request.add_field('Content-Type', 'application/json')
|
48
|
+
request.body = post_data
|
49
|
+
response = http.request(request)
|
50
|
+
(JSON.parse response.body)
|
51
|
+
end
|
56
52
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
53
|
+
# get wechat public menu list
|
54
|
+
def query_menu()
|
55
|
+
# request access_token
|
56
|
+
access_token = get_access_token()
|
57
|
+
response = HTTParty.get("https://api.weixin.qq.com/cgi-bin/menu/get?access_token=#{access_token}").body
|
58
|
+
(JSON.parse response)
|
59
|
+
end
|
64
60
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
end
|
61
|
+
###
|
62
|
+
# delete wechat query from access_token
|
63
|
+
# @return <JSON> {"errcode"=>0, "errmsg"=>"ok"}
|
64
|
+
#
|
65
|
+
def delete_menu()
|
66
|
+
# request access_token
|
67
|
+
access_token = get_access_token()
|
68
|
+
response = HTTParty.get("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=#{access_token}").body
|
69
|
+
(JSON.parse response)
|
75
70
|
end
|
76
71
|
end
|
77
72
|
end
|