Class: TelegramOnSteroids::Action

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/telegram_on_steroids/action.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request:, client:, session:) ⇒ Action

Returns a new instance of Action.



29
30
31
32
33
# File 'lib/telegram_on_steroids/action.rb', line 29

def initialize(request:, client:, session:)
  @request = request
  @client = client
  @session = session
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



7
8
9
# File 'lib/telegram_on_steroids/action.rb', line 7

def action
  @action
end

#clientObject

Returns the value of attribute client.



7
8
9
# File 'lib/telegram_on_steroids/action.rb', line 7

def client
  @client
end

#redirect_toObject

Returns the value of attribute redirect_to.



71
72
73
# File 'lib/telegram_on_steroids/action.rb', line 71

def redirect_to
  @redirect_to
end

#requestObject

Returns the value of attribute request.



7
8
9
# File 'lib/telegram_on_steroids/action.rb', line 7

def request
  @request
end

#sessionObject

Returns the value of attribute session.



7
8
9
# File 'lib/telegram_on_steroids/action.rb', line 7

def session
  @session
end

Class Method Details

.on(name, &block) ⇒ Object

Raises:

  • (StandardError)


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/telegram_on_steroids/action.rb', line 11

def on(name, &block)
  raise StandardError, "#{name} not allowed callback" unless TelegramOnSteroids::UPDATE_TYPES.include?(name.to_s)

  define_method("on_#{name.to_s}") do
    instance_variable_set('@on_callback', block)
  end

  define_method("__run_on_#{name.to_s}") do
    return respond_with_keyboard if request.params.callback? && pagination_callback? && current_keyboard

    instance_eval(&block)
  end
end

Instance Method Details

#answer_callback_query(**params) ⇒ Object



52
53
54
# File 'lib/telegram_on_steroids/action.rb', line 52

def answer_callback_query(**params)
  client.answer_callback_query callback_query_id: request.params.to_h.dig('callback_query', 'id'), **params
end

#current_keyboardObject



60
61
62
63
64
# File 'lib/telegram_on_steroids/action.rb', line 60

def current_keyboard
  return unless session.read(:keyboard)

  Object.const_get(session.read(:keyboard))
end

#pagination_callback?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/telegram_on_steroids/action.rb', line 56

def pagination_callback?
  request.params.callback_data =~ /page/
end

#respond_with_keyboard(keyboard: current_keyboard) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/telegram_on_steroids/action.rb', line 37

def respond_with_keyboard(keyboard: current_keyboard)
  keyboard_instance = keyboard.is_a?(Class) ? keyboard.new(request:, action: self) : keyboard
  inline_keyboard = keyboard_instance.to_telegram_format
  message_id = request.params.to_h.dig('callback_query', 'message', 'message_id')
  text = keyboard_instance.text
  session.write(:current_page, 1) unless keyboard == current_keyboard

  if message_id
    client.edit_message_text message_id:, text:, reply_markup: { inline_keyboard: }
  else
    client.send_message text:, reply_markup: { inline_keyboard: }
  end
  session.write(:keyboard, keyboard.is_a?(Class) ? keyboard.name : keyboard.class.name)
end