qywx-client 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rubocop.yml +20 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +89 -0
- data/README.md +72 -0
- data/Rakefile +4 -0
- data/_config.yml +1 -0
- data/bin/console +16 -0
- data/bin/setup +8 -0
- data/lib/qywx/client/configurable.rb +55 -0
- data/lib/qywx/client/errors.rb +26 -0
- data/lib/qywx/client/group_robot_client/core.rb +36 -0
- data/lib/qywx/client/group_robot_client/notify_feedcard.rb +38 -0
- data/lib/qywx/client/group_robot_client/notify_markdown.rb +51 -0
- data/lib/qywx/client/group_robot_client/notify_text.rb +45 -0
- data/lib/qywx/client/group_robot_client.rb +21 -0
- data/lib/qywx/client/result.rb +25 -0
- data/lib/qywx/client/version.rb +7 -0
- data/lib/qywx/client.rb +19 -0
- data/qywx-client.gemspec +30 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: dc4278cbbc1fd3145fc2f785de68674a9cf82fc7b07bcdcb6f46358c8ebe16dd
|
4
|
+
data.tar.gz: cef28e5b72560973ae369492f41325e0de100cabae32066e8f2c69a864f74a10
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 930bb2b1cd3dc445a0ba6a71e7b25bda9ba1d29173b7a7f33517fa64cded7bae4c284721f543e1685f288d9e4cea578300967d5e37063b9304fb06c914c0e496
|
7
|
+
data.tar.gz: b76a1fc911a21e593832a7691c8a7572f183488afc3806dd856b1e3c2b2bbd7bcccda81810e5216089d04ead48025f4ee62a7c5624c3efd80cca9a917156cd28
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.5
|
3
|
+
Metrics/LineLength:
|
4
|
+
Max: 120
|
5
|
+
Metrics/MethodLength:
|
6
|
+
Max: 30 # default * 3
|
7
|
+
Metrics/AbcSize:
|
8
|
+
Max: 50 # default * 2
|
9
|
+
Metrics/ModuleLength:
|
10
|
+
Exclude:
|
11
|
+
- "**/*_spec.rb"
|
12
|
+
Metrics/BlockLength:
|
13
|
+
Exclude:
|
14
|
+
- "**/*_spec.rb"
|
15
|
+
Style/BlockDelimiters:
|
16
|
+
EnforcedStyle: braces_for_chaining
|
17
|
+
Style/Documentation:
|
18
|
+
Enabled: false
|
19
|
+
Style/ModuleFunction:
|
20
|
+
EnforcedStyle: extend_self
|
data/.travis.yml
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 pinewong@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
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
6
|
+
|
7
|
+
# Specify your gem's dependencies in active_notifier.gemspec
|
8
|
+
gemspec
|
9
|
+
|
10
|
+
gem 'byebug'
|
11
|
+
gem 'codecov', '>= 0.1.10', require: false
|
12
|
+
gem 'rake', '~> 10.0'
|
13
|
+
gem 'rspec', '~> 3.7', require: false
|
14
|
+
gem 'rubocop', '~> 0.66.0', require: false
|
15
|
+
gem 'simplecov', require: false
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
qywx-client (0.2.1)
|
5
|
+
activesupport (~> 5.2.1)
|
6
|
+
httparty (~> 0.16.2)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activesupport (5.2.6.3)
|
12
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
13
|
+
i18n (>= 0.7, < 2)
|
14
|
+
minitest (~> 5.1)
|
15
|
+
tzinfo (~> 1.1)
|
16
|
+
ast (2.4.2)
|
17
|
+
byebug (10.0.2)
|
18
|
+
codecov (0.6.0)
|
19
|
+
simplecov (>= 0.15, < 0.22)
|
20
|
+
concurrent-ruby (1.1.9)
|
21
|
+
diff-lcs (1.3)
|
22
|
+
docile (1.3.1)
|
23
|
+
httparty (0.16.4)
|
24
|
+
mime-types (~> 3.0)
|
25
|
+
multi_xml (>= 0.5.2)
|
26
|
+
i18n (1.10.0)
|
27
|
+
concurrent-ruby (~> 1.0)
|
28
|
+
jaro_winkler (1.5.4)
|
29
|
+
json (2.1.0)
|
30
|
+
mime-types (3.4.1)
|
31
|
+
mime-types-data (~> 3.2015)
|
32
|
+
mime-types-data (3.2022.0105)
|
33
|
+
minitest (5.15.0)
|
34
|
+
multi_xml (0.6.0)
|
35
|
+
parallel (1.21.0)
|
36
|
+
parser (3.1.1.0)
|
37
|
+
ast (~> 2.4.1)
|
38
|
+
psych (4.0.3)
|
39
|
+
stringio
|
40
|
+
rainbow (3.1.1)
|
41
|
+
rake (10.5.0)
|
42
|
+
rspec (3.8.0)
|
43
|
+
rspec-core (~> 3.8.0)
|
44
|
+
rspec-expectations (~> 3.8.0)
|
45
|
+
rspec-mocks (~> 3.8.0)
|
46
|
+
rspec-core (3.8.0)
|
47
|
+
rspec-support (~> 3.8.0)
|
48
|
+
rspec-expectations (3.8.1)
|
49
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
50
|
+
rspec-support (~> 3.8.0)
|
51
|
+
rspec-mocks (3.8.0)
|
52
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
53
|
+
rspec-support (~> 3.8.0)
|
54
|
+
rspec-support (3.8.0)
|
55
|
+
rubocop (0.66.0)
|
56
|
+
jaro_winkler (~> 1.5.1)
|
57
|
+
parallel (~> 1.10)
|
58
|
+
parser (>= 2.5, != 2.5.1.1)
|
59
|
+
psych (>= 3.1.0)
|
60
|
+
rainbow (>= 2.2.2, < 4.0)
|
61
|
+
ruby-progressbar (~> 1.7)
|
62
|
+
unicode-display_width (>= 1.4.0, < 1.6)
|
63
|
+
ruby-progressbar (1.11.0)
|
64
|
+
simplecov (0.16.1)
|
65
|
+
docile (~> 1.1)
|
66
|
+
json (>= 1.8, < 3)
|
67
|
+
simplecov-html (~> 0.10.0)
|
68
|
+
simplecov-html (0.10.2)
|
69
|
+
stringio (3.0.1)
|
70
|
+
thread_safe (0.3.6)
|
71
|
+
tzinfo (1.2.9)
|
72
|
+
thread_safe (~> 0.1)
|
73
|
+
unicode-display_width (1.5.0)
|
74
|
+
|
75
|
+
PLATFORMS
|
76
|
+
ruby
|
77
|
+
|
78
|
+
DEPENDENCIES
|
79
|
+
bundler (~> 1.16)
|
80
|
+
byebug
|
81
|
+
codecov (>= 0.1.10)
|
82
|
+
qywx-client!
|
83
|
+
rake (~> 10.0)
|
84
|
+
rspec (~> 3.7)
|
85
|
+
rubocop (~> 0.66.0)
|
86
|
+
simplecov
|
87
|
+
|
88
|
+
BUNDLED WITH
|
89
|
+
1.17.3
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# Qywx Client
|
2
|
+
|
3
|
+
A simple HTTP client wrapper for [Qywx](https://developer.work.weixin.qq.com/document/path/91770)
|
4
|
+
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/qywx-client.svg)](https://rubygems.org/gems/qywx-client)
|
6
|
+
[![Documentation](https://img.shields.io/badge/docs-YARD-blue.svg)](https://rubydoc.info/gems/qywx-client)
|
7
|
+
[![Build Status](https://api.travis-ci.org/songhuangcn/qywx-client.svg)](https://travis-ci.org/joeyl-ljy/qywx-client)
|
8
|
+
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'qywx-client'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
|
21
|
+
```shell
|
22
|
+
$ bundle
|
23
|
+
```
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
```shell
|
28
|
+
$ gem install qywx-client
|
29
|
+
```
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
### Group Robot Message
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
# Config api token and message template directory
|
37
|
+
Qywx::Client.configure do |config|
|
38
|
+
config.template_dir = '.'
|
39
|
+
config.group_robot_tokens = { order: 'TOKEN' }
|
40
|
+
end
|
41
|
+
system %q(echo '> <%= @message %>' > order.markdown.erb)
|
42
|
+
|
43
|
+
# Notify markdown type message
|
44
|
+
Qywx::Client.notify_markdown(:order, 'Title') { @message = 'Awesome message' }
|
45
|
+
|
46
|
+
# Notify feedcard type
|
47
|
+
links = [{ title: 'title1', url: '...', picurl: '...' }, { title: 'title2', url: '...', picurl: '...' }]
|
48
|
+
Qywx::Client.notify_feedcard(:order, links)
|
49
|
+
|
50
|
+
# Notify text type
|
51
|
+
Qywx::Client.notify_text(:order, 'message')
|
52
|
+
|
53
|
+
# Want use instance variables in exist environment?
|
54
|
+
class OrderController < ApplicationContrller
|
55
|
+
include Qywx::Client
|
56
|
+
|
57
|
+
def index
|
58
|
+
@message = 'Some message'
|
59
|
+
notify_markdown(:order, 'Title')
|
60
|
+
end
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
## Development
|
65
|
+
|
66
|
+
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.
|
67
|
+
|
68
|
+
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).
|
69
|
+
|
70
|
+
## License
|
71
|
+
|
72
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/_config.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
theme: jekyll-theme-cayman
|
data/bin/console
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'qywx/client'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require 'pry'
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
require 'byebug'
|
16
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Qywx
|
4
|
+
module Client
|
5
|
+
# @example Config single item
|
6
|
+
# Qywx::Client.group_robot_tokens = { default: 'TOKENXXXXXXXXXXX' }
|
7
|
+
# @example Config multiple items
|
8
|
+
# Qywx::Client.configure do |config|
|
9
|
+
# config.group_robot_tokens = { default: 'TOKENXXXXXXXBBXX' }
|
10
|
+
# config.group_robot_webhook_prefix = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key='
|
11
|
+
# end
|
12
|
+
module Configurable
|
13
|
+
def self.included(base)
|
14
|
+
base.extend ClassMethods
|
15
|
+
end
|
16
|
+
|
17
|
+
# @see Configurable
|
18
|
+
module ClassMethods
|
19
|
+
def configure
|
20
|
+
yield config
|
21
|
+
end
|
22
|
+
|
23
|
+
def config
|
24
|
+
@config ||= Configuration.new
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# @see Configurable
|
29
|
+
class Configuration
|
30
|
+
attr_writer :group_robot_tokens, :group_robot_webhook_prefix, :template_dir
|
31
|
+
attr_accessor :is_debugging
|
32
|
+
|
33
|
+
def initialize
|
34
|
+
@group_robot_webhook_prefix = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key='
|
35
|
+
@template_dir = '.'
|
36
|
+
@is_debugging = false
|
37
|
+
end
|
38
|
+
|
39
|
+
def group_robot_tokens
|
40
|
+
return @group_robot_tokens.symbolize_keys if @group_robot_tokens.is_a?(Hash)
|
41
|
+
|
42
|
+
raise ConfigurationError.new(:group_robot_tokens, @group_robot_tokens, Hash)
|
43
|
+
end
|
44
|
+
|
45
|
+
def group_robot_webhook_prefix
|
46
|
+
String(@group_robot_webhook_prefix)
|
47
|
+
end
|
48
|
+
|
49
|
+
def template_dir
|
50
|
+
String(@template_dir)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Qywx
|
4
|
+
module Client
|
5
|
+
# Basic exception
|
6
|
+
class Error < StandardError; end
|
7
|
+
|
8
|
+
# :nodoc:
|
9
|
+
class ConfigurationError < Error
|
10
|
+
attr_reader :key, :value, :valid_values
|
11
|
+
|
12
|
+
def initialize(key, value, valid_values)
|
13
|
+
@key = key
|
14
|
+
@valud = value
|
15
|
+
@valid_values = valid_values
|
16
|
+
valid_values_str = Array(valid_values).join(', ')
|
17
|
+
message = if value.blank?
|
18
|
+
"#{key} not configured or configured with blank value, valid_values: #{valid_values_str}"
|
19
|
+
else
|
20
|
+
"Configure #{key} with value #{value} failed, valid_values: #{valid_values_str}"
|
21
|
+
end
|
22
|
+
super(message)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Qywx
|
4
|
+
module Client
|
5
|
+
module GroupRobotClient
|
6
|
+
# Group Bobot core functions
|
7
|
+
module Core
|
8
|
+
private
|
9
|
+
|
10
|
+
# @param token_code [Symbol]
|
11
|
+
def fetch_token(token_code)
|
12
|
+
Qywx::Client.config.group_robot_tokens[token_code].tap do |token|
|
13
|
+
raise ArgumentError, "Not found token code: #{token_code}" unless token
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# @param token [String]
|
18
|
+
# @param body [Hash]
|
19
|
+
def notify(token, body)
|
20
|
+
response = GroupRobotClient.post(token, body: body.to_json)
|
21
|
+
if response.parsed_response.try(:[], 'errcode').try(:zero?)
|
22
|
+
Result.new(true)
|
23
|
+
else
|
24
|
+
message = <<-MESSAGE.gsub(/^\s+/, '')
|
25
|
+
HTTP Status: #{response.code} #{response.message}
|
26
|
+
Response Body: #{response.body}
|
27
|
+
MESSAGE
|
28
|
+
Result.new(false, message: message)
|
29
|
+
end
|
30
|
+
rescue StandardError => exception
|
31
|
+
Result.new(false, message: exception.inspect)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Qywx
|
4
|
+
module Client
|
5
|
+
module GroupRobotClient
|
6
|
+
module NotifyFeedcard
|
7
|
+
# @param code [Symbol] set token_code and template_file
|
8
|
+
# @param links [Array<Hash>, Hash]
|
9
|
+
def notify_feedcard(code, links)
|
10
|
+
token_code = code.to_sym
|
11
|
+
token = fetch_token(token_code)
|
12
|
+
links = Array.wrap(links).map do |link|
|
13
|
+
Hash(link).symbolize_keys.tap do |hash|
|
14
|
+
required_keys = %i[title url picurl]
|
15
|
+
keys_valid = required_keys.all? { |arg| hash[arg].present? }
|
16
|
+
raise ArgumentError, "links requires these keys present: #{required_keys.join(', ')}" unless keys_valid
|
17
|
+
end
|
18
|
+
end
|
19
|
+
body = get_feedcard_body(links)
|
20
|
+
notify(token, body)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
# @param links [Array<Hash>]
|
26
|
+
# @return [Hash]
|
27
|
+
def get_feedcard_body(links)
|
28
|
+
{
|
29
|
+
msgtype: 'news',
|
30
|
+
news: {
|
31
|
+
articles: links
|
32
|
+
}
|
33
|
+
}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Qywx
|
4
|
+
module Client
|
5
|
+
module GroupRobotClient
|
6
|
+
module NotifyMarkdown
|
7
|
+
# @param code [Symbol] set token_code and template_file
|
8
|
+
# @param title [String]
|
9
|
+
# @option options [String] template_file (code) without file suffix
|
10
|
+
# @yield a block which will execute in template locale environment
|
11
|
+
def notify_markdown(code, title, **options, &block)
|
12
|
+
options.with_defaults!(
|
13
|
+
template_file: code
|
14
|
+
)
|
15
|
+
token_code = code.to_sym
|
16
|
+
token = fetch_token(token_code)
|
17
|
+
title = String(title)
|
18
|
+
template_file = String(options[:template_file])
|
19
|
+
content = get_markdown_content(template_file, &block)
|
20
|
+
body = get_markdown_body(title, content)
|
21
|
+
notify(token, body)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
# @param template_file [String]
|
27
|
+
# @yield a block which will execute in template locale environment
|
28
|
+
def get_markdown_content(template_file, &block)
|
29
|
+
instance_eval(&block) if block
|
30
|
+
path = "#{Qywx::Client.config.template_dir}/#{template_file}.markdown.erb"
|
31
|
+
ERB.new(File.read(path)).result(binding)
|
32
|
+
rescue Errno::ENOENT
|
33
|
+
raise ArgumentError, "Not found template: #{File.expand_path(path)}"
|
34
|
+
end
|
35
|
+
|
36
|
+
# @param title [String]
|
37
|
+
# @param content [String]
|
38
|
+
# @return [Hash]
|
39
|
+
def get_markdown_body(title, content)
|
40
|
+
{
|
41
|
+
msgtype: 'markdown',
|
42
|
+
markdown: {
|
43
|
+
title: title,
|
44
|
+
content: content
|
45
|
+
}
|
46
|
+
}
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Qywx
|
4
|
+
module Client
|
5
|
+
module GroupRobotClient
|
6
|
+
module NotifyText
|
7
|
+
# @param code [Symbol] set token_code and template_file
|
8
|
+
# @param content [String]
|
9
|
+
# @option options [String] template_file (code) without file suffix
|
10
|
+
# @option options [Array] at_users (["wangqing","@all"])
|
11
|
+
# @option options [Array] at_mobiles (["13800001111","@all"])
|
12
|
+
def notify_text(code, content, **options)
|
13
|
+
options.with_defaults!(
|
14
|
+
at_users: [],
|
15
|
+
at_mobiles: []
|
16
|
+
)
|
17
|
+
token_code = code.to_sym
|
18
|
+
token = fetch_token(token_code)
|
19
|
+
content = String(content)
|
20
|
+
at_users = Array(options[:at_users]).presence
|
21
|
+
at_mobiles = Array(options[:at_mobiles]).presence
|
22
|
+
body = get_text_body(content, at_users, at_mobiles)
|
23
|
+
notify(token, body)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
# @param content [String]
|
29
|
+
# @param at_users [Array]
|
30
|
+
# @param at_mobiles [Array]
|
31
|
+
# @return [Hash]
|
32
|
+
def get_text_body(content, at_users, at_mobiles)
|
33
|
+
{
|
34
|
+
msgtype: 'text',
|
35
|
+
text: {
|
36
|
+
content: content,
|
37
|
+
mentioned_mobile_list: at_mobiles,
|
38
|
+
mentioned_list: at_users
|
39
|
+
}.compact
|
40
|
+
}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'qywx/client/group_robot_client/core'
|
4
|
+
require 'qywx/client/group_robot_client/notify_text'
|
5
|
+
require 'qywx/client/group_robot_client/notify_markdown'
|
6
|
+
require 'qywx/client/group_robot_client/notify_feedcard'
|
7
|
+
|
8
|
+
module Qywx
|
9
|
+
module Client
|
10
|
+
module GroupRobotClient
|
11
|
+
include HTTParty
|
12
|
+
include Core
|
13
|
+
include NotifyText
|
14
|
+
include NotifyMarkdown
|
15
|
+
include NotifyFeedcard
|
16
|
+
base_uri Qywx::Client.config.group_robot_webhook_prefix
|
17
|
+
headers 'Content-Type' => 'application/json'
|
18
|
+
logger ::Logger.new($stdout), :debug, :curl if Qywx::Client.config.is_debugging
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Qywx
|
4
|
+
module Client
|
5
|
+
# HTTP Response result
|
6
|
+
class Result
|
7
|
+
attr_reader :status, :message
|
8
|
+
|
9
|
+
# @param status [Boolean]
|
10
|
+
# @param message [String] ('')
|
11
|
+
def initialize(status, message: '')
|
12
|
+
@status = status != false
|
13
|
+
@message = message.to_s
|
14
|
+
end
|
15
|
+
|
16
|
+
def failed?
|
17
|
+
!success?
|
18
|
+
end
|
19
|
+
|
20
|
+
def success?
|
21
|
+
status
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/qywx/client.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'httparty'
|
4
|
+
require 'active_support/all'
|
5
|
+
require 'qywx/client/errors'
|
6
|
+
require 'qywx/client/result'
|
7
|
+
|
8
|
+
module Qywx
|
9
|
+
# Qywx Client
|
10
|
+
# @see https://developer.work.weixin.qq.com/document/path/91770
|
11
|
+
module Client
|
12
|
+
extend ActiveSupport::Autoload
|
13
|
+
autoload :Configurable
|
14
|
+
autoload :GroupRobotClient
|
15
|
+
include Configurable
|
16
|
+
include GroupRobotClient
|
17
|
+
extend self
|
18
|
+
end
|
19
|
+
end
|
data/qywx-client.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'qywx/client/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'qywx-client'
|
9
|
+
spec.version = Qywx::Client::VERSION
|
10
|
+
spec.authors = ['joeyl']
|
11
|
+
spec.email = ['lu.joeyl.lu@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = '企业微信机器人.'
|
14
|
+
spec.description = '企业微信机器人.'
|
15
|
+
spec.homepage = 'https://github.com/joeyl-ljy/qywx-client'
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = 'exe'
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ['lib']
|
25
|
+
|
26
|
+
spec.required_ruby_version = '>= 2.5'
|
27
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
28
|
+
spec.add_dependency 'activesupport', '~> 5.2.1'
|
29
|
+
spec.add_dependency 'httparty', '~> 0.16.2'
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: qywx-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- joeyl
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-03-14 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: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 5.2.1
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 5.2.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: httparty
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.16.2
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.16.2
|
55
|
+
description: 企业微信机器人.
|
56
|
+
email:
|
57
|
+
- lu.joeyl.lu@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rubocop.yml"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- Gemfile.lock
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- _config.yml
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- lib/qywx/client.rb
|
74
|
+
- lib/qywx/client/configurable.rb
|
75
|
+
- lib/qywx/client/errors.rb
|
76
|
+
- lib/qywx/client/group_robot_client.rb
|
77
|
+
- lib/qywx/client/group_robot_client/core.rb
|
78
|
+
- lib/qywx/client/group_robot_client/notify_feedcard.rb
|
79
|
+
- lib/qywx/client/group_robot_client/notify_markdown.rb
|
80
|
+
- lib/qywx/client/group_robot_client/notify_text.rb
|
81
|
+
- lib/qywx/client/result.rb
|
82
|
+
- lib/qywx/client/version.rb
|
83
|
+
- qywx-client.gemspec
|
84
|
+
homepage: https://github.com/joeyl-ljy/qywx-client
|
85
|
+
licenses: []
|
86
|
+
metadata: {}
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '2.5'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubygems_version: 3.0.3
|
103
|
+
signing_key:
|
104
|
+
specification_version: 4
|
105
|
+
summary: 企业微信机器人.
|
106
|
+
test_files: []
|