wechat_public_api 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/wechat_public_api/access_token.rb +40 -0
- data/lib/wechat_public_api/kf_message.rb +151 -0
- data/lib/wechat_public_api/menu.rb +77 -0
- data/lib/wechat_public_api/templet_message.rb +109 -0
- data/lib/wechat_public_api/version.rb +3 -0
- data/lib/wechat_public_api.rb +13 -0
- data/wechat_public_api.gemspec +40 -0
- metadata +157 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bed6ccb358f95e9f869f07ecee7062de0fe186d9
|
4
|
+
data.tar.gz: 26e0fc2bd8ec03557bd73121292c7b7a761f6163
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 65b6ee27a31430294041f827c3743aeecf00dc629ac742615c0d1a40d832b9424983ca06e6128effc0ba41b59ffc0ca52d1d54bfc090b1b4e0cba5be2b96a1b4
|
7
|
+
data.tar.gz: 45fbfac8144f482c114a44a43c271fca6782a1bf62251b76868674541b8ba5a9db62ebc1e2521917cb91a9569c497870b956888c56b64d34900310823d01580e
|
data/.gitignore
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at mnzmx_z@163.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 zhangS2
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# WechatPublicApi
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/wechat_public_api`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'wechat_public_api'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install wechat_public_api
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/wechat_public_api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
40
|
+
|
41
|
+
## Code of Conduct
|
42
|
+
|
43
|
+
Everyone interacting in the WechatPublicApi project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/wechat_public_api/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "wechat_public_api"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
#####################################################
|
2
|
+
# 获得微信公众号的 access_token (Get wechat public access_token)
|
3
|
+
# Created by zhangmingxin
|
4
|
+
# Date: 2018-05-17
|
5
|
+
# Wechat number: zmx119966
|
6
|
+
####################################################
|
7
|
+
|
8
|
+
module WechatPublicApi
|
9
|
+
module AccessToken
|
10
|
+
class << self
|
11
|
+
|
12
|
+
###
|
13
|
+
# 获取 access_token
|
14
|
+
# 判断access_token_cache,决定是否需要缓存数据
|
15
|
+
# @return <string> nil or access_token
|
16
|
+
def get()
|
17
|
+
appid = WechatPublicApi.app_id
|
18
|
+
secret = WechatPublicApi.app_secret
|
19
|
+
access_token_cache = WechatPublicApi.access_token_cache
|
20
|
+
|
21
|
+
unless access_token_cache
|
22
|
+
response = HTTParty.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=#{appid}&secret=#{secret}").body
|
23
|
+
response_body = (JSON.parse response)
|
24
|
+
return response_body['access_token']
|
25
|
+
end
|
26
|
+
|
27
|
+
_cache_key = "#{appid}_access_token"
|
28
|
+
_cached_access_token = $redis.get _cache_key
|
29
|
+
if _cached_access_token == nil or _cached_access_token == ''
|
30
|
+
response = HTTParty.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=#{appid}&secret=#{secret}").body
|
31
|
+
response_body = (JSON.parse response)
|
32
|
+
Rails.logger.info response_body
|
33
|
+
_cached_access_token = response_body['access_token']
|
34
|
+
$redis.set _cache_key, _cached_access_token, ex: 2.minutes
|
35
|
+
end
|
36
|
+
_cached_access_token
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,151 @@
|
|
1
|
+
#####################################################
|
2
|
+
# 客服消息管理 (about kf_account message)
|
3
|
+
# Created by zhangmingxin
|
4
|
+
# Date: 2018-05-17
|
5
|
+
# Wechat number: zmx119966
|
6
|
+
####################################################
|
7
|
+
|
8
|
+
require "wechat_public_api/access_token"
|
9
|
+
|
10
|
+
module WechatPublicApi
|
11
|
+
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 self.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
|
+
|
47
|
+
###
|
48
|
+
# @param <string> openid -- 用户的openid
|
49
|
+
# @param <string> content -- 需要发送的客服消息内容
|
50
|
+
#
|
51
|
+
# 发送文字消息
|
52
|
+
def self.text_message(openid, content)
|
53
|
+
|
54
|
+
custom_message = {
|
55
|
+
touser: openid,
|
56
|
+
msgtype: 'text',
|
57
|
+
text: {content: content}
|
58
|
+
}
|
59
|
+
|
60
|
+
if kf_account
|
61
|
+
custom_message.merge({customservice:{account: kf_account}})
|
62
|
+
end
|
63
|
+
|
64
|
+
self.post_customer_message custom_message
|
65
|
+
end
|
66
|
+
|
67
|
+
###
|
68
|
+
# @param <string> media_id -- 发送的图片/语音/视频/图文消息(点击跳转到图文消息页)的媒体ID
|
69
|
+
#
|
70
|
+
# 发送图片消息
|
71
|
+
def self.image_message(openid, media_id)
|
72
|
+
|
73
|
+
custom_message = {
|
74
|
+
touser: openid,
|
75
|
+
msgtype: 'image',
|
76
|
+
image: {media_id: media_id}
|
77
|
+
}
|
78
|
+
|
79
|
+
if kf_account
|
80
|
+
custom_message.merge({customservice:{account: kf_account}})
|
81
|
+
end
|
82
|
+
|
83
|
+
self.post_customer_message custom_message
|
84
|
+
end
|
85
|
+
|
86
|
+
# 发送图文消息(点击跳转到图文消息页面)
|
87
|
+
def mpnews_message(openid, media_id)
|
88
|
+
custom_message = {
|
89
|
+
touser: openid,
|
90
|
+
msgtype: 'mpnews',
|
91
|
+
mpnews: {media_id: media_id}
|
92
|
+
}
|
93
|
+
if kf_account
|
94
|
+
custom_message.merge({customservice:{account: kf_account}})
|
95
|
+
end
|
96
|
+
|
97
|
+
self.post_customer_message custom_message
|
98
|
+
end
|
99
|
+
|
100
|
+
###
|
101
|
+
# @param <JSON> articles -- 图文消息列表
|
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 self.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
|
133
|
+
|
134
|
+
# 发送语音消息
|
135
|
+
def self.voice_message(openid, media_id)
|
136
|
+
|
137
|
+
custom_message = {
|
138
|
+
touser: openid,
|
139
|
+
msgtype: 'voice',
|
140
|
+
voice: {
|
141
|
+
media_id: media_id
|
142
|
+
}
|
143
|
+
}
|
144
|
+
if kf_account
|
145
|
+
custom_message.merge({customservice:{account: kf_account}})
|
146
|
+
end
|
147
|
+
self.post_customer_message custom_message
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
#####################################################
|
2
|
+
# 公众号自定义菜单栏模块 (about wechat public menu)
|
3
|
+
# Created by zhangmingxin
|
4
|
+
# Date: 2018-05-17
|
5
|
+
# Wechat number: zmx119966
|
6
|
+
####################################################
|
7
|
+
|
8
|
+
require "wechat_public_api/access_token"
|
9
|
+
|
10
|
+
module WechatPublicApi
|
11
|
+
module Menu
|
12
|
+
class << self
|
13
|
+
|
14
|
+
###
|
15
|
+
# create wechat public menu
|
16
|
+
# @param <json> post_data
|
17
|
+
#
|
18
|
+
# => post_data example
|
19
|
+
# {
|
20
|
+
# "button": [
|
21
|
+
# {
|
22
|
+
# "type": "view",
|
23
|
+
# "name": "",
|
24
|
+
# "url": "",
|
25
|
+
# "sub_button": []
|
26
|
+
# },
|
27
|
+
# {
|
28
|
+
# "type": "click",
|
29
|
+
# "name": "",
|
30
|
+
# "key": "menu_3",
|
31
|
+
# "sub_button": []
|
32
|
+
# }
|
33
|
+
# ]
|
34
|
+
# }
|
35
|
+
#
|
36
|
+
# if success
|
37
|
+
# @return <JSON> {"errcode"=>0, "errmsg"=>"ok"}
|
38
|
+
# if failed
|
39
|
+
# @return <JSON> {"errcode"=>40166, "errmsg"=>"...."}
|
40
|
+
#
|
41
|
+
def create(post_data)
|
42
|
+
# request access_token
|
43
|
+
access_token = AccessToken.get()
|
44
|
+
|
45
|
+
uri = URI.parse("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=#{access_token}")
|
46
|
+
post_data = post_data
|
47
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
48
|
+
http.use_ssl = true
|
49
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
50
|
+
request = Net::HTTP::Post.new("/cgi-bin/menu/create?access_token=#{access_token}")
|
51
|
+
request.add_field('Content-Type', 'application/json')
|
52
|
+
request.body = post_data
|
53
|
+
response = http.request(request)
|
54
|
+
(JSON.parse response.body)
|
55
|
+
end
|
56
|
+
|
57
|
+
# get wechat public menu list
|
58
|
+
def query()
|
59
|
+
# request access_token
|
60
|
+
access_token = AccessToken.get()
|
61
|
+
response = HTTParty.get("https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN=#{access_token}").body
|
62
|
+
(JSON.parse response)
|
63
|
+
end
|
64
|
+
|
65
|
+
###
|
66
|
+
# delete wechat query from access_token
|
67
|
+
# @return <JSON> {"errcode"=>0, "errmsg"=>"ok"}
|
68
|
+
#
|
69
|
+
def delete()
|
70
|
+
# request access_token
|
71
|
+
access_token = AccessToken.get()
|
72
|
+
response = HTTParty.get("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN=#{access_token}").body
|
73
|
+
(JSON.parse response)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
#####################################################
|
2
|
+
# 微信公众号模板消息相关接口 (About templet message of wechat public)
|
3
|
+
# Created by zhangmingxin
|
4
|
+
# Date: 2018-05-17
|
5
|
+
# Wechat number: zmx119966
|
6
|
+
####################################################
|
7
|
+
|
8
|
+
require "wechat_public_api/access_token"
|
9
|
+
|
10
|
+
module WechatPublicApi
|
11
|
+
module TP
|
12
|
+
class << self
|
13
|
+
|
14
|
+
###
|
15
|
+
# @param <JSON> message
|
16
|
+
# @param <string> url_params -- 消息路径
|
17
|
+
# 发送模板消息接口
|
18
|
+
def post_template_message(message, url_params)
|
19
|
+
# get access_token
|
20
|
+
access_token = AccessToken.get()
|
21
|
+
|
22
|
+
@access_token = Wxutils.get_access_token wx_account
|
23
|
+
uri = URI.parse("https://api.weixin.qq.com/cgi-bin/message/template/#{url_params}?access_token=#{@access_token}")
|
24
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
25
|
+
http.use_ssl = true
|
26
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
27
|
+
request = Net::HTTP::Post.new("/cgi-bin/message/template/#{url_params}?access_token=#{@access_token}")
|
28
|
+
request.add_field('Content-Type', 'application/json')
|
29
|
+
# 部分字符转换为json后 成为unicode编码
|
30
|
+
request.body = message.to_json.gsub(/\\u([0-9a-z]{4})/) {|s| [$1.to_i(16)].pack("U")}
|
31
|
+
response = http.request(request)
|
32
|
+
JSON.parse(response.body)
|
33
|
+
end
|
34
|
+
|
35
|
+
###
|
36
|
+
# @param <string> openid -- 用户的openid
|
37
|
+
# @param <string> templet_id -- 微信公众号后台提供的模板ID
|
38
|
+
# @param <string> url -- 模板跳转链接
|
39
|
+
# @param <string> appid -- 小程序的appid
|
40
|
+
# @param <string> pagepath -- 小程序页面路径 (eq: /index/index)
|
41
|
+
# @param <JSON> data -- 模板数据
|
42
|
+
# => data example
|
43
|
+
# data = {
|
44
|
+
# "first": {
|
45
|
+
# "value":"恭喜你购买成功!",
|
46
|
+
# "color":"#173177"
|
47
|
+
# },
|
48
|
+
# "keyword1":{
|
49
|
+
# "value":"巧克力",
|
50
|
+
# "color":"#173177"
|
51
|
+
# },
|
52
|
+
# "keyword2": {
|
53
|
+
# "value":"39.8元",
|
54
|
+
# "color":"#173177"
|
55
|
+
# },
|
56
|
+
# "keyword3": {
|
57
|
+
# "value":"2014年9月22日",
|
58
|
+
# "color":"#173177"
|
59
|
+
# },
|
60
|
+
# "remark":{
|
61
|
+
# "value":"欢迎再次购买!",
|
62
|
+
# "color":"#173177"
|
63
|
+
# }
|
64
|
+
# }
|
65
|
+
# 小程序模板消息
|
66
|
+
def miniprogram_message(openid, templet_id, url, appid, pagepath, data)
|
67
|
+
message = {
|
68
|
+
'touser': openid,
|
69
|
+
'template_id': templet_id,
|
70
|
+
'url': url,
|
71
|
+
"miniprogram":{
|
72
|
+
"appid": appid,
|
73
|
+
"pagepath": pagepath
|
74
|
+
},
|
75
|
+
'data': data
|
76
|
+
}
|
77
|
+
|
78
|
+
self.post_template_message message, 'send'
|
79
|
+
end
|
80
|
+
|
81
|
+
# 普通的模板消息,不跳转小程序
|
82
|
+
def general_message(openid, templet_id, url, data)
|
83
|
+
message = {
|
84
|
+
'touser': openid,
|
85
|
+
'template_id': templet_id,
|
86
|
+
'url': url,
|
87
|
+
'data': data
|
88
|
+
}
|
89
|
+
self.post_template_message message, 'send'
|
90
|
+
end
|
91
|
+
|
92
|
+
# 删除模板
|
93
|
+
def delete(templet_id)
|
94
|
+
message = {
|
95
|
+
'template_id': template_id
|
96
|
+
}
|
97
|
+
self.post_template_message message, 'del_private_template'
|
98
|
+
end
|
99
|
+
|
100
|
+
# 获得模板列表
|
101
|
+
def get_all()
|
102
|
+
access_token = AccessToken.get()
|
103
|
+
response = HTTParty.get("https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=#{access_token}").body
|
104
|
+
response_body = (JSON.parse response)
|
105
|
+
response_body
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require "wechat_public_api/version"
|
2
|
+
require "wechat_public_api/menu"
|
3
|
+
require "wechat_public_api/kf_message"
|
4
|
+
require "wechat_public_api/templet_message"
|
5
|
+
require "wechat_public_api/access_token"
|
6
|
+
|
7
|
+
module WechatPublicApi
|
8
|
+
class << self
|
9
|
+
# 默认不缓存 access_token, access_token_cache = True 缓存
|
10
|
+
# @param <Boolearn> access_token_cache
|
11
|
+
attr_accessor :app_id, :app_secret, :access_token_cache
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "wechat_public_api/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "wechat_public_api"
|
8
|
+
spec.version = WechatPublicApi::VERSION
|
9
|
+
spec.authors = ["张明鑫"]
|
10
|
+
spec.email = ["mnzmx_z@163.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{微信公众号开发API,包含对微信公众号菜单栏、客服消息、模板消息、帐号管理等接口的封装}
|
13
|
+
spec.description = %q{}
|
14
|
+
spec.homepage = "https://github.com/Diyilou/wechat_public_api.git"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
# else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
# "public gem pushes."
|
24
|
+
# end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
35
|
+
spec.add_development_dependency "redis"
|
36
|
+
spec.add_development_dependency "httparty"
|
37
|
+
spec.add_development_dependency "json"
|
38
|
+
spec.add_development_dependency "rest-client"
|
39
|
+
spec.add_development_dependency 'unf_ext', '~> 0.0.7.5'
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wechat_public_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- 张明鑫
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-05-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: redis
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: httparty
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: json
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rest-client
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: unf_ext
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 0.0.7.5
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 0.0.7.5
|
111
|
+
description: ''
|
112
|
+
email:
|
113
|
+
- mnzmx_z@163.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- CODE_OF_CONDUCT.md
|
120
|
+
- Gemfile
|
121
|
+
- LICENSE.txt
|
122
|
+
- README.md
|
123
|
+
- Rakefile
|
124
|
+
- bin/console
|
125
|
+
- bin/setup
|
126
|
+
- lib/wechat_public_api.rb
|
127
|
+
- lib/wechat_public_api/access_token.rb
|
128
|
+
- lib/wechat_public_api/kf_message.rb
|
129
|
+
- lib/wechat_public_api/menu.rb
|
130
|
+
- lib/wechat_public_api/templet_message.rb
|
131
|
+
- lib/wechat_public_api/version.rb
|
132
|
+
- wechat_public_api.gemspec
|
133
|
+
homepage: https://github.com/Diyilou/wechat_public_api.git
|
134
|
+
licenses:
|
135
|
+
- MIT
|
136
|
+
metadata: {}
|
137
|
+
post_install_message:
|
138
|
+
rdoc_options: []
|
139
|
+
require_paths:
|
140
|
+
- lib
|
141
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - ">="
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0'
|
151
|
+
requirements: []
|
152
|
+
rubyforge_project:
|
153
|
+
rubygems_version: 2.5.2.1
|
154
|
+
signing_key:
|
155
|
+
specification_version: 4
|
156
|
+
summary: 微信公众号开发API,包含对微信公众号菜单栏、客服消息、模板消息、帐号管理等接口的封装
|
157
|
+
test_files: []
|