Class: YATelegramBot::TelegramAPI::User

Inherits:
Hash
  • Object
show all
Defined in:
lib/ya_telegram_bot/telegram_api/user.rb

Overview

represents Telegram User

Constant Summary collapse

FIELDS =
[:id, :first_name, :last_name, :username].freeze

Instance Method Summary collapse

Constructor Details

#initialize(hash, bot = nil) ⇒ User

Returns a new instance of User.

Parameters:

  • hash (Hash)

    hash from json.

  • bot (Base) (defaults to: nil)

    for using api methods



13
14
15
16
17
# File 'lib/ya_telegram_bot/telegram_api/user.rb', line 13

def initialize(hash, bot = nil)
  super()
  @bot = bot
  FIELDS.each { |f| self[f] = hash[f.to_s] if hash[f.to_s] }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object

Examples:

user.first_name



22
23
24
25
# File 'lib/ya_telegram_bot/telegram_api/user.rb', line 22

def method_missing(m, *args)
  return self[m] if self[m] && args.empty?
  super
end

Instance Method Details

#send_photo(params = {}) ⇒ Object

uses bot to send photo to this user.

Parameters:

  • params (Hash) (defaults to: {})

    params for Base#send_photo. This method will only set :chat to self



44
45
46
47
48
49
# File 'lib/ya_telegram_bot/telegram_api/user.rb', line 44

def send_photo(params = {})
  fail InitWithoutBot unless @bot

  params[:chat] = id
  @bot.send_photo params
end

#send_text(params = {}) ⇒ Object

uses bot to send text to this user.

Parameters:

  • params (Hash) (defaults to: {})

    params for Base#send_text. This method will only set :chat to self



32
33
34
35
36
37
# File 'lib/ya_telegram_bot/telegram_api/user.rb', line 32

def send_text(params = {})
  fail InitWithoutBot unless @bot

  params[:chat] = id
  @bot.send_text params
end