kybus-bot 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7d6ea56c2bd47e58087f433d504c984c2055958b8b5cbd6fde2b438ffbcc77f9
4
- data.tar.gz: e256785b5022eb71f9f8943ab8e54dae9696b6ffe97431f443b23630c9dfad81
3
+ metadata.gz: 2b9b6a8575a2207a5aeff2a9a890973019460a86d904f2bc5989e4da2269cbff
4
+ data.tar.gz: 527bbd3abfb670bb76b4181e919124d43c2fc0c4cd353bb6457fc7a36ba34db0
5
5
  SHA512:
6
- metadata.gz: 1ef852ff7b0d785be08dac681cb10185ddde7b4362e343d7e188e93a3e2bf143ba48f126c301f1cfff8b7e6069f172de1b92057ad9be258fcb86a5b0880908e0
7
- data.tar.gz: 7dc0c97b640a932295c9d5b13454a865d9eb6384a95ba613de292b4f5c0d28ef1953617f1d3af7a4ccd4839d48a97ca27aa31b48c2c37e53c7854e4d87a19994
6
+ metadata.gz: 31fbae47c94d21fe626e3b00a24fae3e22d5928da955e9efc795f76808baab49bed4ef358e229fe7172f4db343dbd8d8a1a62f2a3f5945e8b7416c1738209796
7
+ data.tar.gz: b23e6febfc1daf445702e2670f181de76bbcd7170426ded2e6a52cf6a2e081092246be1603a275e5e52623916446e3f7b9b9a85d112872a5a1995f064a973fd1
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'base'
4
+ require_relative '../message'
5
+
3
6
  module Kybus
4
7
  module Bot
5
8
  # :nodoc: #
@@ -8,9 +11,10 @@ module Kybus
8
11
  # Wraps a debugging message inside a class.
9
12
  class DebugMessage < Kybus::Bot::Message
10
13
  # It receives a string with the raw text and the id of the channel
11
- def initialize(text, channel)
14
+ def initialize(text, channel, attachments = {})
12
15
  @text = text
13
16
  @channel = channel
17
+ @attachments = attachments
14
18
  end
15
19
 
16
20
  # Returns the channel id
@@ -22,6 +26,10 @@ module Kybus
22
26
  def raw_message
23
27
  @text
24
28
  end
29
+
30
+ def user
31
+ channel_id
32
+ end
25
33
  end
26
34
 
27
35
  # This class simulates a message chat with a user.
@@ -36,11 +36,61 @@ module Kybus
36
36
  @message.chat.type == 'private'
37
37
  end
38
38
 
39
+ def has_attachment?
40
+ !!@message.document
41
+ end
42
+
43
+ def attachment
44
+ @message.document
45
+ end
46
+
39
47
  def user
40
48
  @message.from.id
41
49
  end
42
50
  end
43
51
 
52
+ class TelegramFile
53
+ extend Kybus::DRY::ResourceInjector
54
+ attr_reader :id
55
+ def initialize(message)
56
+ if message.is_a?(String)
57
+ @id = message
58
+ elsif message.is_a?(Hash)
59
+ @id = message['id'] || message[:id]
60
+ elsif message.is_a?(TelegramFile)
61
+ @id = message.id
62
+ else
63
+ @id = message.file_id
64
+ end
65
+ end
66
+
67
+ def to_h
68
+ {
69
+ provide: 'telegram',
70
+ id: @id
71
+ }
72
+ end
73
+
74
+ def cli
75
+ @cli ||= TelegramFile.resource(:cli)
76
+ end
77
+
78
+ def meta
79
+ @meta ||= cli.api.get_file(file_id: @id)
80
+ end
81
+
82
+ def original_name
83
+ meta.dig('result', 'file_name')
84
+ end
85
+
86
+ def download
87
+ token = cli.api.token
88
+ file_path = meta.dig('result', 'file_path')
89
+ path = "https://api.telegram.org/file/bot#{token}/#{file_path}"
90
+ Faraday.get(path).body
91
+ end
92
+ end
93
+
44
94
  ##
45
95
  # This adapter is intended to be used on unit tests and development.
46
96
  class Telegram
@@ -53,6 +103,7 @@ module Kybus
53
103
  def initialize(configs)
54
104
  @config = configs
55
105
  @client = ::Telegram::Bot::Client.new(@config['token'])
106
+ TelegramFile.register(:cli, @client)
56
107
  end
57
108
 
58
109
  # Interface for receiving message
@@ -99,6 +150,16 @@ module Kybus
99
150
  file = Faraday::UploadIO.new(image_url, 'image/jpeg')
100
151
  @client.api.send_photo(chat_id: channel_name, photo: file)
101
152
  end
153
+
154
+ # interface for sending document
155
+ def send_document(channel_name, image_url)
156
+ file = Faraday::UploadIO.new(image_url, 'application/octect-stream')
157
+ @client.api.send_document(chat_id: channel_name, document: file)
158
+ end
159
+
160
+ def file_builder(file)
161
+ TelegramFile.new(file)
162
+ end
102
163
  end
103
164
 
104
165
  register('telegram', Telegram)
@@ -41,6 +41,10 @@ module Kybus
41
41
  provider.send_audio(channel || current_channel, content)
42
42
  end
43
43
 
