Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional ReplyKeyboardMarkup creation depending on specific #8

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
kanal-plugins-batteries_bridge (0.1.0)
kanal-plugins-batteries_bridge (0.1.1)
kanal (>= 0.4.2)
kanal-interfaces-telegram (>= 0.3.4)

Expand Down Expand Up @@ -42,70 +42,72 @@ GEM
ice_nine (0.11.2)
jaro_winkler (1.5.4)
json (2.6.3)
kanal (0.4.3)
kanal-interfaces-telegram (0.3.4)
kanal (= 0.4.3)
kanal (0.7.0)
kanal-interfaces-telegram (0.4.2)
kanal (>= 0.7.0)
telegram-bot-ruby (= 1.0.0)
kramdown (2.4.0)
rexml
kramdown-parser-gfm (1.1.0)
kramdown (~> 2.0)
multipart-post (2.3.0)
nokogiri (1.14.2-x86_64-darwin)
nokogiri (1.14.3-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.14.2-x86_64-linux)
nokogiri (1.14.3-x86_64-linux)
racc (~> 1.4)
parallel (1.22.1)
parser (3.2.1.1)
parallel (1.23.0)
parser (3.2.2.1)
ast (~> 2.4.1)
racc (1.6.2)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.7.0)
rbs (2.8.4)
regexp_parser (2.8.0)
reverse_markdown (2.1.1)
nokogiri
rexml (3.2.5)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.1)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.2)
rspec-expectations (3.12.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.4)
rspec-mocks (3.12.5)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.0)
rubocop (1.48.1)
rubocop (1.50.2)
json (~> 2.3)
parallel (~> 1.10)
parser (>= 3.2.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.26.0, < 2.0)
rubocop-ast (>= 1.28.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.27.0)
rubocop-ast (1.28.0)
parser (>= 3.2.1.0)
ruby-debug-ide (0.7.3)
rake (>= 0.8.1)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
solargraph (0.48.0)
solargraph (0.49.0)
backport (~> 1.2)
benchmark
bundler (>= 1.17.2)
bundler (~> 2.0)
diff-lcs (~> 1.4)
e2mmap
jaro_winkler (~> 1.5)
kramdown (~> 2.3)
kramdown-parser-gfm (~> 1.1)
parser (~> 3.0)
reverse_markdown (>= 1.0.5, < 3)
rubocop (>= 0.52)
rbs (~> 2.0)
reverse_markdown (~> 2.0)
rubocop (~> 1.38)
thor (~> 1.0)
tilt (~> 2.0)
yard (~> 0.9, >= 0.9.24)
Expand All @@ -117,9 +119,7 @@ GEM
thor (1.2.1)
tilt (2.1.0)
unicode-display_width (2.4.2)
webrick (1.7.0)
yard (0.9.28)
webrick (~> 1.7.0)
yard (0.9.34)
zeitwerk (2.6.7)

PLATFORMS
Expand Down
3 changes: 1 addition & 2 deletions lib/kanal/plugins/batteries_bridge/bridges/bridge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ def attach_hooks
if input.source == _source
_output_converters.each do |converter|
next if output.send(converter.from_param).nil?

output.send("#{converter.to_param}=", converter.block.call(output.send(converter.from_param)))
output.send("#{converter.to_param}=", converter.block.call(output.send(converter.from_param), input, output))
rescue Exception => e
_logger.error "BatteriesBridge output param converter #{self.class} tried to convert #{converter.from_param} to #{converter.to_param} and experienced an error: #{e}"

Expand Down
34 changes: 25 additions & 9 deletions lib/kanal/plugins/batteries_bridge/bridges/telegram_bridge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,38 @@ def setup
val
end

output_convert :keyboard, :tg_reply_markup do |keyboard_object|
output_convert :keyboard, :tg_reply_markup do |keyboard_object, input, output|
nil if keyboard_object.nil? || !keyboard_object.to_a.count.positive?

inline_keyboard = []
if output.specifics.has?(:tg_reply_keyboard) == false || output.specifics.get(:tg_reply_keyboard) == false
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check for :tg_reply_keyboard == true
in this case - use reply keyboard

Otherwise, use inline keyboard by default

And only one check for get == nil is sufficient

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inline_keyboard = []

keyboard_object.to_a.each do |row_of_button_names|
row_of_buttons = []
keyboard_object.to_a.each do |row_of_button_names|
row_of_buttons = []

row_of_button_names.each do |button_name|
row_of_buttons << Telegram::Bot::Types::InlineKeyboardButton.new(text: button_name, callback_data: button_name)
row_of_button_names.each do |button_name|
row_of_buttons << Telegram::Bot::Types::InlineKeyboardButton.new(text: button_name, callback_data: button_name)
end

inline_keyboard << row_of_buttons
end

inline_keyboard << row_of_buttons
end
Telegram::Bot::Types::InlineKeyboardMarkup.new inline_keyboard: inline_keyboard
else
regular_keyboard = []

keyboard_object.to_a.each do |row_of_button_names|
row_of_buttons = []

row_of_button_names.each do |button_name|
row_of_buttons << Telegram::Bot::Types::KeyboardButton.new(text: button_name)
end

Telegram::Bot::Types::InlineKeyboardMarkup.new inline_keyboard: inline_keyboard
regular_keyboard << row_of_buttons
end

Telegram::Bot::Types::ReplyKeyboardMarkup.new keyboard: regular_keyboard
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
it "successfully converts parameters to telegram bridge parameters" do
core = Kanal::Core::Core.new

core.logger.add_logger Logger.new STDOUT

core.register_plugin Kanal::Plugins::Batteries::BatteriesPlugin.new

core.register_input_parameter :tg_text, readonly: true
Expand Down Expand Up @@ -36,7 +38,7 @@
core.register_plugin bb_plugin

core.router.configure do
on :flow, :any do
on :body, contains: "Test" do
respond do
body "Output text"
image "/some/path/to/image.jpg"
Expand All @@ -48,6 +50,16 @@
end
end
end

on :body, contains: "Specifics" do
respond do
keyboard.build do
row "First", "Second"
end

specifics.add :tg_reply_keyboard, true
end
end
end

input_conversion_check = lambda do |inp|
Expand Down Expand Up @@ -92,6 +104,20 @@
expect(output.tg_video_path).to eq "/some/path/to/video.mp4"
expect(output.tg_document_path).to eq "/some/path/to/document.doc"
expect(output.tg_reply_markup.instance_of?(::Telegram::Bot::Types::InlineKeyboardMarkup)).to eq true

input_conversion_check = lambda do |inp|
expect(inp.body).to eq "Specifics"
end

core.hooks.attach :input_before_router do |input|
input_conversion_check.call input
end

input = core.create_input
input.tg_text = "Specifics"
input.source = :telegram
core.router.consume_input input
expect(output.tg_reply_markup.instance_of?(::Telegram::Bot::Types::ReplyKeyboardMarkup)).to eq true
end
end