wework-next 1.2.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.travis.yml +5 -0
  4. data/CODE_OF_CONDUCT.md +74 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +184 -0
  8. data/Rakefile +10 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/lib/wework/api/agent.rb +39 -0
  12. data/lib/wework/api/base.rb +94 -0
  13. data/lib/wework/api/contact.rb +7 -0
  14. data/lib/wework/api/corp.rb +27 -0
  15. data/lib/wework/api/methods/agent.rb +63 -0
  16. data/lib/wework/api/methods/appchat.rb +62 -0
  17. data/lib/wework/api/methods/approval.rb +16 -0
  18. data/lib/wework/api/methods/batch.rb +38 -0
  19. data/lib/wework/api/methods/checkin.rb +19 -0
  20. data/lib/wework/api/methods/crm.rb +11 -0
  21. data/lib/wework/api/methods/department.rb +36 -0
  22. data/lib/wework/api/methods/media.rb +20 -0
  23. data/lib/wework/api/methods/menu.rb +21 -0
  24. data/lib/wework/api/methods/message.rb +51 -0
  25. data/lib/wework/api/methods/provider.rb +38 -0
  26. data/lib/wework/api/methods/service.rb +53 -0
  27. data/lib/wework/api/methods/tag.rb +37 -0
  28. data/lib/wework/api/methods/user.rb +55 -0
  29. data/lib/wework/api/provider.rb +28 -0
  30. data/lib/wework/api/suite.rb +48 -0
  31. data/lib/wework/cipher.rb +98 -0
  32. data/lib/wework/config.rb +28 -0
  33. data/lib/wework/global_code.rb +327 -0
  34. data/lib/wework/mock_api.rb +34 -0
  35. data/lib/wework/request.rb +117 -0
  36. data/lib/wework/token/app_token.rb +21 -0
  37. data/lib/wework/token/base.rb +59 -0
  38. data/lib/wework/token/corp_token.rb +20 -0
  39. data/lib/wework/token/js_agent_ticket.rb +23 -0
  40. data/lib/wework/token/js_ticket.rb +23 -0
  41. data/lib/wework/token/provider_token.rb +21 -0
  42. data/lib/wework/token/suite_token.rb +21 -0
  43. data/lib/wework/version.rb +3 -0
  44. data/lib/wework.rb +39 -0
  45. data/mock_responses/agent/get.json +29 -0
  46. data/mock_responses/agent/list.json +16 -0
  47. data/mock_responses/agent/set.json +4 -0
  48. data/mock_responses/batch/getresult.json +8 -0
  49. data/mock_responses/department/list.json +24 -0
  50. data/mock_responses/error.json +4 -0
  51. data/mock_responses/files/party.csv +12 -0
  52. data/mock_responses/files/sample.amr +0 -0
  53. data/mock_responses/files/sample.mp4 +0 -0
  54. data/mock_responses/files/sample.txt +1 -0
  55. data/mock_responses/files/user.csv +2 -0
  56. data/mock_responses/files/zhiren.png +0 -0
  57. data/mock_responses/get_jsapi_ticket.json +6 -0
  58. data/mock_responses/gettoken.json +6 -0
  59. data/mock_responses/menu/get.json +24 -0
  60. data/mock_responses/service/get.json +0 -0
  61. data/mock_responses/service/get_corp_token.json +4 -0
  62. data/mock_responses/service/get_login_info.json +34 -0
  63. data/mock_responses/service/get_permanent_code.json +54 -0
  64. data/mock_responses/service/get_pre_auth_code.json +6 -0
  65. data/mock_responses/service/get_provider_token.json +4 -0
  66. data/mock_responses/service/get_suite_token.json +4 -0
  67. data/mock_responses/success.json +4 -0
  68. data/mock_responses/user/convert_to_openid.json +6 -0
  69. data/mock_responses/user/convert_to_userid.json +5 -0
  70. data/mock_responses/user/get.json +18 -0
  71. data/mock_responses/user/getuserdetail.json +10 -0
  72. data/mock_responses/user/getuserinfo.json +8 -0
  73. data/mock_responses/user/list.json +22 -0
  74. data/mock_responses/user/simplelist.json +11 -0
  75. data/wework.gemspec +33 -0
  76. metadata +243 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8c4fdc61a04f5bae81bd178df0e255d8f3f176435d285b1e5c976f7a99750f2f
