kybus-bot 0.4.4 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/kybus/bot/adapters/debug.rb +20 -6
- data/lib/kybus/bot/adapters/telegram.rb +61 -0
- data/lib/kybus/bot/base.rb +39 -5
- data/lib/kybus/bot/migrator.rb +1 -0
- data/lib/kybus/bot/test.rb +32 -0
- data/lib/kybus/bot/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fcb4039ffd0fa38cadb57ba4a6a0813d5c105590677f1e6f6a7ef85fa6ee2365
|
4
|
+
data.tar.gz: abf1c8941afd74565c25e012b3eec8e94e17d1e0a42a83b2bc20be1eceac0192
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f1e09cd5437c55dbfc6a38df6c3f8ff1383f6f2974538e3ec2ebd380ea38db0e12cd9ce8ba82b87de15bd1200fef0d610ab1e81ee34c644e5fd064f564a31bd7
|
7
|
+
data.tar.gz: 159c13cb384b3168647e572544cdb96098a4d11b8ae4ed7c1143ba562fd0c52eccdf9295729f97e1614ef44d020681bd9615dc064522664114a3c211d9319566
|
@@ -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,12 @@ 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
|
-
|
14
|
+
attr_reader :attachment
|
15
|
+
|
16
|
+
def initialize(text, channel, attachment = nil)
|
12
17
|
@text = text
|
13
18
|
@channel = channel
|
19
|
+
@attachment = attachment
|
14
20
|
end
|
15
21
|
|
16
22
|
# Returns the channel id
|
@@ -22,6 +28,14 @@ module Kybus
|
|
22
28
|
def raw_message
|
23
29
|
@text
|
24
30
|
end
|
31
|
+
|
32
|
+
def user
|
33
|
+
channel_id
|
34
|
+
end
|
35
|
+
|
36
|
+
def has_attachment?
|
37
|
+
!!attachment
|
38
|
+
end
|
25
39
|
end
|
26
40
|
|
27
41
|
# This class simulates a message chat with a user.
|
@@ -52,7 +66,7 @@ module Kybus
|
|
52
66
|
DebugMessage.new(@pending_messages.shift, @name)
|
53
67
|
end
|
54
68
|
|
55
|
-
def send_data(message)
|
69
|
+
def send_data(message, attachment)
|
56
70
|
return unless @echo
|
57
71
|
|
58
72
|
puts "Sending message to channel: #{@name}"
|
@@ -62,8 +76,8 @@ module Kybus
|
|
62
76
|
attr_writer :echo
|
63
77
|
|
64
78
|
# receives the answer from the bot
|
65
|
-
def answer(message)
|
66
|
-
send_data(message)
|
79
|
+
def answer(message, attachment = nil)
|
80
|
+
send_data(message, attachment)
|
67
81
|
@state = :open
|
68
82
|
end
|
69
83
|
end
|
@@ -112,8 +126,8 @@ module Kybus
|
|
112
126
|
end
|
113
127
|
|
114
128
|
# interface for sending messages
|
115
|
-
def send_message(channel_name, contents)
|
116
|
-
channel(channel_name).answer(contents)
|
129
|
+
def send_message(channel_name, contents, attachment = nil)
|
130
|
+
channel(channel_name).answer(contents, attachment)
|
117
131
|
end
|
118
132
|
|
119
133
|
# interface for sending video
|
@@ -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)
|
data/lib/kybus/bot/base.rb
CHANGED
@@ -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
|
-
|
224
|
-
|
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
|
-
|
258
|
+
%i[params files].each do |param|
|
259
|
+
@state[param] = backup[param]
|
260
|
+
end
|
227
261
|
end
|
228
262
|
|
229
263
|
def session
|
data/lib/kybus/bot/migrator.rb
CHANGED
@@ -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 = nil)
|
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
|
data/lib/kybus/bot/version.rb
CHANGED
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
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gilberto Vargas
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
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
|