class Telegram::Bot::Client

Attributes

command[R]
message[R]
update[R]

Public Class Methods

new(handler) click to toggle source
# File lib/telegram/bot/client.rb, line 7
def initialize(handler)
  @handler = handler.new
  @handler.api = Telegram::Bot::Api.new
  @handler.botan = Telegram::Bot::Botan.new
end

Public Instance Methods

handle(params) click to toggle source
# File lib/telegram/bot/client.rb, line 13
def handle(params)
  parse_request(params)
  invoke
end
invoke() click to toggle source
# File lib/telegram/bot/client.rb, line 18
def invoke
  @handler.update = @update
  @handler.message = @message
  @handler.command = @command

  if !@command.nil? && @handler.respond_to?("cmd_#{@command}")
    @handler.public_send("cmd_#{@command}")
  else
    @handler.public_send(:cmd_fallback) if @handler.respond_to?(:cmd_fallback)
  end
end

Private Instance Methods

parse_request(params) click to toggle source
# File lib/telegram/bot/client.rb, line 32
def parse_request(params)
  @update = Types::Update.new(params)

  if params.has_key? :callback_query
    @message = @update.callback_query.message
    @command = pull_command(@update.callback_query.data)
  else
    message_key = PAYLOAD_KEYS.find { |key| params.has_key?(key.to_sym) }

    if !message_key.nil? && @update.respond_to?(message_key)
      @message = @update.send(message_key)
      @command = pull_command(@message.text)
      @command = pull_command(@message.reply_to_message.text) if !@command && @message.reply_to_message
    end
  end

  raise Telegram::Bot::ParseError.new('Can not parse request') unless @message
end
pull_command(text) click to toggle source
# File lib/telegram/bot/client.rb, line 51
def pull_command(text)
  text =  text.gsub(Telegram::Bot.configuration.name, '') if Telegram::Bot.configuration.name && !text.nil?
  !text.nil? && text.index('/') == 0 ? text.downcase.split(' ').first.gsub('/', '') : nil
end