4
+ data.tar.gz: da7f4e91b91eb3c556dcf95f04c18f978bf14d9c9dd7680053f54088643523d8
5
+ SHA512:
6
+ metadata.gz: 6509f750bdf7a79ac1acb10a152c2e865587b4130a2d52e350c71de37b3370759919ae39c4c9a2a4e1482e99182d700d270327245cb29096bb5113abfd13a875
7
+ data.tar.gz: 6ad27f6ed7bf09e6f9f24985809113e0259993c7b54ea6d2e56cafdb9a1aa7d236ccd14007a70000395d5ef56db53525b90d5f2f806bd6b6d71ff5368ce4867e
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
11
+ .DS_Store
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.3
5
+ before_install: gem install bundler -v 1.13.6
@@ -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 sindon@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
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in wework.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 seandong
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,184 @@
1
+ # Wechat Work 「企业微信」
2
+
3
+ Wework is a ruby API wrapper for wechat work「企业微信」.
4
+
5
+ [企业微信 API 文档](https://work.weixin.qq.com/api/doc)
6
+
7
+ [![CircleCI](https://circleci.com/gh/mycolorway/wework/tree/suite.svg?style=svg)](https://circleci.com/gh/mycolorway/wework/tree/suite) [![Gem Version](https://badge.fury.io/rb/wework.svg)](https://badge.fury.io/rb/wework)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'wework'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install wework
24
+
25
+ ## Usage
26
+
27
+ ### 初始化
28
+
29
+ ```
30
+ Wework.configure do |config|
31
+ config.redis = Redis.new(host: ENV['REDIS_HOST'], port: ENV['REDIS_PORT'], db: ENV['REDIS_CACHE_DB']) # redis
32
+ config.http_timeout_options = {write: 15, read: 15, connect: 10} # 请求超时
33
+ config.expired_shift_seconds = 180 # access token 有效期偏移量 (时间有效期 - 偏移量)
34
+ end
35
+ ```
36
+
37
+ ### 第三方应用
38
+
39
+ 1). 初始化套件接口 [Wework::Api::Suite](https://github.com/mycolorway/wework/blob/master/lib/wework/api/suite.rb)
40
+
41
+ ```ruby
42
+ $wework_suite = Wework::Api::Suite.new(
43
+ corp_id: ENV['WEWORK_CORPID'], # 应用服务商 CorpID
44
+ suite_id: ENV['WEWORK_SUITE_ID'], # SuiteID
45
+ encoding_aes_key: ENV['WEWORK_SUITE_ENCODING_AES_KEY'], # EncodingAESKey
46
+ suite_token: ENV['WEWORK_SUITE_TOKEN'], # Token
47
+ suite_secret: ENV['WEWORK_SUITE_SECRET'] # Secret
48
+ )
49
+ ```
50
+
51
+
52
+ 2). 获取第三方应用接口 [Wework::Api::Agent](https://github.com/mycolorway/wework/blob/master/lib/wework/api/agent.rb)
53
+
54
+ ```ruby
55
+ agent_api = $wework_suite.corp(
56
+ corp_id, # 授权企业 CorpID
57
+ permanent_code # 永久授权码
58
+ ).agent(agent_id)
59
+ ```
60
+
61
+ 3). 第三方应用回调协议
62
+
63
+ 获取加密数据
64
+
65
+ ```ruby
66
+ encrypt_str = if params[:xml].present?
67
+ params[:xml]['Encrypt']
68
+ else
69
+ Hash.from_xml(request.raw_post)['xml']['Encrypt']
70
+ end
71
+
72
+ #首次校验接口 encrpt_str = params[:echostr]
73
+ ```
74
+
75
+ 消息签名
76
+
77
+ ```ruby
78
+ $wework_suite.signature(timestamp, nonce, encrypt_str)
79
+ ```
80
+
81
+ 消息解密
82
+
83
+ ```ruby
84
+ $wework_suite.msg_decrypt(encrypt_data)
85
+ ```
86
+
87
+
88
+ ### 自建应用
89
+
90
+ 初始化接口 [Wework::Api::Agent](https://github.com/mycolorway/wework/blob/master/lib/wework/api/agent.rb)
91
+
92
+ ``` ruby
93
+ $agent_api = Wework::Api::Agent.new(
94
+ corp_id: ENV['WEWORK_CORPID'], # 企业 CorpID
95
+ agent_id: ENV['WEWORK_APPID'], # AgentId
96
+ secret: ENV['DEMO_APP_SECRET'] # Secret
97
+ )
98
+ ```
99
+
100
+ ### 通讯录同步
101
+
102
+ 初始化接口 [Wework::Api::Contact](https://github.com/mycolorway/wework/blob/master/lib/wework/api/contact.rb)
103
+
104
+ ``` ruby
105
+ $contact_api = Wework::Api::Contact.new(
106
+ corp_id: ENV['WEWORK_CORPID'], # 企业 CorpID
107
+ secret: ENV['DEMO_APP_SECRET'] # 通讯录同步 Secret
108
+ )
109
+ ```
110
+
111
+ ### 应用服务商接口
112
+
113
+ 初始化
114
+
115
+ ``` ruby
116
+ $provider_api = Wework::Api::Provider.new(
117
+ corp_id: ENV['WEWORK_PROVIDER_CORPID'], # 应用服务商 CorpID
118
+ secret: ENV['WEWORK_PROVIDER_SECRET'], # Secret
119
+ token: ENV['WEWORK_PROVIDER_TOKEN'],
120
+ encoding_aes_key: ENV['WEWORK_PROVIDER_ENCODING_AES_KEY']
121
+ )
122
+ ```
123
+
124
+ 回调接口消息加解密参考「第三方应用回调协议」
125
+
126
+
127
+ ### 小程序 encryptedData 解密
128
+
129
+ ```ruby
130
+ require "openssl"
131
+ CIPHER = 'AES-128-CBC'.freeze
132
+
133
+ cipher = OpenSSL::Cipher.new(CIPHER)
134
+ cipher.decrypt
135
+ cipher.key = Base64.decode64(session_key)
136
+ cipher.iv = Base64.decode64(iv)
137
+ encrypted = Base64.decode64(encryptedData)
138
+ data = cipher.update(encrypted) + cipher.final
139
+ values = JSON.parse data
140
+ ```
141
+
142
+
143
+ ## 已实现接口列表
144
+
145
+ [Wework::Api::Methods::Agent](https://github.com/mycolorway/wework/blob/master/lib/wework/api/methods/agent.rb)
146
+
147
+ [Wework::Api::Methods::User](https://github.com/mycolorway/wework/blob/master/lib/wework/api/methods/user.rb)
148
+
149
+ [Wework::Api::Methods::Tag](https://github.com/mycolorway/wework/blob/master/lib/wework/api/methods/tag.rb)
150
+
151
+ [Wework::Api::Methods::Department](https://github.com/mycolorway/wework/blob/master/lib/wework/api/methods/department.rb)
152
+
153
+ [Wework::Api::Methods::Message](https://github.com/mycolorway/wework/blob/master/lib/wework/api/methods/message.rb)
154
+
155
+ [Wework::Api::Methods::Appchat](https://github.com/mycolorway/wework/blob/master/lib/wework/api/methods/appchat.rb)
156
+
157
+ [Wework::Api::Methods::Media](https://github.com/mycolorway/wework/blob/master/lib/wework/api/methods/media.rb)
158
+
159
+ [Wework::Api::Methods::Menu](https://github.com/mycolorway/wework/blob/master/lib/wework/api/methods/menu.rb)
160
+
161
+ [Wework::Api::Methods::Approval](https://github.com/mycolorway/wework/blob/master/lib/wework/api/methods/approval.rb)
162
+
163
+ [Wework::Api::Methods::Checkin](https://github.com/mycolorway/wework/blob/master/lib/wework/api/methods/checkin.rb)
164
+
165
+ [Wework::Api::Methods::Batch](https://github.com/mycolorway/wework/blob/master/lib/wework/api/methods/batch.rb)
166
+
167
+ [Wework::Api::Methods::Provider](https://github.com/mycolorway/wework/blob/master/lib/wework/api/methods/provider.rb)
168
+
169
+ [Wework::Api::Methods::Service](https://github.com/mycolorway/wework/blob/master/lib/wework/api/methods/service.rb)
170
+
171
+
172
+ ## Contributing
173
+
174
+ * Fork `Wework` on GitHub
175
+ * Make your changes
176
+ * Ensure all tests pass (`bundle exec rake`)
177
+ * Send a pull request
178
+ * If we like them we'll merge them
179
+ * If we've accepted a patch, feel free to ask for commit access!
180
+
181
+ ## License
182
+
183
+ Copyright (c) 2018 MyColorway. See LICENSE.txt for further details.
184
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "wework"
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
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,39 @@
1
+ module Wework
2
+ module Api
3
+ class Agent < Base
4
+
5
+ include Methods::Agent
6
+ include Methods::Message
7
+ include Methods::Appchat
8
+ include Methods::Menu
9
+ include Methods::Checkin
10
+ include Methods::Approval
11
+
12
+ attr_reader :agent_id
13
+
14
+ def initialize(options={})
15
+ @agent_id = options.delete(:agent_id)
16
+ @agent_id = @agent_id.to_i if @agent_id.to_s =~ /\A\d+\Z/
17
+ super(options)
18
+ end
19
+
20
+ def jsapi_ticket
21
+ jsticket_store.ticket
22
+ end
23
+
24
+ def jsapi_agent_ticket
25
+ jsticket_agent_store.ticket
26
+ end
27
+
28
+ private
29
+
30
+ def jsticket_store
31
+ @jsticket_store ||= Token::JsTicket.new self
32
+ end
33
+
34
+ def jsticket_agent_store
35
+ @jsticket_agent_store ||= Token::JsAgentTicket.new self
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,94 @@
1
+ require 'wework/request'
2
+ require 'wework/api/methods/media'
3
+ require 'wework/api/methods/user'
4
+ require 'wework/api/methods/agent'
5
+ require 'wework/api/methods/department'
6
+ require 'wework/api/methods/tag'
7
+ require 'wework/api/methods/crm'
8
+ require 'wework/api/methods/menu'
9
+ require 'wework/api/methods/checkin'
10
+ require 'wework/api/methods/message'
11
+ require 'wework/api/methods/service'
12
+ require 'wework/api/methods/provider'
13
+ require 'wework/api/methods/batch'
14
+ require 'wework/api/methods/approval'
15
+ require 'wework/api/methods/appchat'
16
+ require 'wework/token/base'
17
+ require 'wework/token/app_token'
18
+ module Wework
19
+ module Api
20
+ class Base
21
+
22
+ include Methods::Media
23
+ include Methods::User
24
+ include Methods::Department
25
+ include Methods::Tag
26
+ include Methods::Crm
27
+ include Methods::Batch
28
+
29
+ attr_accessor :corp_id, :secret, :options
30
+
31
+ def initialize options={}
32
+ @corp_id = options.delete(:corp_id)
33
+ @secret = options.delete(:secret)
34
+ @token_store = options.delete(:token_store)
35
+ @options = options
36
+ end
37
+
38
+ def request
39
+ @request ||= Wework::Request.new(API_ENDPOINT, true)
40
+ end
41
+
42
+ def valid?
43
+ return false if corp_id.nil?
44
+ token_store.token.present?
45
+ rescue AccessTokenExpiredError
46
+ false
47
+ rescue => e
48
+ Rails.logger.error "[WEWORK] (valid?) fetch access token error(#{corp_id}): #{e.inspect}" if defined?(Rails)
49
+ false
50
+ end
51
+
52
+ def get(path, headers = {})
53
+ with_token(headers[:params]) do |params|
54
+ request.get path, headers.merge(params: params)
55
+ end
56
+ end
57
+
58
+ def post(path, payload, headers = {})
59
+ with_token(headers[:params]) do |params|
60
+ request.post path, payload, headers.merge(params: params)
61
+ end
62
+ end
63
+
64
+ def post_file(path, file, headers = {})
65
+ with_token(headers[:params]) do |params|
66
+ request.post_file path, file, headers.merge(params: params)
67
+ end
68
+ end
69
+
70
+ def access_token
71
+ token_store.token
72
+ end
73
+
74
+ private
75
+
76
+ def with_token(params = {}, tries = 2)
77
+ params ||= {}
78
+ yield(params.merge(token_params))
79
+ rescue AccessTokenExpiredError
80
+ token_store.update_token
81
+ retry unless (tries -= 1).zero?
82
+ end
83
+
84
+ def token_store
85
+ @token_store ||= Token::AppToken.new self
86
+ end
87
+
88
+ def token_params
89
+ {access_token: access_token}
90
+ end
91
+
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,7 @@
1
+ require 'wework/api/base'
2
+ module Wework
3
+ module Api
4
+ class Contact < Base
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,27 @@
1
+ module Wework
2
+ module Api
3
+ class Corp < Base
4
+
5
+ include Wework::Cipher
6
+
7
+ attr_reader :suite, :permanent_code
8
+
9
+ def initialize(options={})
10
+ @suite = options.delete(:suite)
11
+ @permanent_code = options.delete(:permanent_code)
12
+ super(options)
13
+ end
14
+
15
+ def agent(agent_id)
16
+ Wework::Api::Agent.new(corp_id: corp_id, agent_id: agent_id, token_store: token_store)
17
+ end
18
+
19
+ private
20
+
21
+ def token_store
22
+ @token_store ||= Token::CorpToken.new self
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,63 @@
1
+ require "erb"
2
+
3
+ module Wework
4
+ module Api
5
+ module Methods
6
+ module Agent
7
+ def authorize_url(redirect_uri, scope="snsapi_base", state="wxwork")
8
+ # user agent: UA is mozilla/5.0 (iphone; cpu iphone os 10_2 like mac os x) applewebkit/602.3.12 (khtml, like gecko) mobile/14c92 wxwork/1.3.2 micromessenger/6.2
9
+ uri = ERB::Util.url_encode(redirect_uri)
10
+ "#{AUTHORIZE_ENDPOINT}?appid=#{corp_id}&redirect_uri=#{uri}&response_type=code&scope=#{scope}&agentid=#{agent_id}&state=#{state}#wechat_redirect"
11
+ end
12
+
13
+ def get_oauth_userinfo code
14
+ get 'user/getuserinfo', params: {code: code}
15
+ end
16
+
17
+ def get_user_detail user_ticket
18
+ post 'user/getuserdetail', {user_ticket: user_ticket}
19
+ end
20
+
21
+ def get_jssign_package url
22
+ timestamp = Time.now.to_i
23
+ noncestr = SecureRandom.hex(8)
24
+ str = "jsapi_ticket=#{jsapi_ticket}&noncestr=#{noncestr}&timestamp=#{timestamp}&url=#{url}"
25
+ {
26
+ "appId" => corp_id,
27
+ "nonceStr" => noncestr,
28
+ "timestamp" => timestamp,
29
+ "url" => url,
30
+ "signature" => Digest::SHA1.hexdigest(str),
31
+ "rawString" => str
32
+ }
33
+ end
34
+
35
+ def get_agent_jssign_package url
36
+ timestamp = Time.now.to_i
37
+ noncestr = SecureRandom.hex(8)
38
+ str = "jsapi_ticket=#{jsapi_agent_ticket}&noncestr=#{noncestr}&timestamp=#{timestamp}&url=#{url}"
39
+ {
40
+ "appId" => corp_id,
41
+ "nonceStr" => noncestr,
42
+ "timestamp" => timestamp,
43
+ "url" => url,
44
+ "signature" => Digest::SHA1.hexdigest(str),
45
+ "rawString" => str
46
+ }
47
+ end
48
+
49
+ def get_session_with_jscode(js_code, grant_type='authorization_code')
50
+ post 'miniprogram/jscode2session', {}, params: {js_code: js_code, grant_type: grant_type}
51
+ end
52
+
53
+ def get_agent
54
+ get 'agent/get', params: {agentid: agent_id}
55
+ end
56
+
57
+ def set_agent data={}
58
+ post 'agent/set', data.merge(agentid: agent_id)
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,62 @@
1
+ require "erb"
2
+
3
+ module Wework
4
+ module Api
5
+ module Methods
6
+ module Appchat
7
+
8
+ def appchat_create group_name, owner_id, group_user_ids, chat_id
9
+ post 'appchat/create',
10
+ name: group_name,
11
+ owner: owner_id,
12
+ userlist: group_user_ids,
13
+ chatid: chat_id
14
+ end
15
+
16
+ def appchat_update chat_id, payload={}
17
+ payload.merge! chatid: chat_id
18
+ post 'appchat/update', payload
19
+ end
20
+
21
+ def appchat_get chat_id
22
+ get 'appchat/get', { params: { chatid: chat_id } }
23
+ end
24
+
25
+ def text_appchat_send chat_id, content
26
+ appchat_send chat_id, {text: {content: content}, msgtype: 'text'}
27
+ end
28
+
29
+ def image_appchat_send chat_id, media_id
30
+ appchat_send chat_id, {image: {media_id: media_id}, msgtype: 'image'}
31
+ end
32
+
33
+ def voice_appchat_send chat_id, media_id
34
+ appchat_send chat_id, {voice: {media_id: media_id}, msgtype: 'voice'}
35
+ end
36
+
37
+ def file_appchat_send chat_id, media_id
38
+ appchat_send chat_id, {file: {media_id: media_id}, msgtype: 'file'}
39
+ end
40
+
41
+ def video_appchat_send chat_id, video={}
42
+ appchat_send chat_id, {video: video, msgtype: 'video'}
43
+ end
44
+
45
+ def textcard_appchat_send chat_id, textcard={}
46
+ appchat_send chat_id, {textcard: textcard, msgtype: 'textcard'}
47
+ end
48
+
49
+ def news_appchat_send chat_id, news=[]
50
+ appchat_send chat_id, {news: {articles: news}, msgtype: 'news'}
51
+ end
52
+
53
+ private
54
+
55
+ def appchat_send chat_id, payload={}
56
+ payload.merge!(chatid: chat_id)
57
+ post 'appchat/send', payload
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,16 @@
1
+ require "erb"
2
+
3
+ module Wework
4
+ module Api
5
+ module Methods
6
+ module Approval
7
+ def get_approval_data start_time, end_time, next_spnum=nil
8
+ # https://work.weixin.qq.com/api/doc#11228
9
+ payload = {starttime: start_time, endtime: end_time}
10
+ payload[:next_spnum] = next_spnum unless next_spnum.nil?
11
+ post 'corp/getapprovaldata', payload
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end