Class: YATelegramBot::TelegramAPI::Message

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

Overview

represents incoming messages

Constant Summary collapse

TYPES =
[:text, :audio, :photo, :document, :video, :voice, :contact, :location].freeze

Instance Method Summary collapse

Constructor Details

#initialize(hash_message, bot = nil) ⇒ Message

Returns a new instance of Message.

Parameters:

  • hash_message (Hash)
  • bot (Base) (defaults to: nil)

    for using api like sending replies



15
16
17
18
# File 'lib/ya_telegram_bot/telegram_api/message.rb', line 15

def initialize(hash_message, bot = nil)
  @bot = bot
  merge! hash_for_merging(hash_message)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object

Examples:

message.text




23
24
25
26
# File 'lib/ya_telegram_bot/telegram_api/message.rb', line 23

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

Instance Method Details

#reply(params = {}) ⇒ Object

send reply to this message via bot from #initialize

params values:

  • :text [String]

  • :as_plain_message [Boolean] default: true. If it’s set, method won’t set :reply_to parameter

Examples:

message.reply(text: ‘Hi, friend!’, markdown: true)




52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ya_telegram_bot/telegram_api/message.rb', line 52

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

  params[:chat] = self[:chat]['id']

  params[:as_plain_message] = true unless params.key? :as_plain_message
  params[:reply_to] = self[:id] unless params[:as_plain_message]
  param.delete :as_plain_message

  @bot.send_text params
end

#typeSymbol

Returns type of a message (see Message::TYPES).

Returns:

  • (Symbol)

    type of a message (see Message::TYPES)



39
40
41
# File 'lib/ya_telegram_bot/telegram_api/message.rb', line 39

def type
  TYPES.find { |t| self[t] }
end