kybus-bot 0.5.1 → 0.8.1
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 +4 -4
- data/lib/kybus/bot/adapters/debug.rb +68 -10
- data/lib/kybus/bot/adapters/discord.rb +9 -9
- data/lib/kybus/bot/adapters/telegram.rb +25 -98
- data/lib/kybus/bot/adapters/telegram_file.rb +59 -0
- data/lib/kybus/bot/adapters/telegram_message.rb +61 -0
- data/lib/kybus/bot/base.rb +39 -210
- data/lib/kybus/bot/{command_definition.rb → command/command.rb} +18 -22
- data/lib/kybus/bot/command/command_definition.rb +29 -0
- data/lib/kybus/bot/command/command_state.rb +76 -0
- data/lib/kybus/bot/command/command_state_factory.rb +69 -0
- data/lib/kybus/bot/command/execution_context.rb +63 -0
- data/lib/kybus/bot/command_executor.rb +131 -0
- data/lib/kybus/bot/dsl_methods.rb +79 -0
- data/lib/kybus/bot/message.rb +5 -8
- data/lib/kybus/bot/test.rb +18 -6
- data/lib/kybus/bot/version.rb +1 -1
- metadata +14 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7751ec07a20b41b400f7d49dbc5dcb8b4951a3dd07a4ea32f0cccefb9985336e
|
4
|
+
data.tar.gz: 1806738e2740b0d626b19a11ae843503fb5171516ef80a64d644ab70c3520dc1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: deabacf971edc2c6b8dbe52736ba5c1bcb6a95aa1bc4253bef5b13de7d37b10068111c618aa5f05f171c0bfb721a546e4a4a3f66781a6773a4c318c644338a3a
|
7
|
+
data.tar.gz: e8b73239aa929d94a85cc831eb8515411a76e6f19fbb1a5d6d3fff7b2157130f328de70e1b0f27e08c8f5af1a5696c6d9da52c56ef6e371edf252bfea18cf4ee
|
@@ -11,12 +11,38 @@ module Kybus
|
|
11
11
|
# Wraps a debugging message inside a class.
|
12
12
|
class DebugMessage < Kybus::Bot::Message
|
13
13
|
# It receives a string with the raw text and the id of the channel
|
14
|
-
attr_reader :attachment
|
14
|
+
attr_reader :attachment, :message_id
|
15
|
+
|
16
|
+
class DebugFile
|
17
|
+
def initialize(path)
|
18
|
+
@path = path
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_json(obj)
|
22
|
+
to_h.to_json(obj)
|
23
|
+
end
|
24
|
+
|
25
|
+
def file_name
|
26
|
+
@path
|
27
|
+
end
|
28
|
+
|
29
|
+
def download
|
30
|
+
File.read(@path)
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_h
|
34
|
+
{ path: @path }
|
35
|
+
end
|
36
|
+
end
|
15
37
|
|
16
38
|
def initialize(text, channel, attachment = nil)
|
39
|
+
super()
|
17
40
|
@text = text
|
18
41
|
@channel = channel
|
19
42
|
@attachment = attachment
|
43
|
+
@@message_id ||= 0
|
44
|
+
@@message_id += 1
|
45
|
+
@message_id = @@message_id + 1
|
20
46
|
end
|
21
47
|
|
22
48
|
# Returns the channel id
|
@@ -36,6 +62,14 @@ module Kybus
|
|
36
62
|
def has_attachment?
|
37
63
|
!!attachment
|
38
64
|
end
|
65
|
+
|
66
|
+
def reply?
|
67
|
+
@reply
|
68
|
+
end
|
69
|
+
|
70
|
+
def is_private?
|
71
|
+
true
|
72
|
+
end
|
39
73
|
end
|
40
74
|
|
41
75
|
# This class simulates a message chat with a user.
|
@@ -66,7 +100,7 @@ module Kybus
|
|
66
100
|
DebugMessage.new(@pending_messages.shift, @name)
|
67
101
|
end
|
68
102
|
|
69
|
-
def send_data(message,
|
103
|
+
def send_data(message, _attachment)
|
70
104
|
return unless @echo
|
71
105
|
|
72
106
|
puts "Sending message to channel: #{@name}"
|
@@ -79,6 +113,7 @@ module Kybus
|
|
79
113
|
def answer(message, attachment = nil)
|
80
114
|
send_data(message, attachment)
|
81
115
|
@state = :open
|
116
|
+
DebugMessage.new(message, @name)
|
82
117
|
end
|
83
118
|
end
|
84
119
|
|
@@ -92,6 +127,8 @@ module Kybus
|
|
92
127
|
end
|
93
128
|
end
|
94
129
|
|
130
|
+
attr_accessor :last_message
|
131
|
+
|
95
132
|
# It receives a hash with the configurations:
|
96
133
|
# - name: the name of the channel
|
97
134
|
# - channels a key value, where the key is a name and the value the
|
@@ -111,12 +148,8 @@ module Kybus
|
|
111
148
|
loop do
|
112
149
|
raise NoMoreMessageException if @channels.values.all?(&:empty?)
|
113
150
|
|
114
|
-
|
115
|
-
return
|
116
|
-
|
117
|
-
# :nocov: #
|
118
|
-
sleep(1)
|
119
|
-
# :nocov: #
|
151
|
+
open_channel = @channels.values.find(&:open?)
|
152
|
+
return @last_message = open_channel.read_message if open_channel
|
120
153
|
end
|
121
154
|
end
|
122
155
|
|
@@ -131,7 +164,7 @@ module Kybus
|
|
131
164
|
end
|
132
165
|
|
133
166
|
# interface for sending video
|
134
|
-
def send_video(channel_name, video_url)
|
167
|
+
def send_video(channel_name, video_url, _caption = nil)
|
135
168
|
channel(channel_name).answer("VIDEO: #{video_url}")
|
136
169
|
end
|
137
170
|
|
@@ -141,10 +174,35 @@ module Kybus
|
|
141
174
|
end
|
142
175
|
|
143
176
|
# interface for sending image
|
144
|
-
def send_image(channel_name, image_url)
|
177
|
+
def send_image(channel_name, image_url, _caption = nil)
|
145
178
|
channel(channel_name).answer("IMG: #{image_url}")
|
146
179
|
end
|
147
180
|
|
181
|
+
# interface for sending image
|
182
|
+
def send_document(channel_name, doc_url)
|
183
|
+
channel(channel_name).answer("DOC: #{doc_url}")
|
184
|
+
end
|
185
|
+
|
186
|
+
def file_builder(data)
|
187
|
+
case data
|
188
|
+
when DebugMessage::DebugFile
|
189
|
+
data
|
190
|
+
when Hash
|
191
|
+
DebugMessage::DebugFile.new(data[:path])
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
include Kybus::Logger
|
196
|
+
|
197
|
+
def message_builder(msg)
|
198
|
+
log_info('Building message object', msg:, msg_class: msg.class.name)
|
199
|
+
msg
|
200
|
+
end
|
201
|
+
|
202
|
+
def mention(user)
|
203
|
+
"@#{user}"
|
204
|
+
end
|
205
|
+
|
148
206
|
# changes echo config
|
149
207
|
def echo=(toogle)
|
150
208
|
@channels.each { |_, channel| channel.echo = toogle }
|
@@ -11,6 +11,7 @@ module Kybus
|
|
11
11
|
class DiscordMessage < Kybus::Bot::Message
|
12
12
|
# It receives a string with the raw text and the id of the channel
|
13
13
|
def initialize(msg)
|
14
|
+
super
|
14
15
|
@message = msg
|
15
16
|
end
|
16
17
|
|
@@ -45,6 +46,9 @@ module Kybus
|
|
45
46
|
# This adapter is intended to be used on unit tests and development.
|
46
47
|
class Discord
|
47
48
|
include ::Kybus::Logger
|
49
|
+
|
50
|
+
attr_reader :last_message, :client
|
51
|
+
|
48
52
|
# It receives a hash with the configurations:
|
49
53
|
# - name: the name of the channel
|
50
54
|
# - channels a key value, where the key is a name and the value the
|
@@ -60,8 +64,6 @@ module Kybus
|
|
60
64
|
@client.run(:async)
|
61
65
|
end
|
62
66
|
|
63
|
-
attr_reader :client
|
64
|
-
|
65
67
|
def mention(id)
|
66
68
|
"<@!#{id}>"
|
67
69
|
end
|
@@ -70,17 +72,15 @@ module Kybus
|
|
70
72
|
def read_message
|
71
73
|
# take the first message from the first open message,
|
72
74
|
loop do
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
break
|
77
|
-
end
|
75
|
+
break unless @pool.empty?
|
76
|
+
|
77
|
+
sleep(0.1)
|
78
78
|
end
|
79
|
-
DiscordMessage.new(@pool.shift)
|
79
|
+
@last_message = DiscordMessage.new(@pool.shift)
|
80
80
|
end
|
81
81
|
|
82
82
|
# interface for sending messages
|
83
|
-
def send_message(channel_name, contents)
|
83
|
+
def send_message(channel_name, contents, _caption = nil)
|
84
84
|
puts "#{channel_name} => #{contents}" if @config['debug']
|
85
85
|
channel = @client.channel(channel_name)
|
86
86
|
if channel
|
@@ -1,100 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'telegram/bot'
|
4
|
+
require 'faraday'
|
5
|
+
require_relative 'telegram_file'
|
6
|
+
require_relative 'telegram_message'
|
4
7
|
|
5
8
|
module Kybus
|
6
9
|
module Bot
|
7
|
-
# :nodoc: #
|
8
10
|
module Adapter
|
9
|
-
# :nodoc: #
|
10
|
-
# Wraps a debugging message inside a class.
|
11
|
-
class TelegramMessage < Kybus::Bot::Message
|
12
|
-
# It receives a string with the raw text and the id of the channel
|
13
|
-
def initialize(message)
|
14
|
-
@message = message
|
15
|
-
end
|
16
|
-
|
17
|
-
def reply?
|
18
|
-
!!@message.reply_to_message
|
19
|
-
end
|
20
|
-
|
21
|
-
def replied_message
|
22
|
-
TelegramMessage.new(@message.reply_to_message)
|
23
|
-
end
|
24
|
-
|
25
|
-
# Returns the channel id
|
26
|
-
def channel_id
|
27
|
-
@message.chat.id
|
28
|
-
end
|
29
|
-
|
30
|
-
# Returns the message contents
|
31
|
-
def raw_message
|
32
|
-
@message.to_s
|
33
|
-
end
|
34
|
-
|
35
|
-
def is_private?
|
36
|
-
@message.chat.type == 'private'
|
37
|
-
end
|
38
|
-
|
39
|
-
def has_attachment?
|
40
|
-
!!@message.document
|
41
|
-
end
|
42
|
-
|
43
|
-
def attachment
|
44
|
-
@message.document
|
45
|
-
end
|
46
|
-
|
47
|
-
def user
|
48
|
-
@message.from.id
|
49
|
-
end
|
50
|
-
end
|
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
|
-
|
94
11
|
##
|
95
12
|
# This adapter is intended to be used on unit tests and development.
|
96
13
|
class Telegram
|
97
14
|
include ::Kybus::Logger
|
15
|
+
|
16
|
+
attr_reader :last_message
|
17
|
+
|
98
18
|
# It receives a hash with the configurations:
|
99
19
|
# - name: the name of the channel
|
100
20
|
# - channels a key value, where the key is a name and the value the
|
@@ -113,10 +33,12 @@ module Kybus
|
|
113
33
|
@client.listen do |message|
|
114
34
|
log_info('Received message', message: message.to_h,
|
115
35
|
from: message.from.to_h)
|
116
|
-
return TelegramMessage.new(message)
|
36
|
+
return @last_message = TelegramMessage.new(message)
|
117
37
|
end
|
118
38
|
rescue ::Telegram::Bot::Exceptions::ResponseError => e
|
39
|
+
# :nocov:
|
119
40
|
log_error('An error ocurred while calling to Telegram API', e)
|
41
|
+
# :nocov:
|
120
42
|
end
|
121
43
|
end
|
122
44
|
|
@@ -124,39 +46,44 @@ module Kybus
|
|
124
46
|
"[user](tg://user?id=#{id})"
|
125
47
|
end
|
126
48
|
|
127
|
-
|
128
49
|
# interface for sending messages
|
129
50
|
def send_message(channel_name, contents)
|
130
51
|
puts "#{channel_name} => #{contents}" if @config['debug']
|
131
52
|
@client.api.send_message(chat_id: channel_name, text: contents)
|
132
|
-
|
133
|
-
|
53
|
+
# :nocov:
|
54
|
+
rescue ::Telegram::Bot::Exceptions::ResponseError => e
|
55
|
+
return if e[:error_code] == '403'
|
134
56
|
end
|
57
|
+
# :nocov:
|
135
58
|
|
136
59
|
# interface for sending video
|
137
|
-
def send_video(channel_name, video_url)
|
138
|
-
file = Faraday::
|
139
|
-
@client.api.send_video(chat_id: channel_name,
|
60
|
+
def send_video(channel_name, video_url, comment = nil)
|
61
|
+
file = Faraday::FilePart.new(video_url, 'video/mp4')
|
62
|
+
@client.api.send_video(chat_id: channel_name, video: file, caption: comment)
|
140
63
|
end
|
141
64
|
|
142
65
|
# interface for sending uadio
|
143
66
|
def send_audio(channel_name, audio_url)
|
144
|
-
file = Faraday::
|
67
|
+
file = Faraday::FilePart.new(audio_url, 'audio/mp3')
|
145
68
|
@client.api.send_audio(chat_id: channel_name, audio: file)
|
146
69
|
end
|
147
70
|
|
148
71
|
# interface for sending image
|
149
|
-
def send_image(channel_name, image_url)
|
150
|
-
file = Faraday::
|
151
|
-
@client.api.send_photo(chat_id: channel_name, photo: file)
|
72
|
+
def send_image(channel_name, image_url, comment = nil)
|
73
|
+
file = Faraday::FilePart.new(image_url, 'image/jpeg')
|
74
|
+
@client.api.send_photo(chat_id: channel_name, photo: file, caption: comment)
|
152
75
|
end
|
153
76
|
|
154
77
|
# interface for sending document
|
155
78
|
def send_document(channel_name, image_url)
|
156
|
-
file = Faraday::
|
79
|
+
file = Faraday::FilePart.new(image_url, 'application/octect-stream')
|
157
80
|
@client.api.send_document(chat_id: channel_name, document: file)
|
158
81
|
end
|
159
82
|
|
83
|
+
def message_builder(raw_message)
|
84
|
+
TelegramMessage.new(raw_message)
|
85
|
+
end
|
86
|
+
|
160
87
|
def file_builder(file)
|
161
88
|
TelegramFile.new(file)
|
162
89
|
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'telegram/bot'
|
4
|
+
require 'faraday'
|
5
|
+
|
6
|
+
module Kybus
|
7
|
+
module Bot
|
8
|
+
# :nodoc: #
|
9
|
+
module Adapter
|
10
|
+
class TelegramFile
|
11
|
+
extend Kybus::DRY::ResourceInjector
|
12
|
+
attr_reader :id
|
13
|
+
|
14
|
+
def initialize(message)
|
15
|
+
@id = case message
|
16
|
+
when String
|
17
|
+
message
|
18
|
+
when Hash
|
19
|
+
message['id'] || message[:id]
|
20
|
+
when TelegramFile
|
21
|
+
message.id
|
22
|
+
else
|
23
|
+
message.file_id
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_h
|
28
|
+
{
|
29
|
+
provide: 'telegram',
|
30
|
+
id: @id
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
def cli
|
35
|
+
@cli ||= TelegramFile.resource(:cli)
|
36
|
+
end
|
37
|
+
|
38
|
+
def meta
|
39
|
+
@meta ||= cli.api.get_file(file_id: @id)
|
40
|
+
end
|
41
|
+
|
42
|
+
def original_name
|
43
|
+
meta.dig('result', 'file_name')
|
44
|
+
end
|
45
|
+
|
46
|
+
def file_name
|
47
|
+
original_name
|
48
|
+
end
|
49
|
+
|
50
|
+
def download
|
51
|
+
token = cli.api.token
|
52
|
+
file_path = meta.dig('result', 'file_path')
|
53
|
+
path = "https://api.telegram.org/file/bot#{token}/#{file_path}"
|
54
|
+
Faraday.get(path).body
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'telegram/bot'
|
4
|
+
require 'faraday'
|
5
|
+
|
6
|
+
module Kybus
|
7
|
+
module Bot
|
8
|
+
# :nodoc: #
|
9
|
+
module Adapter
|
10
|
+
# :nodoc: #
|
11
|
+
# Wraps a debugging message inside a class.
|
12
|
+
class TelegramMessage < Kybus::Bot::Message
|
13
|
+
# It receives a string with the raw text and the id of the channel
|
14
|
+
def initialize(message)
|
15
|
+
super()
|
16
|
+
@message = message
|
17
|
+
end
|
18
|
+
|
19
|
+
def reply?
|
20
|
+
@message.respond_to?(:reply_to_message) && @message.reply_to_message
|
21
|
+
end
|
22
|
+
|
23
|
+
def replied_message
|
24
|
+
TelegramMessage.new(@message.reply_to_message)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Returns the channel id
|
28
|
+
def channel_id
|
29
|
+
@message.chat.id
|
30
|
+
end
|
31
|
+
|
32
|
+
def message_id
|
33
|
+
@message.respond_to?(:message_id) ? @message.message_id : @message['result']['message_id']
|
34
|
+
end
|
35
|
+
|
36
|
+
# Returns the message contents
|
37
|
+
def raw_message
|
38
|
+
@message.to_s
|
39
|
+
end
|
40
|
+
|
41
|
+
def is_private?
|
42
|
+
@message.chat.type == 'private'
|
43
|
+
end
|
44
|
+
|
45
|
+
def has_attachment?
|
46
|
+
!!attachment
|
47
|
+
end
|
48
|
+
|
49
|
+
def attachment
|
50
|
+
(@message.respond_to?(:document) && @message&.document) ||
|
51
|
+
(@message.respond_to?(:photo) && @message.photo&.last) ||
|
52
|
+
(@message.respond_to?(:audio) && @message&.audio)
|
53
|
+
end
|
54
|
+
|
55
|
+
def user
|
56
|
+
@message.from.id
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|