class Telegram::Controller

Attributes

bot_name[W]
bots[W]
message[W]
params[W]

Public Instance Methods

dispatch(action) click to toggle source
# File lib/telegram/controller.rb, line 17
def dispatch action
  @current_action = action
  self.send action

  render unless @has_response

  @current_action = nil

  # WIP rescue from
end
expose_instance_variables() click to toggle source

TEST

# File lib/telegram/controller.rb, line 39
def expose_instance_variables
  hash = {}
  variables  = instance_variables
  variables -= not_exposed_instance_variables.map { |v| :"@#{v}" }

  variables.each do |name|
    # cut @
    var_name = name[1..-1]
    hash[var_name] = instance_variable_get(name)
  end

  hash
end
not_exposed_instance_variables() click to toggle source
# File lib/telegram/controller.rb, line 29
def not_exposed_instance_variables
  [
    :bots, :message,
    :current_action, :rendered,
    :bot_name, :has_response,
  ]
end

Private Instance Methods

bot() click to toggle source
# File lib/telegram/controller.rb, line 80
def bot
  @bots[@bot_name]
end
bots() click to toggle source
# File lib/telegram/controller.rb, line 85
def bots
  @bots
end
default_response() click to toggle source
# File lib/telegram/controller.rb, line 106
def default_response
  { chat_id: @message.chat.id }
end
has_response?() click to toggle source
# File lib/telegram/controller.rb, line 56
def has_response?
  @has_response
end
params() click to toggle source
# File lib/telegram/controller.rb, line 95
def params
  @params
end
parse_modes() click to toggle source
# File lib/telegram/controller.rb, line 71
def parse_modes
  {
    markdown: 'Markdown',
    html:     'Html',
    text:     nil,
  }
end
render(opts = {}) click to toggle source
# File lib/telegram/controller.rb, line 61
def render opts = {}
  raise Telegram::Errors::Renderer::DoubleRenderError if @rendered

  response, response_format = Renderer.new(controller: self, action: @current_action).render(opts)
  respond_with response, parse_mode: parse_modes[response_format]

  @rendered = true
end
request() click to toggle source
# File lib/telegram/controller.rb, line 90
def request
  @message
end
respond_with(payload, rest = {}) click to toggle source
# File lib/telegram/controller.rb, line 111
def respond_with payload, rest = {}
  response = payload.clone
  # WIP угадывать тип, посмотреть какие есть типы ответа
  unless response.is_a? Hash
    response = {text: response.to_s}
  end

  # :keyboard        => :reply_markup, call :keyboard
  # :inline_keyboard => :reply_markup, call :inline_keyboard
  if response.has_key? :keyboard
    response[:reply_markup] = keyboard(response[:keyboard])
    response.delete :keyboard
  end

  if response.has_key? :inline_keyboard
    response[:reply_markup] = inline_keyboard(response[:inline_keyboard])
    response.delete :inline_keyboard
  end

  if response.has_key?(:remove_keyboard) && response[:remove_keyboard]
    response[:reply_markup] = remove_keyboard
    response.delete :remove_keyboard
  end

  send_message default_response.merge(response).merge(rest)
end
send_message(*args) click to toggle source
# File lib/telegram/controller.rb, line 100
def send_message *args
  bot.api.send_message *args
  @has_response = true
end