khl 1.0.1 → 1.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 +4 -4
- data/CHANGELOG.md +10 -4
- data/README.md +7 -0
- data/lib/khl/event.rb +24 -0
- data/lib/khl/http/response.rb +10 -7
- data/lib/khl/message.rb +65 -0
- data/lib/khl/version.rb +1 -1
- data/lib/khl/web_socket/client.rb +2 -2
- data/lib/khl/webhook/client.rb +98 -0
- data/lib/khl.rb +1 -0
- metadata +5 -3
- data/lib/khl/web_socket/message.rb +0 -54
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9299dfafd8b0c827311f73bc349ef23f8e8d6e2c87a664dc94c52c8b8fee74de
|
4
|
+
data.tar.gz: 2089da69756876c4d9f5e2afaebdc92e71c381e2fbf6c6f09d50fdac3a476817
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f1f1607a954fd9ff6f3d0516d79cb79502db44b7c675cc45d68ff4f1c4bcec264f5872b3b7c6b8fceda363787bcf298932a561e5aab39e7e4da401c5ade753a4
|
7
|
+
data.tar.gz: 974ebd1222e12470b06effe666df2aa522c93e53061f4bb8f540826bc2076c9e43b54db4a37245381bb653f7300979b6f9f82f7af6040a7b4f1b64c597e2dfd1
|
data/CHANGELOG.md
CHANGED
@@ -1,9 +1,15 @@
|
|
1
|
-
### 1.
|
1
|
+
### 1.1.0 - 2022-04-02
|
2
2
|
|
3
|
-
* Add 开黑啦
|
4
|
-
* Add
|
5
|
-
* Add GitHub CI
|
3
|
+
* Add 开黑啦 Webhook API
|
4
|
+
* Add Event Wrapper
|
6
5
|
|
7
6
|
### 1.0.1 - 2022-03-29
|
8
7
|
|
9
8
|
* Fix params type
|
9
|
+
|
10
|
+
|
11
|
+
### 1.0.0 - 2022-03-29
|
12
|
+
|
13
|
+
* Add 开黑啦 HTTP API
|
14
|
+
* Add 开黑啦 WebSocket API
|
15
|
+
* Add GitHub CI
|
data/README.md
CHANGED
@@ -40,6 +40,13 @@ ws_client = KHL::WebSocket::Client.new(token: token)
|
|
40
40
|
Thread.new { ws_client.run } # Run WebSocket client
|
41
41
|
ws_clinet.state # Get current state
|
42
42
|
ws_client.messages.pop # Get message from queue
|
43
|
+
|
44
|
+
# Use Webhook API
|
45
|
+
webhook_client = KHL::Webhook::Client.new("webhook_url", "challenge_token")
|
46
|
+
webhook_client.online? # => false
|
47
|
+
webhook_client.challenge # Do challenge
|
48
|
+
webhook_client.online? # => true
|
49
|
+
webhook_client.parse_message("data from webhook")
|
43
50
|
```
|
44
51
|
|
45
52
|
## Development
|
data/lib/khl/event.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/hash/indifferent_access"
|
4
|
+
require "active_support/hash_with_indifferent_access"
|
5
|
+
|
6
|
+
module KHL
|
7
|
+
class Event
|
8
|
+
attr_accessor :channel_type, :type, :target_id, :author_id, :content, :msg_id, :msg_timestamp, :nonce, :extra
|
9
|
+
|
10
|
+
def initialize(params = {})
|
11
|
+
params = ActiveSupport::HashWithIndifferentAccess.new(params)
|
12
|
+
|
13
|
+
@channel_type = params[:channel_type]
|
14
|
+
@type = params[:type]
|
15
|
+
@target_id = params[:target_id]
|
16
|
+
@author_id = params[:author_id]
|
17
|
+
@content = params[:content]
|
18
|
+
@msg_id = params[:msg_id]
|
19
|
+
@msg_timestamp = params[:msg_timestamp]
|
20
|
+
@nonce = params[:nonce]
|
21
|
+
@extra = params[:extra]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/khl/http/response.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "active_support/core_ext/hash/indifferent_access"
|
4
|
+
require "active_support/hash_with_indifferent_access"
|
5
|
+
|
3
6
|
module KHL
|
4
7
|
module HTTP
|
5
8
|
class Response
|
@@ -9,16 +12,16 @@ module KHL
|
|
9
12
|
def initialize(raw_response)
|
10
13
|
@raw_response = raw_response
|
11
14
|
@http_code = raw_response.code.to_i
|
12
|
-
@body = JSON.parse(@raw_response.body)
|
15
|
+
@body = ActiveSupport::HashWithIndifferentAccess.new(JSON.parse(@raw_response.body))
|
13
16
|
end
|
14
17
|
|
15
18
|
def data
|
16
|
-
@body[
|
19
|
+
@body[:data]
|
17
20
|
end
|
18
21
|
|
19
22
|
# The code in the response body
|
20
23
|
def code
|
21
|
-
@body[
|
24
|
+
@body[:code]
|
22
25
|
end
|
23
26
|
|
24
27
|
def max_request_limit
|
@@ -50,19 +53,19 @@ module KHL
|
|
50
53
|
end
|
51
54
|
|
52
55
|
def message
|
53
|
-
@body[
|
56
|
+
@body[:message]
|
54
57
|
end
|
55
58
|
|
56
59
|
def page
|
57
|
-
data.dig(
|
60
|
+
data.dig(:meta, :page) || 0
|
58
61
|
end
|
59
62
|
|
60
63
|
def page_size
|
61
|
-
data.dig(
|
64
|
+
data.dig(:meta, :page_size) || 0
|
62
65
|
end
|
63
66
|
|
64
67
|
def page_total
|
65
|
-
data.dig(
|
68
|
+
data.dig(:meta, :total) || 0
|
66
69
|
end
|
67
70
|
end
|
68
71
|
end
|
data/lib/khl/message.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/hash/indifferent_access"
|
4
|
+
require "active_support/hash_with_indifferent_access"
|
5
|
+
|
6
|
+
require_relative "event"
|
7
|
+
|
8
|
+
module KHL
|
9
|
+
class Message
|
10
|
+
TYPES = %i[
|
11
|
+
event
|
12
|
+
hello
|
13
|
+
ping
|
14
|
+
pong
|
15
|
+
resume
|
16
|
+
reconnect
|
17
|
+
resume_ack
|
18
|
+
].freeze
|
19
|
+
|
20
|
+
attr_accessor :type, :data, :sn
|
21
|
+
|
22
|
+
def self.parse(raw)
|
23
|
+
return unless raw
|
24
|
+
|
25
|
+
data = JSON.parse(raw)
|
26
|
+
data = ActiveSupport::HashWithIndifferentAccess.new(data)
|
27
|
+
|
28
|
+
message = Message.new
|
29
|
+
message.type = TYPES[data[:s]]
|
30
|
+
message.data = data[:d]
|
31
|
+
message.sn = data[:sn]
|
32
|
+
message
|
33
|
+
end
|
34
|
+
|
35
|
+
def initialize(type: nil, data: nil, sn: nil)
|
36
|
+
@type = type
|
37
|
+
@data = data
|
38
|
+
@sn = sn
|
39
|
+
end
|
40
|
+
|
41
|
+
def event
|
42
|
+
return unless event?
|
43
|
+
|
44
|
+
Event.new(@data)
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_h
|
48
|
+
ActiveSupport::HashWithIndifferentAccess.new(
|
49
|
+
s: TYPES.index(type),
|
50
|
+
d: data,
|
51
|
+
sn: sn
|
52
|
+
)
|
53
|
+
end
|
54
|
+
|
55
|
+
def to_json
|
56
|
+
to_h.to_json
|
57
|
+
end
|
58
|
+
|
59
|
+
TYPES.each do |type|
|
60
|
+
define_method("#{type}?") do
|
61
|
+
@type == type
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/khl/version.rb
CHANGED
@@ -7,7 +7,7 @@ require "json"
|
|
7
7
|
require "uri"
|
8
8
|
require "zlib"
|
9
9
|
|
10
|
-
require_relative "message"
|
10
|
+
require_relative "../message"
|
11
11
|
|
12
12
|
module KHL
|
13
13
|
module WebSocket
|
@@ -24,7 +24,7 @@ module KHL
|
|
24
24
|
# @option config [String] :token_type Token type
|
25
25
|
# @option config [String] :language Language
|
26
26
|
# @option config [Boolean] :compress Compress
|
27
|
-
def initialize(config)
|
27
|
+
def initialize(config = {})
|
28
28
|
config[:compress] ||= false
|
29
29
|
|
30
30
|
@config = config
|
@@ -0,0 +1,98 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module KHL
|
4
|
+
module Webhook
|
5
|
+
# Client for the KHL Webhook API
|
6
|
+
# @example
|
7
|
+
# client = KHL::Webhook::Client.new("https://foo.bar/callback", "your_challenge_token")
|
8
|
+
# client.online? # => false
|
9
|
+
# client.challenge # => true
|
10
|
+
# client.online? # => true
|
11
|
+
# data = client.parse_message(data_from_webhook)
|
12
|
+
class Client
|
13
|
+
attr_reader :key
|
14
|
+
|
15
|
+
# @param [String] challenge_url The source url from the request
|
16
|
+
# @param [String] challenge_token The challenge param from the request
|
17
|
+
# @param [Hash] options Additional options
|
18
|
+
# @option options [Boolean] :compress (true) Enable/disable compression
|
19
|
+
# @option options [encrypt] :encrypt (false) Enable/disable encryption
|
20
|
+
# @option options [String] :key (nil) Encryption key
|
21
|
+
def initialize(challenge_url, challenge_token, options = {})
|
22
|
+
@challenge_url = challenge_url
|
23
|
+
@challenge_token = challenge_token
|
24
|
+
|
25
|
+
@compress = options[:compress] || true
|
26
|
+
@encrypt = options[:encrypt] || false
|
27
|
+
@key = options[:key]
|
28
|
+
|
29
|
+
raise ArgumentError, "key is required" if @encrypt && @key.nil?
|
30
|
+
end
|
31
|
+
|
32
|
+
# Do the challenge
|
33
|
+
# @return [Boolean] The challenge success state
|
34
|
+
def challenge
|
35
|
+
uri = URI.parse(@challenge_url)
|
36
|
+
res = Net::HTTP.post_form(uri, challenge: @challenge_token)
|
37
|
+
if res.code == "200"
|
38
|
+
@online = true
|
39
|
+
return true
|
40
|
+
end
|
41
|
+
|
42
|
+
false
|
43
|
+
end
|
44
|
+
|
45
|
+
def compress?
|
46
|
+
@compress
|
47
|
+
end
|
48
|
+
|
49
|
+
def encrypt?
|
50
|
+
@encrypt
|
51
|
+
end
|
52
|
+
|
53
|
+
def online?
|
54
|
+
@online || false
|
55
|
+
end
|
56
|
+
|
57
|
+
# Parse message from raw data
|
58
|
+
# @param [String] data The raw data from the webhook
|
59
|
+
# @return [KHL::Message] The parsed message
|
60
|
+
def parse_message(data)
|
61
|
+
if encrypt?
|
62
|
+
data = decrypt(data)
|
63
|
+
end
|
64
|
+
|
65
|
+
if compress?
|
66
|
+
data = decompress(data)
|
67
|
+
end
|
68
|
+
|
69
|
+
Message.parse(data)
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
# Decode the data
|
75
|
+
# @param [String] encrypted_data Encrypted data
|
76
|
+
# @return [String] Decrypted data
|
77
|
+
def decode(encrypted_data)
|
78
|
+
return nil unless encrypt?
|
79
|
+
|
80
|
+
encrypted_data = Base64.strict_decode64(encrypted_data)
|
81
|
+
cipher = OpenSSL::Cipher.new("aes-256-cbc")
|
82
|
+
cipher.decrypt
|
83
|
+
cipher.iv = encrypted_data[0..15]
|
84
|
+
cipher.key = key.ljust(32, "\0")
|
85
|
+
cipher.update(encrypted_data) + cipher.final
|
86
|
+
end
|
87
|
+
|
88
|
+
# Decompress the data
|
89
|
+
# @param [String] data Compressed data
|
90
|
+
# @return [String] Decompressed data
|
91
|
+
def decompress(data)
|
92
|
+
return nil unless compress?
|
93
|
+
|
94
|
+
Zlib::Inflate.inflate(data.pack("C*"))
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
data/lib/khl.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: khl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dounx
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-04-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -54,6 +54,7 @@ files:
|
|
54
54
|
- CHANGELOG.md
|
55
55
|
- README.md
|
56
56
|
- lib/khl.rb
|
57
|
+
- lib/khl/event.rb
|
57
58
|
- lib/khl/http/asset.rb
|
58
59
|
- lib/khl/http/badge.rb
|
59
60
|
- lib/khl/http/base.rb
|
@@ -75,9 +76,10 @@ files:
|
|
75
76
|
- lib/khl/http/response.rb
|
76
77
|
- lib/khl/http/user.rb
|
77
78
|
- lib/khl/http/user_chat.rb
|
79
|
+
- lib/khl/message.rb
|
78
80
|
- lib/khl/version.rb
|
79
81
|
- lib/khl/web_socket/client.rb
|
80
|
-
- lib/khl/
|
82
|
+
- lib/khl/webhook/client.rb
|
81
83
|
homepage: https://github.com/DessertShop/KHL
|
82
84
|
licenses:
|
83
85
|
- MIT
|
@@ -1,54 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module KHL
|
4
|
-
module WebSocket
|
5
|
-
class Message
|
6
|
-
TYPES = %i[
|
7
|
-
event
|
8
|
-
hello
|
9
|
-
ping
|
10
|
-
pong
|
11
|
-
resume
|
12
|
-
reconnect
|
13
|
-
resume_ack
|
14
|
-
].freeze
|
15
|
-
|
16
|
-
attr_accessor :type, :data, :sn
|
17
|
-
|
18
|
-
def self.parse(raw)
|
19
|
-
return unless raw
|
20
|
-
|
21
|
-
data = JSON.parse(raw)
|
22
|
-
message = Message.new
|
23
|
-
message.type = TYPES[data["s"]]
|
24
|
-
message.data = data["d"]
|
25
|
-
message.sn = data["sn"]
|
26
|
-
message
|
27
|
-
end
|
28
|
-
|
29
|
-
def initialize(type: nil, data: nil, sn: nil)
|
30
|
-
@type = type
|
31
|
-
@data = data
|
32
|
-
@sn = sn
|
33
|
-
end
|
34
|
-
|
35
|
-
def to_h
|
36
|
-
{
|
37
|
-
s: TYPES.index(type),
|
38
|
-
d: data,
|
39
|
-
sn: sn
|
40
|
-
}
|
41
|
-
end
|
42
|
-
|
43
|
-
def to_json
|
44
|
-
to_h.to_json
|
45
|
-
end
|
46
|
-
|
47
|
-
TYPES.each do |type|
|
48
|
-
define_method("#{type}?") do
|
49
|
-
@type == type
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|