kanal-interfaces-pachka 0.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: da3ea2ca5eb705a798c4f661e30128b9b7acfd219a81e2b59c7b16fd140c68b5
4
- data.tar.gz: d9625a3ec90ddea49fe88a5c65e2d6eae44e4ae0671e8c070d752ece652154de
3
+ metadata.gz: ef104bb341ba6f3678ba148f790a89ab2c280c3f449c6d428a21f47ebd527854
4
+ data.tar.gz: b187b939765f3f51dbaba85354412a71a7760a6984ebc0d2b452e755f55777bd
5
5
  SHA512:
6
- metadata.gz: 8506860e02a1077d7b5ac2d2805c2ced396df3d745e9f5b2ef9527366c017d5384e2443eed20b1c2f4f060ce291a067c14f09d8e588ebdf1394c865988cc1b0f
7
- data.tar.gz: 2116b6b5469bd94488484ba220c891e732d6391b296a720bf675df221b780ecef0e1a8365b6987a800e254a075b6792e7f283ef13b3397f1aa904335785797a3
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.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)
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("PACHKA_API_KEY", nil)
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: false)
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 send_text(entity_id, text)
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 sends message with file
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 [void] <description>
92
+ # @return [UploadedFile] object containing all needed info to send with message
72
93
  #
73
- def send_file(entity_id, filepath, text)
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
- res = @conn.post(
111
- "messages",
112
- {
113
- message: {
114
- entity_type: "discussion",
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
@@ -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, local_server_debug_log: false, api_debug_log: false)
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
- @local_server_debug_log = local_server_debug_log
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,12 +53,37 @@ module Kanal
53
53
  end
54
54
 
55
55
  def consume_output(output)
56
+ text = output.pachka_text
57
+
58
+ uploaded_files = []
59
+
56
60
  unless output.pachka_file_path.nil?
57
- @api.send_file(output.pachka_entity_id, output.pachka_file_path, output.pachka_text)
58
- return
61
+ uploaded_file = @api.upload_file output.pachka_file_path
62
+
63
+ uploaded_files << uploaded_file
59
64
  end
60
65
 
61
- @api.send_text(output.pachka_entity_id, output.pachka_text) unless output.pachka_text.nil?
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
62
87
  end
63
88
 
64
89
  def start
@@ -66,10 +91,11 @@ module Kanal
66
91
 
67
92
  endpoint = Kanal::Interfaces::Pachka::Helpers::LocalServer.new(@host, @port)
68
93
  endpoint.on_request do |body|
69
- logger.debug "Local server received request with body: #{body}" if @local_server_debug_log
94
+ logger.debug "Local server received request with body: #{body}" if @local_server_log
70
95
 
71
96
  input = core.create_input
72
97
  input.source = :pachka
98
+ input.pachka_entity_type = body["entity_type"]
73
99
  input.pachka_entity_id = body["entity_id"]
74
100
  input.pachka_query = body["content"]
75
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
 
@@ -3,7 +3,7 @@
3
3
  module Kanal
4
4
  module Interfaces
5
5
  module Pachka
6
- VERSION = "0.1.1"
6
+ VERSION = "0.1.2"
7
7
  end
8
8
  end
9
9
  end
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.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - idchlife
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-15 00:00:00.000000000 Z
11
+ date: 2023-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday