wechat-api 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 15ac3fb9d0cc42e4c68c0ec5735ebecc931fb5e2
4
- data.tar.gz: 12e850d08d683026d0fd835210e3a098316fe7bc
2
+ SHA256:
3
+ metadata.gz: 8586728146eb49d713757e2a324ef9607ddf1098355e8347c35a5b8acd34c392
4
+ data.tar.gz: b6d7f195acf180b876802a1c52516ac96a085e35bbd10612ad68a87023ffc049
5
5
  SHA512:
6
- metadata.gz: 40bd671ea3f24e6de3ca06c838fadbb0314f53eaab1fc8b8fbcec3fb5a9439fae97ef2875ac865d08ccdd1d51a9d70fff46ff2fba0faf6db41565dea84ee5192
7
- data.tar.gz: ed62e611850637e810d75c21ffdc2ca9a4da40cf17bb79c94286628210dd236bb411b38971557ed9954f8b79028ba90a427d7416bb410a93e4dfbe19d146ec6b
6
+ metadata.gz: 07a8e8d9ac1157d77196a895c282011cc248fa9dbc4e3932de24037461fef6fbb3284869a97f3f01d585d77b1a3a7c0509ae2396e07571e19f5d4bf0cc74e0c2
7
+ data.tar.gz: 6a182e5b2058d6aa8bc8c8260e06f53f6f0e86ba7ebbf5bf414a6a212a8d12b61e1cbfe2143a759a1a222989008875a840b13c3fa0df79a203ba2b151aeec16a
@@ -0,0 +1,20 @@
1
+ name: Ruby
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ build:
7
+
8
+ runs-on: ubuntu-latest
9
+
10
+ steps:
11
+ - uses: actions/checkout@v1
12
+ - name: Set up Ruby 2.6
13
+ uses: actions/setup-ruby@v1
14
+ with:
15
+ ruby-version: 2.6.x
16
+ - name: Build and test with Rake
17
+ run: |
18
+ gem install bundler
19
+ bundle install --jobs 4 --retry 3
20
+ bundle exec rake
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
1
  Gemfile.lock
2
2
  pkg
3
+ .DS_Store
data/Gemfile CHANGED
@@ -1,9 +1,3 @@
1
- source 'http://ruby.taobao.org'
1
+ source 'https://rubygems.org'
2
2
  # Specify your gem's dependencies in ucpaas.gemspec
3
3
  gemspec
