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



17
18
19
20
# File 'lib/ya_telegram_bot/telegram_api/message.rb', line 17

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



25
26
27
28
# File 'lib/ya_telegram_bot/telegram_api/message.rb', line 25

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 true, method won’t set :reply_to parameter

TODO: what about files?

Examples:

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



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ya_telegram_bot/telegram_api/message.rb', line 56

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)



41
42
43
# File 'lib/ya_telegram_bot/telegram_api/message.rb', line 41

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