kanal-interfaces-pachka 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +4 -1
- data/example/example.rb +2 -2
- data/lib/kanal/interfaces/pachka/helpers/pachka_api.rb +39 -33
- data/lib/kanal/interfaces/pachka/pachka_interface.rb +34 -7
- data/lib/kanal/interfaces/pachka/plugins/pachka_integration_plugin.rb +4 -1
- data/lib/kanal/interfaces/pachka/version.rb +1 -1
- metadata +5 -6
- data/kanal-interfaces-pachka.gemspec +0 -43
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ef104bb341ba6f3678ba148f790a89ab2c280c3f449c6d428a21f47ebd527854
|
4
|
+
data.tar.gz: b187b939765f3f51dbaba85354412a71a7760a6984ebc0d2b452e755f55777bd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a76f6bd5236bc98e2c334b7d60e3251246cb8066bb11f4f91dbadce6b5c7497e8993d9cfb76ba9e18f804ee70e0ba6c9212321e5c558c5874a7773ce0e47f762
|
7
|
+
data.tar.gz: 9e79580d073bd983da530f8a7a71ce8c51dbb917172d2cb308b6aa361ba10d3209c718c3db2d93eceac4efeac75e8dd2ed82b16bbb848ced4a4f4d83afa8e18e
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
kanal-interfaces-pachka (0.1.
|
4
|
+
kanal-interfaces-pachka (0.1.2)
|
5
5
|
faraday (= 2.7.4)
|
6
6
|
faraday-multipart (= 1.0.4)
|
7
7
|
kanal (= 0.5.1)
|
@@ -33,6 +33,8 @@ GEM
|
|
33
33
|
multipart-post (2.3.0)
|
34
34
|
nokogiri (1.14.3-x86_64-darwin)
|
35
35
|
racc (~> 1.4)
|
36
|
+
nokogiri (1.14.3-x86_64-linux)
|
37
|
+
racc (~> 1.4)
|
36
38
|
parallel (1.22.1)
|
37
39
|
parser (3.2.2.0)
|
38
40
|
ast (~> 2.4.1)
|
@@ -97,6 +99,7 @@ GEM
|
|
97
99
|
|
98
100
|
PLATFORMS
|
99
101
|
x86_64-darwin-22
|
102
|
+
x86_64-linux
|
100
103
|
|
101
104
|
DEPENDENCIES
|
102
105
|
kanal-interfaces-pachka!
|
data/example/example.rb
CHANGED
@@ -9,11 +9,11 @@ logger = Logger.new $stdout
|
|
9
9
|
logger.level = Logger::WARN
|
10
10
|
core.add_logger logger
|
11
11
|
|
12
|
-
api_key = ENV.fetch("
|
12
|
+
api_key = ENV.fetch("PACHKA_ACCESS_TOKEN", nil)
|
13
13
|
|
14
14
|
puts "Pachka api key: #{api_key}"
|
15
15
|
|
16
|
-
interface = Kanal::Interfaces::Pachka::PachkaInterface.new(core, api_key, api_debug_log:
|
16
|
+
interface = Kanal::Interfaces::Pachka::PachkaInterface.new(core, api_key, local_server_log: true, api_debug_log: true)
|
17
17
|
|
18
18
|
interface.router.default_response do
|
19
19
|
pachka_text "Hey! I don't know how to answer to this yet... But I will, someday ;)"
|
@@ -16,6 +16,8 @@ module Kanal
|
|
16
16
|
class PachkaApi
|
17
17
|
include Kanal::Logger
|
18
18
|
|
19
|
+
UploadedFile = Struct.new(:file_key, :file_name, :file_type, :file_size)
|
20
|
+
|
19
21
|
#
|
20
22
|
# @param access_token [String] access token for bot
|
21
23
|
# @param verbose [Boolean] pass true to enable STDOUT logging of requests and responses
|
@@ -39,40 +41,57 @@ module Kanal
|
|
39
41
|
|
40
42
|
#
|
41
43
|
# Method sends text message via bot into the Pachka api
|
44
|
+
# with optionally sending files
|
42
45
|
#
|
43
46
|
# @param entity_id [Integer] id of discussion obtained from input
|
47
|
+
# @param entity_type [String] type of chat obtained from input
|
44
48
|
# @param text [String] text to be sent
|
49
|
+
# @param uploaded_files [Array] array of UploadedFile objects to send with message
|
45
50
|
#
|
46
51
|
# @return [void] <description>
|
47
52
|
#
|
48
|
-
def
|
53
|
+
def send_message(entity_id, entity_type, text, uploaded_files = [])
|
54
|
+
payload = {
|
55
|
+
message: {
|
56
|
+
entity_type: entity_type,
|
57
|
+
entity_id: entity_id,
|
58
|
+
content: text
|
59
|
+
}
|
60
|
+
}
|
61
|
+
|
62
|
+
unless uploaded_files.empty?
|
63
|
+
files = []
|
64
|
+
|
65
|
+
uploaded_files.each do |uploaded_file|
|
66
|
+
files << {
|
67
|
+
key: uploaded_file.file_key,
|
68
|
+
name: uploaded_file.file_name,
|
69
|
+
file_type: uploaded_file.file_type,
|
70
|
+
size: uploaded_file.file_size
|
71
|
+
}
|
72
|
+
end
|
73
|
+
|
74
|
+
payload[:message][:files] = files
|
75
|
+
end
|
76
|
+
|
49
77
|
@conn.post(
|
50
78
|
"messages",
|
51
|
-
|
52
|
-
message: {
|
53
|
-
entity_type: "discussion",
|
54
|
-
entity_id: entity_id,
|
55
|
-
content: text
|
56
|
-
}
|
57
|
-
}.to_json,
|
79
|
+
payload.to_json,
|
58
80
|
{ "Content-Type" => "application/json" }
|
59
81
|
)
|
60
82
|
rescue Exception => e
|
61
83
|
logger.error "Cant send message to Pachka api! Error: #{e.full_message}"
|
84
|
+
raise
|
62
85
|
end
|
63
86
|
|
64
87
|
#
|
65
|
-
# Method
|
88
|
+
# Method uploads file at filepath to Pachka api and returns UploadedFile
|
66
89
|
#
|
67
|
-
# @param entity_id [Integer] entity_id obtained via input.pachka_entity_id
|
68
90
|
# @param filepath [String] local filepath to file
|
69
|
-
# @param text [String] text of message
|
70
91
|
#
|
71
|
-
# @return [
|
92
|
+
# @return [UploadedFile] object containing all needed info to send with message
|
72
93
|
#
|
73
|
-
def
|
74
|
-
text ||= " "
|
75
|
-
|
94
|
+
def upload_file(filepath)
|
76
95
|
# Obtaining all the needed fields for uploading a file
|
77
96
|
res = @conn.post("uploads")
|
78
97
|
|
@@ -107,25 +126,12 @@ module Kanal
|
|
107
126
|
|
108
127
|
raise "Problem with uploading file to Pachka api! More in api logs." unless res.success?
|
109
128
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
entity_id: entity_id,
|
116
|
-
content: text,
|
117
|
-
files: [
|
118
|
-
key: file_key,
|
119
|
-
name: filename,
|
120
|
-
file_type: mime_type.include?("image") ? "image" : "file",
|
121
|
-
size: File.size(filepath)
|
122
|
-
]
|
123
|
-
}
|
124
|
-
}.to_json,
|
125
|
-
{ "Content-Type" => "application/json" }
|
129
|
+
UploadedFile.new(
|
130
|
+
file_key,
|
131
|
+
filename,
|
132
|
+
mime_type.include?("image") ? "image" : "file",
|
133
|
+
File.size(filepath)
|
126
134
|
)
|
127
|
-
|
128
|
-
raise "Problem with sending message with file to Pachka api! More in api logs." unless res.success?
|
129
135
|
rescue Exception => e
|
130
136
|
logger.error "Error sending file to Pachka api! More info: #{e.full_message}"
|
131
137
|
end
|
@@ -21,7 +21,7 @@ module Kanal
|
|
21
21
|
#
|
22
22
|
# Output parameters registered:
|
23
23
|
# output.pachka_text - you can specify it in the respond block for bot to reply with text
|
24
|
-
#
|
24
|
+
# output.pachka_file_path - you can specify it in the respond block for bot to reply with file
|
25
25
|
#
|
26
26
|
class PachkaInterface < Kanal::Core::Interfaces::Interface
|
27
27
|
include Kanal::Logger
|
@@ -36,13 +36,13 @@ module Kanal
|
|
36
36
|
# @param local_server_debug_log [Boolean] pass true for local server to log requests to it to Kanal logger
|
37
37
|
# @param api_debug_log [Boolean] pass true to log pachka api requests to STDOUT
|
38
38
|
#
|
39
|
-
def initialize(core, access_token, host: "localhost", port: 8090,
|
39
|
+
def initialize(core, access_token, host: "localhost", port: 8090, local_server_log: false, api_debug_log: false)
|
40
40
|
super(core)
|
41
41
|
|
42
42
|
@port = port
|
43
43
|
@host = host
|
44
44
|
|
45
|
-
@
|
45
|
+
@local_server_log = local_server_log
|
46
46
|
|
47
47
|
@api = Kanal::Interfaces::Pachka::Helpers::PachkaApi.new access_token, verbose: api_debug_log
|
48
48
|
|
@@ -53,11 +53,37 @@ module Kanal
|
|
53
53
|
end
|
54
54
|
|
55
55
|
def consume_output(output)
|
56
|
-
|
56
|
+
text = output.pachka_text
|
57
57
|
|
58
|
-
|
58
|
+
uploaded_files = []
|
59
59
|
|
60
|
-
|
60
|
+
unless output.pachka_file_path.nil?
|
61
|
+
uploaded_file = @api.upload_file output.pachka_file_path
|
62
|
+
|
63
|
+
uploaded_files << uploaded_file
|
64
|
+
end
|
65
|
+
|
66
|
+
# When sending files without text
|
67
|
+
text ||= " "
|
68
|
+
|
69
|
+
@api.send_message(
|
70
|
+
output.pachka_entity_id,
|
71
|
+
output.pachka_entity_type,
|
72
|
+
text,
|
73
|
+
uploaded_files
|
74
|
+
)
|
75
|
+
rescue Exception => e
|
76
|
+
logger.error "Error sending output as message to Pachka api! More info: #{e.full_message}"
|
77
|
+
|
78
|
+
begin
|
79
|
+
@api.send_message(
|
80
|
+
output.pachka_entity_id,
|
81
|
+
output.pachka_entity_type,
|
82
|
+
"Error occured while sending message to Pachka api. Please consult with developers of this bot"
|
83
|
+
)
|
84
|
+
rescue Exception => e
|
85
|
+
logger.fatal "Can't even send message about erro to Pachka api! More info: #{e.full_message}"
|
86
|
+
end
|
61
87
|
end
|
62
88
|
|
63
89
|
def start
|
@@ -65,10 +91,11 @@ module Kanal
|
|
65
91
|
|
66
92
|
endpoint = Kanal::Interfaces::Pachka::Helpers::LocalServer.new(@host, @port)
|
67
93
|
endpoint.on_request do |body|
|
68
|
-
logger.debug "Local server received request with body: #{body}" if @
|
94
|
+
logger.debug "Local server received request with body: #{body}" if @local_server_log
|
69
95
|
|
70
96
|
input = core.create_input
|
71
97
|
input.source = :pachka
|
98
|
+
input.pachka_entity_type = body["entity_type"]
|
72
99
|
input.pachka_entity_id = body["entity_id"]
|
73
100
|
input.pachka_query = body["content"]
|
74
101
|
|
@@ -30,12 +30,14 @@ module Kanal
|
|
30
30
|
|
31
31
|
def register_input_parameters(core)
|
32
32
|
core.register_input_parameter :pachka_query, readonly: true
|
33
|
+
core.register_input_parameter :pachka_entity_type, readonly: true
|
33
34
|
core.register_input_parameter :pachka_entity_id, readonly: true
|
34
35
|
core.register_input_parameter :pachka_command, readonly: true
|
35
36
|
core.register_input_parameter :pachka_text, readonly: true
|
36
37
|
end
|
37
38
|
|
38
39
|
def register_output_parameters(core)
|
40
|
+
core.register_output_parameter :pachka_entity_type
|
39
41
|
core.register_output_parameter :pachka_entity_id
|
40
42
|
core.register_output_parameter :pachka_text
|
41
43
|
core.register_output_parameter :pachka_file_path
|
@@ -52,7 +54,8 @@ module Kanal
|
|
52
54
|
end
|
53
55
|
|
54
56
|
core.hooks.attach :output_before_returned do |input, output|
|
55
|
-
output.pachka_entity_id = input.pachka_entity_id
|
57
|
+
output.pachka_entity_id = input.pachka_entity_id if output.pachka_entity_id.nil?
|
58
|
+
output.pachka_entity_type = input.pachka_entity_type if output.pachka_entity_type.nil?
|
56
59
|
end
|
57
60
|
end
|
58
61
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kanal-interfaces-pachka
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- idchlife
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-04-
|
11
|
+
date: 2023-04-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -100,7 +100,6 @@ files:
|
|
100
100
|
- example/example.rb
|
101
101
|
- example/sample_file.zip
|
102
102
|
- example/sample_image.png
|
103
|
-
- kanal-interfaces-pachka.gemspec
|
104
103
|
- lib/kanal/interfaces/pachka.rb
|
105
104
|
- lib/kanal/interfaces/pachka/helpers/local_server.rb
|
106
105
|
- lib/kanal/interfaces/pachka/helpers/pachka_api.rb
|
@@ -116,7 +115,7 @@ metadata:
|
|
116
115
|
homepage_uri: https://github.com/idchlife/kanal-interfaces-pachka
|
117
116
|
source_code_uri: https://github.com/idchlife/kanal-interfaces-pachka
|
118
117
|
changelog_uri: https://github.com/idchlife/kanal-interfaces-pachka/blob/main/CHANGELOG.md
|
119
|
-
post_install_message:
|
118
|
+
post_install_message:
|
120
119
|
rdoc_options: []
|
121
120
|
require_paths:
|
122
121
|
- lib
|
@@ -132,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
132
131
|
version: '0'
|
133
132
|
requirements: []
|
134
133
|
rubygems_version: 3.1.6
|
135
|
-
signing_key:
|
134
|
+
signing_key:
|
136
135
|
specification_version: 4
|
137
136
|
summary: Kanal Interface to work with Pachka messenger
|
138
137
|
test_files: []
|
@@ -1,43 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative "lib/kanal/interfaces/pachka/version"
|
4
|
-
|
5
|
-
Gem::Specification.new do |spec|
|
6
|
-
spec.name = "kanal-interfaces-pachka"
|
7
|
-
spec.version = Kanal::Interfaces::Pachka::VERSION
|
8
|
-
spec.authors = ["idchlife"]
|
9
|
-
spec.email = ["idchlife@gmail.com"]
|
10
|
-
|
11
|
-
spec.summary = "Kanal Interface to work with Pachka messenger"
|
12
|
-
spec.description = "Use this interface with your Kanal to create bot for Pachka messenger"
|
13
|
-
spec.homepage = "https://github.com/idchlife/kanal-interfaces-pachka"
|
14
|
-
spec.license = "MIT"
|
15
|
-
spec.required_ruby_version = ">= 2.7.6"
|
16
|
-
|
17
|
-
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
18
|
-
|
19
|
-
spec.metadata["homepage_uri"] = spec.homepage
|
20
|
-
spec.metadata["source_code_uri"] = "https://github.com/idchlife/kanal-interfaces-pachka"
|
21
|
-
spec.metadata["changelog_uri"] = "https://github.com/idchlife/kanal-interfaces-pachka/blob/main/CHANGELOG.md"
|
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(__dir__) do
|
26
|
-
`git ls-files -z`.split("\x0").reject do |f|
|
27
|
-
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
|
28
|
-
end
|
29
|
-
end
|
30
|
-
spec.bindir = "exe"
|
31
|
-
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
32
|
-
spec.require_paths = ["lib"]
|
33
|
-
|
34
|
-
# Uncomment to register a new dependency of your gem
|
35
|
-
spec.add_dependency "faraday", "2.7.4"
|
36
|
-
spec.add_dependency "faraday-multipart", "1.0.4"
|
37
|
-
spec.add_dependency "kanal", "0.5.1"
|
38
|
-
spec.add_dependency "mini_mime", "1.1.2"
|
39
|
-
spec.add_dependency "webrick", "1.8.1"
|
40
|
-
|
41
|
-
# For more information and examples about making a new gem, check out our
|
42
|
-
# guide at: https://bundler.io/guides/creating_gem.html
|
43
|
-
end
|