Class: BotTelegram::Custom::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/tramway/bots/telegram/message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text: nil, file: nil, reply_markup: nil, inline_keyboard: nil) ⇒ Message

Returns a new instance of Message.



8
9
10
11
12
13
14
15
# File 'lib/tramway/bots/telegram/message.rb', line 8

def initialize(text: nil, file: nil, reply_markup: nil, inline_keyboard: nil)
  raise 'You set text: argument for message' unless text.present?

  @text = text
  @file = file
  @reply_markup = reply_markup
  @inline_keyboard = inline_keyboard
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



6
7
8
# File 'lib/tramway/bots/telegram/message.rb', line 6

def file
  @file
end

Instance Method Details

#build_options(button) ⇒ Object



17
18
19
20
21
# File 'lib/tramway/bots/telegram/message.rb', line 17

def build_options(button)
  button_options = { text: button[0] }
  button_options.merge! callback_data: button[1][:data].to_json if button[1][:data].present?
  Telegram::Bot::Types::InlineKeyboardButton.new(**button_options)
end

#optionsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tramway/bots/telegram/message.rb', line 23

def options
  arguments = {}
  arguments.merge!(text: @text) if @text.present?
  if @reply_markup.present?
    arguments.merge!(reply_markup: Telegram::Bot::Types::ReplyKeyboardMarkup.new(**@reply_markup))
  elsif @inline_keyboard.present?
    keyboard = @inline_keyboard.map do |button|
      if button[0].is_a? Array
        button.map do |b|
          build_options b
        end
      else
        build_options button
      end
    end
    arguments.merge!(reply_markup: Telegram::Bot::Types::InlineKeyboardMarkup.new(inline_keyboard: keyboard))
  end
  arguments
end