ocean-wechat 0.1.0
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 +7 -0
- data/.gitignore +8 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/README.md +39 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/ocean/wechat/version.rb +5 -0
- data/lib/ocean/wechat.rb +187 -0
- data/ocean-wechat.gemspec +35 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c84fb045f758d8641894311894135dc3525cdcfc3a17701018bb50debe758d49
|
4
|
+
data.tar.gz: 7efcc812a28f9c6d355f014c0ee30caf4a037df5dc9c1820578e0a38ca8baa56
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6d11c3ffa7e096aab6e83afe347f21ffc941a4f1495f5e433e5511a28df7f85559d79c7662cbe00698e06e6bc865400999669c346cf84a287026dc3af4fc29a4
|
7
|
+
data.tar.gz: b9497e673889bb38f87e318e9ad19a00166931ccf8fb6391c906e9933f1159359090ce9a3d36265847920a04cc10692383c89028c730ffa0f24d0e03b1d3bf3e
|
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 mr.bestjane@gmail.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/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Ocean::Wechat
|
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/ocean/wechat`. 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 'ocean-wechat'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install ocean-wechat
|
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]/ocean-wechat. 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
|
+
## Code of Conduct
|
38
|
+
|
39
|
+
Everyone interacting in the Ocean::Wechat project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ocean-wechat/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 "ocean/wechat"
|
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
data/lib/ocean/wechat.rb
ADDED
@@ -0,0 +1,187 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Wechat service, View more at: https://mp.weixin.qq.com/wiki
|
4
|
+
# example:
|
5
|
+
# Wechat.notify(event: 'crash', first: 'Your website is not accessible', time: Time.now, reason: 'Connect fail!', remark: 'remark', redirect_url: 'www.baidu.com')
|
6
|
+
module Wechat
|
7
|
+
extend self
|
8
|
+
|
9
|
+
BASE_URL = 'https://api.weixin.qq.com/cgi-bin'
|
10
|
+
ACCESS_TOKEN_FILE = 'tmp/access_token.txt'
|
11
|
+
NOTIFY_RECIPIENT_FILE = 'config/wechat_notify.yml'
|
12
|
+
SUCCESS_CODE = 0
|
13
|
+
ACCESS_TOKEN_EXPIRED_CODE = 420_01
|
14
|
+
ACCESS_TOKEN_INVALID_CODE = 400_01
|
15
|
+
NOTIFY_DEFAULT_COLOR = '#173177'
|
16
|
+
MAX_RETRY_NUM = 5
|
17
|
+
|
18
|
+
def access_token
|
19
|
+
read_access_token || refresh_access_token
|
20
|
+
end
|
21
|
+
|
22
|
+
# Use wechat template to send message
|
23
|
+
def notify(**args)
|
24
|
+
notify_block = lambda do |open_id|
|
25
|
+
url = "#{BASE_URL}/message/template/send"
|
26
|
+
params = {
|
27
|
+
touser: open_id,
|
28
|
+
template_id: wechat_notify(args[:event], 'template'),
|
29
|
+
url: args[:redirect_url],
|
30
|
+
data: {}
|
31
|
+
}
|
32
|
+
|
33
|
+
args.each { |k, v| params[:data][k] = { value: v, color: NOTIFY_DEFAULT_COLOR } }
|
34
|
+
|
35
|
+
request(url, params, :post)
|
36
|
+
end
|
37
|
+
|
38
|
+
[].tap do |item|
|
39
|
+
wechat_notify(args[:event], 'users').each do |open_id|
|
40
|
+
item << {
|
41
|
+
open_id: open_id,
|
42
|
+
success: notify_block.call(open_id)[:errcode] == SUCCESS_CODE
|
43
|
+
}
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Get all notify template
|
49
|
+
def notify_templates
|
50
|
+
request("#{BASE_URL}/template/get_all_private_template")
|
51
|
+
end
|
52
|
+
|
53
|
+
# Get all followers
|
54
|
+
def users
|
55
|
+
request("#{BASE_URL}/user/get")
|
56
|
+
end
|
57
|
+
|
58
|
+
def create_user_tag(name)
|
59
|
+
params = {
|
60
|
+
tag: {
|
61
|
+
name: name
|
62
|
+
}
|
63
|
+
}
|
64
|
+
|
65
|
+
request("#{BASE_URL}/tags/create", params, :post)
|
66
|
+
end
|
67
|
+
|
68
|
+
# Get all user tag
|
69
|
+
def user_tags
|
70
|
+
request("#{BASE_URL}/tags/get")
|
71
|
+
end
|
72
|
+
|
73
|
+
def find_users_by_tag(name)
|
74
|
+
res = request(
|
75
|
+
"#{BASE_URL}/user/tag/get",
|
76
|
+
{
|
77
|
+
tagid: find_tag_id_by_name(name)
|
78
|
+
},
|
79
|
+
:post
|
80
|
+
)
|
81
|
+
|
82
|
+
res['count'] == 0 ? [] : res['data']['openid']
|
83
|
+
end
|
84
|
+
|
85
|
+
def find_tag_id_by_name(name)
|
86
|
+
user_tags[:tags].detect { |item| item['name'] == name }['id']
|
87
|
+
end
|
88
|
+
|
89
|
+
def add_tag_for_users(users, tag_id)
|
90
|
+
params = {
|
91
|
+
open_list: users,
|
92
|
+
tagid: tag_id
|
93
|
+
}
|
94
|
+
|
95
|
+
request("#{BASE_URL}/tags/members/batchtagging", params, :post)
|
96
|
+
end
|
97
|
+
|
98
|
+
private
|
99
|
+
|
100
|
+
def request(source_url, params = {}, method = :get, retry_num = 0)
|
101
|
+
url = compose_url(source_url)
|
102
|
+
|
103
|
+
log.debug "Send #{method} request #{params} to #{url}"
|
104
|
+
|
105
|
+
res = if method == :get
|
106
|
+
RestClient.get(url, params)
|
107
|
+
else
|
108
|
+
RestClient.post(url, params.to_json, content_type: :json, accept: :json)
|
109
|
+
end
|
110
|
+
|
111
|
+
res_body = read_content(res)
|
112
|
+
json = JSON.parse force_encoding(res_body)
|
113
|
+
|
114
|
+
json.symbolize_keys!
|
115
|
+
|
116
|
+
if json[:errcode] == ACCESS_TOKEN_EXPIRED_CODE || json[:errcode] == ACCESS_TOKEN_INVALID_CODE
|
117
|
+
# Try again five times at most
|
118
|
+
return if retry_num == MAX_RETRY_NUM
|
119
|
+
|
120
|
+
log.info("Access token is expired or invalid, retry #{retry_num} ...")
|
121
|
+
|
122
|
+
refresh_access_token
|
123
|
+
|
124
|
+
# Control call frequency
|
125
|
+
sleep(retry_num * 10)
|
126
|
+
retry_num += 1
|
127
|
+
return request(source_url, params, method, retry_num)
|
128
|
+
elsif json[:errcode] && json[:errcode] != SUCCESS_CODE
|
129
|
+
raise "Request #{url}, errmsg: #{json[:errmsg]}, errcode: #{json[:errcode]}"
|
130
|
+
else
|
131
|
+
json
|
132
|
+
end
|
133
|
+
rescue StandardError => ex
|
134
|
+
log.error("Call wechat api error: #{ex}\n #{ex.backtrace.join("\n")}")
|
135
|
+
json
|
136
|
+
end
|
137
|
+
|
138
|
+
# Remove not UTF-8 encoding
|
139
|
+
def force_encoding(data)
|
140
|
+
data.force_encoding('UTF-8').gsub(/[\u0000-\u001f\u007f\u0080-\u009f]+/, '*')
|
141
|
+
end
|
142
|
+
|
143
|
+
def read_content(res)
|
144
|
+
if res['content-encoding'].present? && res['content-encoding'] == 'gzip'
|
145
|
+
Zlib::GzipReader.new(StringIO.new(res.body), encoding: 'UTF-8').read
|
146
|
+
else
|
147
|
+
res.body
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def compose_url(source_url)
|
152
|
+
source_url.include?('?') ? source_url : "#{source_url}?access_token=#{access_token}"
|
153
|
+
end
|
154
|
+
|
155
|
+
def refresh_access_token
|
156
|
+
params = {
|
157
|
+
grant_type: 'client_credential',
|
158
|
+
appid: ENV.fetch('WECHAT_APP_ID'),
|
159
|
+
secret: ENV.fetch('WECHAT_APP_SECRET')
|
160
|
+
}
|
161
|
+
|
162
|
+
res = request("#{BASE_URL}/token?#{params.to_query}", {}, 'GET')
|
163
|
+
save_access_token(res[:access_token]) if res[:access_token]
|
164
|
+
res[:access_token]
|
165
|
+
end
|
166
|
+
|
167
|
+
def save_access_token(token)
|
168
|
+
file = File.new(ACCESS_TOKEN_FILE, 'w')
|
169
|
+
file.write(token)
|
170
|
+
rescue IOError => ex
|
171
|
+
log.error("Write access_token fail: #{ex}")
|
172
|
+
ensure
|
173
|
+
file.close
|
174
|
+
end
|
175
|
+
|
176
|
+
def read_access_token
|
177
|
+
File.exist?(ACCESS_TOKEN_FILE) ? File.read(ACCESS_TOKEN_FILE) : nil
|
178
|
+
end
|
179
|
+
|
180
|
+
def wechat_notify(event, key)
|
181
|
+
File.exist?(NOTIFY_RECIPIENT_FILE) ? YAML.load_file(NOTIFY_RECIPIENT_FILE)[event.to_s][key] : []
|
182
|
+
end
|
183
|
+
|
184
|
+
def log
|
185
|
+
Rails.logger
|
186
|
+
end
|
187
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "ocean/wechat/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ocean-wechat"
|
8
|
+
spec.version = Ocean::Wechat::VERSION
|
9
|
+
spec.authors = ["bestjane"]
|
10
|
+
spec.email = ["mr.bestjane@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Wechat api}
|
13
|
+
spec.description = %q{ Wechat api achieve}
|
14
|
+
spec.homepage = ""
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
unless spec.respond_to?(:metadata)
|
19
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
20
|
+
"public gem pushes."
|
21
|
+
end
|
22
|
+
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
26
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
spec.add_dependency 'rest-client', '>= 2.0.2'
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ocean-wechat
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- bestjane
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-10-08 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: rest-client
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.0.2
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.0.2
|
55
|
+
description: " Wechat api achieve"
|
56
|
+
email:
|
57
|
+
- mr.bestjane@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- CODE_OF_CONDUCT.md
|
64
|
+
- Gemfile
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- bin/console
|
68
|
+
- bin/setup
|
69
|
+
- lib/ocean/wechat.rb
|
70
|
+
- lib/ocean/wechat/version.rb
|
71
|
+
- ocean-wechat.gemspec
|
72
|
+
homepage: ''
|
73
|
+
licenses: []
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.7.7
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: Wechat api
|
95
|
+
test_files: []
|