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
18 changes: 9 additions & 9 deletions lib/kanal/plugins/batteries_bridge/bridges/telegram_bridge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,34 +66,34 @@ def setup
output_convert :keyboard, :tg_reply_markup do |keyboard_object, input, output|
nil if keyboard_object.nil? || !keyboard_object.to_a.count.positive?

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

Choose a reason for hiding this comment

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

Better check explicitly for true because any object or something passed here will be considered truthy. Only falsy values are false, nil.

Just in case. I doubt there will be the case but just to be sure we won't have any side effects

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::InlineKeyboardButton.new(text: button_name, callback_data: button_name)
row_of_buttons << Telegram::Bot::Types::KeyboardButton.new(text: button_name)
end

inline_keyboard << row_of_buttons
regular_keyboard << row_of_buttons
end

Telegram::Bot::Types::InlineKeyboardMarkup.new inline_keyboard: inline_keyboard
Telegram::Bot::Types::ReplyKeyboardMarkup.new keyboard: regular_keyboard
else
regular_keyboard = []
inline_keyboard = []
Copy link
Owner

Choose a reason for hiding this comment

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

This whole if/else code can be refactored to reduce copypaste


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)
row_of_buttons << Telegram::Bot::Types::InlineKeyboardButton.new(text: button_name, callback_data: button_name)
end

regular_keyboard << row_of_buttons
inline_keyboard << row_of_buttons
end

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

core.logger.add_logger Logger.new STDOUT
# core.logger.add_logger Logger.new STDOUT

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

Expand Down