Class: YATelegramBot::TelegramAPI::Chat

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

Overview

represents Telegram Chat

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(hash, bot = nil) ⇒ Chat

Returns a new instance of Chat.

Parameters:

  • hash (Hash)

    hash from json.

  • bot (Base) (defaults to: nil)

    for using api methods



13
14
15
16
17
18
19
# File 'lib/ya_telegram_bot/telegram_api/chat.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] }

  self[:type] = self[:type].to_sym
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object

Examples:

chat.type



24
25
26
27
# File 'lib/ya_telegram_bot/telegram_api/chat.rb', line 24

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 chat.

Parameters:

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

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



46
47
48
49
50
51
# File 'lib/ya_telegram_bot/telegram_api/chat.rb', line 46

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 chat.

Parameters:

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

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



34
35
36
37
38
39
# File 'lib/ya_telegram_bot/telegram_api/chat.rb', line 34

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

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