4
-
5
- group :development do
6
- gem 'guard'
7
- gem 'guard-rspec'
8
- gem 'rspec-its'
9
- end
@@ -0,0 +1,84 @@
1
+ # wechat-api
2
+
3
+ https://github.com/lazing/wechat-api
4
+
5
+ 用于微信 api 调用(非服务端推送信息)的处理。
6
+
7
+ [![Gem Version](https://badge.fury.io/rb/wechat-api.svg)](http://badge.fury.io/rb/wechat-api)
8
+
9
+ 常见的应用场景如:
10
+ * 获取关注用户
11
+ * 推送模板信息
12
+ * 创建二维码
13
+ * 创建短链接
14
+
15
+ 远期计划进一步支持微信支付等。
16
+
17
+ ## 为什么不使用其他 gem
18
+
19
+ * 很多 gem 基于 rails 或其他 web 框架,单独使用过于笨重
20
+ * 不够简单,特别是用于多账号管理的时候
21
+ * 微信 api 更新频繁,需要易于使用新功能
22
+
23
+ ## 主要功能清单
24
+ * 支持微信服务号和订阅号API
25
+ * 支持微信js_ticket获取
26
+ * 支持微信红包API
27
+ * 支持微信企业号主动调用API
28
+
29
+ ## 使用方式
30
+
31
+ ````ruby
32
+ gem 'wechat-api'
33
+ ````
34
+
35
+ ````ruby
36
+ require 'wechat-api'
37
+
38
+ api = Wechat::Api::Client.new 'appid', 'appsecret'
39
+
40
+ api.get 'user/info', nextopenid: 'xxx'
41
+ # 当使用 get 方式时,hash 参数将做个 query params 附在请求后
42
+
43
+ api.post 'user/info/updateremark', openid;'xxxx', remark: '我是注释'
44
+ # 当使用 post 方法时,hash 参数将转换为 json,因此可以支持嵌套的结构
45
+ ````
46
+
47
+ 微信企业号主动调用API
48
+
49
+ ````ruby
50
+ require 'wechat-api'
51
+
52
+ qy = Wechat::Qy::Client.new 'corpid', 'corpsecret'
53
+ qy.text_send 'agentid', 'message', touser: 'UserId1|UserId2' #提供的预定义方法
54
+ qy.post 'api/uri', { key: :value} #未提供预定义方法时调用
55
+ ````
56
+
57
+ ## 一些预定义方法
58
+
59
+ 预定义接口方法可以方便使用。
60
+
61
+ 目前支持如下:
62
+
63
+ ````ruby
64
+ # 创建固定二维码
65
+ api.create_qrcode(scene_str)
66
+
67
+ # 创建临时二维码
68
+ api.create_qrcode_temp(scene_id)
69
+
70
+ # 发送模板消息
71
+ api.send_template(template_id, openid, url, data = {})
72
+ ````
73
+
74
+ 以上接口返回微信文档定义的 json 数据转换后的ruby的 hash 对象
75
+
76
+ ````ruby
77
+ {
78
+ "ticket"=>"gQHc7zoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0NVeGttUnJrOGZhSTdyX2RzR1F3AAIEZKcDVgMEAAAAAA==",
79
+ "url"=>"http://weixin.qq.com/q/CUxkmRrk8faI7r_dsGQw"
80
+ }
81
+ ````
82
+
83
+ ## 关于贡献
84
+ 功能请求,请直接 fork 并创建 Merge Request
@@ -1 +1,2 @@
1
1
  require 'wechat/api'
2
+ require 'wechat/qy'
@@ -37,14 +37,13 @@ module Wechat
37
37
  end
38
38
 
39
39
  def refresh
40
- logger.debug { 'refresh token' }
41
40
  url = format('%stoken', API_BASE)
42
41
  resp = connection.get(url, token_params)
43
42
  response = MultiJson.load(resp.body)
44
43
  return handle_error(response) if response['errcode']
45
- token = response['access_token']
46
- File.open(@token_file, 'w') { |f| f.write(resp.body) } if token
47
- token
44
+ @access_token = response['access_token']
45
+ File.open(@token_file, 'w') { |f| f.write(resp.body) } if @access_token
46
+ @access_token
48
47
  end
49
48
 
50
49
  def get(uri, params = {})
@@ -27,6 +27,11 @@ module Wechat
27
27
 
28
28
  post 'qrcode/create', params
29
29
  end
30
+
31
+ def js_ticket
32
+ res = get 'ticket/getticket', type: :jsapi
33
+ res['ticket']
34
+ end
30
35
  end
31
36
  end
32
37
  end
@@ -2,6 +2,6 @@
2
2
  module Wechat
3
3
  #
4
4
  module Api
5
- VERSION = '0.1.0'
5
+ VERSION = '0.3.0'.freeze
6
6
  end
7
7
  end
@@ -0,0 +1,10 @@
1
+ module Wechat
2
+ #
3
+ module Qy
4
+ end
5
+ end
6
+
7
+ require 'wechat/qy/client'
8
+ require 'wechat/qy/message'
9
+
10
+ ::Wechat::Qy::Client.send :include, ::Wechat::Qy::Message
@@ -0,0 +1,117 @@
1
+ require 'multi_json'
2
+ require 'faraday'
3
+ require 'logger'
4
+
5
+ module Wechat
6
+ module Qy
7
+ #
8
+ class ResponseError < StandardError; end
9
+ class AccessTokenExpiredError < ResponseError; end
10
+ #
11
+ class Client
12
+
13
+ API_BASE = 'https://qyapi.weixin.qq.com/cgi-bin/'
14
+
15
+ attr_reader :corp_id, :corp_secret
16
+ attr_accessor :logger
17
+
18
+ def initialize(corp_id, corp_secret)
19
+ @corp_id, @corp_secret = corp_id, corp_secret
20
+ @logger = Logger.new(STDOUT)
21
+ @token_file = File.join('/tmp', "wechat-api-#{corp_id}")
22
+ end
23
+
24
+ def access_token
25
+ @access_token ||= begin
26
+ token = MultiJson.load(File.read(@token_file))
27
+ token['access_token']
28
+ rescue
29
+ refresh
30
+ end
31
+ end
32
+
33
+ def refresh
34
+ url = format('%sgettoken', API_BASE)
35
+ resp = connection.get(url, token_params)
36
+ response = MultiJson.load(resp.body)
37
+ return handle_error(response) if response['errcode']
38
+ @access_token = response['access_token']
39
+ File.open(@token_file, 'w') { |f| f.write(resp.body) } if @access_token
40
+ @access_token
41
+ end
42
+
43
+ def get(uri, params = {})
44
+ with_access_token(uri, params) do |url, params_with_token|
45
+ debug_request do
46
+ connection.get do |req|
47
+ req.url url, params_with_token
48
+ req.headers[:accept] = 'application/json'
49
+ req.headers[:content_type] = 'application/json'
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ def post(uri, data = {})
56
+ with_access_token(uri, {}) do |url, params|
57
+ logger.debug { [:data, data] }
58
+ debug_request do
59
+ connection.post do |req|
60
+ req.url url, params
61
+ req.headers[:accept] = 'application/json'
62
+ req.headers[:content_type] = 'application/json'
63
+ req.body = MultiJson.dump(data)
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ def with_access_token(uri, params, tried = 2)
70
+ url = format('%s%s', API_BASE, uri)
71
+ begin
72
+ resp = yield(url, params.merge(access_token: access_token))
73
+ response = MultiJson.load(resp.body)
74
+ handle_error(response)
75
+ rescue AccessTokenExpiredError => e
76
+ refresh
77
+ retry unless (tried -= 1).zero?
78
+ raise e
79
+ end
80
+ end
81
+
82
+ private
83
+
84
+ def debug_request
85
+ response = yield
86
+ logger.debug { response }
87
+ response
88
+ end
89
+
90
+ def handle_error(response)
91
+ case response['errcode']
92
+ when 0, nil
93
+ response
94
+ when 40_001, 42_001, 40_014
95
+ fail AccessTokenExpiredError, response
96
+ else
97
+ fail ResponseError, response
98
+ end
99
+ end
100
+
101
+ def token_params
102
+ {
103
+ corpid: corp_id,
104
+ corpsecret: corp_secret
105
+ }
106
+ end
107
+
108
+ def connection
109
+ @connection ||= begin
110
+ Faraday.new do |faraday|
111
+ faraday.adapter Faraday.default_adapter
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,16 @@
1
+ module Wechat
2
+ module Qy
3
+ module Message
4
+ # text_send('appid', 'hello', touser: 'User1|User2')
5
+ def text_send(agent_id, content, data = {})
6
+ params = {
7
+ msgtype: :text,
8
+ agentid: agent_id,
9
+ safe: 0,
10
+ text: { content: content }
11
+ }.merge(data)
12
+ post 'message/send', params
13
+ end
14
+ end
15
+ end
16
+ end
@@ -17,11 +17,15 @@ Gem::Specification.new do |spec|
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ["lib"]
19
19
 
20
- spec.add_runtime_dependency "faraday", "~> 0.9"
21
- spec.add_runtime_dependency "multi_json", "~> 1.2"
22
- spec.add_development_dependency "bundler"
23
- spec.add_development_dependency "rake"
24
- spec.add_development_dependency "pry"
25
- spec.add_development_dependency "rspec", "~> 3.0"
26
- spec.add_development_dependency "webmock", "~> 1.18"
20
+ spec.add_runtime_dependency 'faraday', '~> 1.0'
21
+ spec.add_runtime_dependency 'multi_json', '~> 1.0'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.0'
24
+ spec.add_development_dependency 'guard', '~> 2.0'
25
+ spec.add_development_dependency 'guard-rspec', '~> 4.0'
26
+ spec.add_development_dependency 'rake', '~> 13.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ spec.add_development_dependency 'rspec-its', '~> 1.0'
29
+ spec.add_development_dependency 'webmock', '~> 3.0'
30
+
27
31
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wechat-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Wong
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-24 00:00:00.000000000 Z
11
+ date: 2020-12-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -16,70 +16,84 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.9'
19
+ version: '1.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.9'
26
+ version: '1.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: multi_json
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.2'
33
+ version: '1.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.2'
40
+ version: '1.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '1.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: '1.0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: rake
56
+ name: guard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard-rspec
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
- - - ">="
73
+ - - "~>"
60
74
  - !ruby/object:Gem::Version
61
- version: '0'
75
+ version: '4.0'
62
76
  type: :development
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
- - - ">="
80
+ - - "~>"
67
81
  - !ruby/object:Gem::Version
68
- version: '0'
82
+ version: '4.0'
69
83
  - !ruby/object:Gem::Dependency
70
- name: pry
84
+ name: rake
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
- - - ">="
87
+ - - "~>"
74
88
  - !ruby/object:Gem::Version
75
- version: '0'
89
+ version: '13.0'
76
90
  type: :development
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
- - - ">="
94
+ - - "~>"
81
95
  - !ruby/object:Gem::Version
82
- version: '0'
96
+ version: '13.0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: rspec
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -94,32 +108,47 @@ dependencies:
94
108
  - - "~>"
95
109
  - !ruby/object:Gem::Version
96
110
  version: '3.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec-its
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.0'
97
125
  - !ruby/object:Gem::Dependency
98
126
  name: webmock
99
127
  requirement: !ruby/object:Gem::Requirement
100
128
  requirements:
101
129
  - - "~>"
102
130
  - !ruby/object:Gem::Version
103
- version: '1.18'
131
+ version: '3.0'
104
132
  type: :development
105
133
  prerelease: false
106
134
  version_requirements: !ruby/object:Gem::Requirement
107
135
  requirements:
108
136
  - - "~>"
109
137
  - !ruby/object:Gem::Version
110
- version: '1.18'
111
- description:
138
+ version: '3.0'
139
+ description:
112
140
  email:
113
141
  - lazing@gmail.com
114
142
  executables: []
115
143
  extensions: []
116
144
  extra_rdoc_files: []
117
145
  files:
146
+ - ".github/workflows/rake_spec.yml"
118
147
  - ".gitignore"
119
148
  - ".rspec"
120
- - ".travis.yml"
121
149
  - Gemfile
122
150
  - Guardfile
151
+ - README.md
123
152
  - Rakefile
124
153
  - lib/wechat-api.rb
125
154
  - lib/wechat/api.rb
@@ -128,6 +157,9 @@ files:
128
157
  - lib/wechat/api/user.rb
129
158
  - lib/wechat/api/util.rb
130
159
  - lib/wechat/api/version.rb
160
+ - lib/wechat/qy.rb
161
+ - lib/wechat/qy/client.rb
162
+ - lib/wechat/qy/message.rb
131
163
  - spec/spec_helper.rb
132
164
  - spec/wechat/api/client_spec.rb
133
165
  - wechat-api.gemspec
@@ -135,7 +167,7 @@ homepage: https://github.com/lazing/wechat-api
135
167
  licenses:
136
168
  - MIT
137
169
  metadata: {}
138
- post_install_message:
170
+ post_install_message:
139
171
  rdoc_options: []
140
172
  require_paths:
141
173
  - lib
@@ -150,12 +182,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
182
  - !ruby/object:Gem::Version
151
183
  version: '0'
152
184
  requirements: []
153
- rubyforge_project:
154
- rubygems_version: 2.2.2
155
- signing_key:
185
+ rubygems_version: 3.0.6
186
+ signing_key:
156
187
  specification_version: 4
157
188
  summary: Wechat API wrapper
158
189
  test_files:
159
190
  - spec/spec_helper.rb
160
191
  - spec/wechat/api/client_spec.rb
161
- has_rdoc:
@@ -1,5 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - ruby-head
4
- - 2.1.0
5
- - 1.9.3