kybus-bot 0.4.6 → 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: '01392aefbe8bf2a7f5be90e5cc0f77fbbcf66ece5ed77c2bd9f01be3fb9b476b'
4
- data.tar.gz: 43ec132ea325c8fe9cd6401d6976a80fcd9e57939483b8f79ea48848b0015bc4
3
+ metadata.gz: 2b9b6a8575a2207a5aeff2a9a890973019460a86d904f2bc5989e4da2269cbff
4
+ data.tar.gz: 527bbd3abfb670bb76b4181e919124d43c2fc0c4cd353bb6457fc7a36ba34db0
5
5
  SHA512:
6
- metadata.gz: 63f439ab82c6faf1324133146b5b47bff2a0c9fa8d29c8daab00f322edd6c5b47a639f0bd99150cdd420e9a95e2ddd9f32a62245f9ddd85bd6eba35af643481a
7
- data.tar.gz: a8a5cffea95d2831d911577d5754d45ed97361bda2f99ddf42812aa2b4058b800abb8a17944d7d84c8f14eba4f4d734290b5c0c24355082e3bf54340adce3bee
6
+ metadata.gz: 31fbae47c94d21fe626e3b00a24fae3e22d5928da955e9efc795f76808baab49bed4ef358e229fe7172f4db343dbd8d8a1a62f2a3f5945e8b7416c1738209796
7
+ data.tar.gz: b23e6febfc1daf445702e2670f181de76bbcd7170426ded2e6a52cf6a2e081092246be1603a275e5e52623916446e3f7b9b9a85d112872a5a1995f064a973fd1
@@ -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,6 +150,25 @@ 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
@@ -177,7 +201,8 @@ module Kybus
177
201
  log_debug('Message set as command', command: cmd)
178
202
 
179
203
  @state[:cmd] = cmd.split(' ').first
180
- @state[:params] = {}
204
+ @state[:params] = { }
205
+ @state[:files] = { }
181
206
  end
182
207
 
183
208
  # validates which is the following parameter required
@@ -212,18 +237,27 @@ module Kybus
212
237
  # Private implementation for load message
213
238
  def load_state(channel)
214
239
  data = @factory.get(channel)
215
- 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)
216
242
  data
217
243
  rescue Kybus::Storage::Exceptions::ObjectNotFound
218
244
  @factory.create(channel_id: channel, params: {}.to_json)
219
245
  end
220
246
 
247
+ def parse_state!
248
+ end
249
+
221
250
  # Saves the state into storage
222
251
  def save_state!
223
- json = @state[:params]
224
- @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
+
225
257
  @state.store
226
- @state[:params] = json
258
+ %i[params files].each do |param|
259
+ @state[param] = backup[param]
260
+ end
227
261
  end
228
262
 
229
263
  def session
@@ -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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Kybus
4
4
  module Bot
5
- VERSION = '0.4.6'
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.6
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: 2022-01-10 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