44
+ def send_document(content, channel = nil)
45
+ provider.send_document(channel || current_channel, content)
46
+ end
47
+
44
48
  # Configurations needed:
45
49
  # - pool_size: number of threads created in execution
46
50
  # - provider: a configuration for a thread provider.
@@ -100,6 +104,7 @@ module Kybus
100
104
  self.command = message.raw_message
101
105
  else
102
106
  add_param(message.raw_message)
107
+ add_file(message.attachment) if message.has_attachment?
103
108
  end
104
109
  if command_ready?
105
110
  run_command!
@@ -145,10 +150,33 @@ module Kybus
145
150
  current_params
146
151
  end
147
152
 
153
+ def add_file(file)
154
+ return if @state[:requested_param].nil?
155
+
156
+ log_debug('Received new file',
157
+ param: @state[:requested_param].to_sym,
158
+ file: file.to_h)
159
+
160
+ files[@state[:requested_param].to_sym] = provider.file_builder(file)
161
+ @state[:params][("_#{@state[:requested_param]}_filename").to_sym] = file.file_name
162
+ end
163
+
164
+ def files
165
+ @state[:files] ||= {}
166
+ end
167
+
168
+ def file(name)
169
+ (file = files[name]) && provider.file_builder(file)
170
+ end
171
+
148
172
  def mention(name)
149
173
  provider.mention(name)
150
174
  end
151
175
 
176
+ def registered_commands
177
+ @commands.registered_commands
178
+ end
179
+
152
180
  # Loads command from state
153
181
  def current_command_object
154
182
  command = @state[:cmd]
@@ -173,7 +201,8 @@ module Kybus
173
201
  log_debug('Message set as command', command: cmd)
174
202
 
175
203
  @state[:cmd] = cmd.split(' ').first
176
- @state[:params] = {}
204
+ @state[:params] = { }
205
+ @state[:files] = { }
177
206
  end
178
207
 
179
208
  # validates which is the following parameter required
@@ -208,18 +237,27 @@ module Kybus
208
237
  # Private implementation for load message
209
238
  def load_state(channel)
210
239
  data = @factory.get(channel)
211
- data[:params] = JSON.parse(data[:params], symbolize_names: true)
240
+ data[:params] = JSON.parse(data[:params] || '{}', symbolize_names: true)
241
+ data[:files] = JSON.parse(data[:files] || '{}', symbolize_names: true)
212
242
  data
213
243
  rescue Kybus::Storage::Exceptions::ObjectNotFound
214
244
  @factory.create(channel_id: channel, params: {}.to_json)
215
245
  end
216
246
 
247
+ def parse_state!
248
+ end
249
+
217
250
  # Saves the state into storage
218
251
  def save_state!
219
- json = @state[:params]
220
- @state[:params] = json.to_json
252
+ backup = @state.clone
253
+ %i[params files].each do |param|
254
+ @state[param] = @state[param].to_json
255
+ end
256
+
221
257
  @state.store
222
- @state[:params] = json
258
+ %i[params files].each do |param|
259
+ @state[param] = backup[param]
260
+ end
223
261
  end
224
262
 
225
263
  def session
@@ -36,6 +36,10 @@ module Kybus
36
36
  @commands[name] = Command.new(params, block)
37
37
  end
38
38
 
39
+ def registered_commands
40
+ @commands.keys
41
+ end
42
+
39
43
  # Returns a command with the name
40
44
  def [](name)
41
45
  @commands[name]
@@ -11,6 +11,7 @@ module Kybus
11
11
  String :channel_id
12
12
  String :user
13
13
  String :params, text: true
14
+ String :files, text: true
14
15
  String :cmd
15
16
  String :requested_param
16
17
  end
@@ -0,0 +1,32 @@
1
+ require_relative 'base'
2
+ require_relative 'adapters/debug'
3
+
4
+ module Kybus
5
+ module Bot
6
+ class Base
7
+ CONFIG = {
8
+ 'name' => 'test',
9
+ 'state_repository' => {
10
+ 'name' => 'json',
11
+ 'storage' => 'storage'
12
+ },
13
+ 'pool_size' => 1,
14
+ 'provider' => {
15
+ 'name' => 'debug',
16
+ 'echo' => true,
17
+ 'channels' => { 'testing' => [] }
18
+ }
19
+ }.freeze
20
+
21
+ def self.make_test_bot
22
+ new(CONFIG)
23
+ end
24
+
25
+ def receives(msg, attachments = {})
26
+ msg = ::Kybus::Bot::Adapter::DebugMessage.new(msg, 'testing', attachments)
27
+ @last_message = msg
28
+ process_message(msg)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Kybus
4
4
  module Bot
5
- VERSION = '0.4.3'
5
+ VERSION = '0.5.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kybus-bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gilberto Vargas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-23 00:00:00.000000000 Z
11
+ date: 2022-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kybus-core
@@ -222,6 +222,7 @@ files:
222
222
  - lib/kybus/bot/command_definition.rb
223
223
  - lib/kybus/bot/message.rb
224
224
  - lib/kybus/bot/migrator.rb
225
+ - lib/kybus/bot/test.rb
225
226
  - lib/kybus/bot/testing.rb
226
227
  - lib/kybus/bot/version.rb
227
228
  homepage: https://github.com/tachomex/kybus