Class: Telegram::TelegramMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/telegram/models.rb

Overview

Telegram Message Model

See Also:

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, event) ⇒ TelegramMessage

Create a new tgmessage instance

Parameters:

  • client (Client)

    Root client instance

  • event (Event)

    Root event instance

See Also:

Since:

  • 0.1.0


290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/telegram/models.rb', line 290

def initialize(client, event)
  @event = event
  
  @id = event.id
  @raw = event.message.text
  @time = event.time
  @content_type = event.message.type

  @raw_sender = event.message.raw_from
  @raw_receiver = event.message.raw_to

  @user = @sender = event.message.from
  @receiver = event.message.to

  @target =
    begin
      case @receiver.type
        when 'user'
          @sender
        when 'chat', 'encr_chat'
          @receiver
        end
    rescue NoMethodError
      @sender
    end
end

Instance Attribute Details

#clientClient (readonly)

Returns Root client instance.

Returns:

  • (Client)

    Root client instance

Since:

  • 0.1.0


260
261
262
# File 'lib/telegram/models.rb', line 260

def client
  @client
end

#content_typeString (readonly)

Returns Content type.

Returns:

Since:

  • 0.1.0


278
279
280
# File 'lib/telegram/models.rb', line 278

def content_type
  @content_type
end

#idInteger (readonly)

Returns Message identifier.

Returns:

  • (Integer)

    Message identifier

Since:

  • 0.1.0


266
267
268
# File 'lib/telegram/models.rb', line 266

def id
  @id
end

#rawString (readonly)

Returns Raw string of the text.

Returns:

  • (String)

    Raw string of the text

Since:

  • 0.1.0


263
264
265
# File 'lib/telegram/models.rb', line 263

def raw
  @raw
end

#raw_targetString (readonly)

Returns:

Since:

  • 0.1.0


275
276
277
# File 'lib/telegram/models.rb', line 275

def raw_target
  @raw_target
end

#targetTelegramChat, TelegramContact (readonly)

Returns:

Since:

  • 0.1.0


282
283
284
# File 'lib/telegram/models.rb', line 282

def target
  @target
end

#timeTime (readonly)

Returns Time message received.

Returns:

  • (Time)

    Time message received

Since:

  • 0.1.0


269
270
271
# File 'lib/telegram/models.rb', line 269

def time
  @time
end

#userTelegramContact (readonly)

Returns The contact who sent this message.

Returns:

Since:

  • 0.1.0


272
273
274
# File 'lib/telegram/models.rb', line 272

def user
  @user
end

Instance Method Details

#membersObject

Since:

  • 0.1.0


353
354
355
356
357
358
359
360
361
362
# File 'lib/telegram/models.rb', line 353

def members
  contact_list = []
  if @target.class == TelegramContact
    contact_list << @target
  else
    contact_list = @target.members
  end

  contact_list
end

#reply(type, content, target = nil, &callback) ⇒ Object

Reply a message to the chat

Parameters:

  • type (Symbol)

    Type of the message (either of :text, :sticker, :image)

  • content (String)

    Content to send a message

  • target (TelegramChat) (defaults to: nil)

    Specify a TelegramChat to send

  • target (TelegramContact) (defaults to: nil)

    Specify a TelegramContact to send

  • callback (Block)

    Callback block that will be called when finished

Since:

  • 0.1.0


334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/telegram/models.rb', line 334

def reply(type, content, target=nil, &callback)
  target = @target if target.nil?

  case type
  when :text
    target.send_message(content, self, &callback)
  when :image
    option = nil
    content, option = content if content.class == Array
    if content.include?('http')
      target.method(:send_image_url).call(content, option, self, &callback)
    else
      target.method(:send_image).call(content, self, &callback)
    end
  when :video
    target.send_video(content, self, &callback)
  end
end

#reply_user(type, content, &callback) ⇒ Object

This method is abstract.

Reply a message to the sender (peer to peer)

Parameters:

  • type (Symbol)

    Type of the message (either of :text, :sticker, :image)

  • content (String)

    Content to send a message

  • callback (Block)

    Callback block that will be called when finished

Since:

  • 0.1.0


322
323
324
# File 'lib/telegram/models.rb', line 322

def reply_user(type, content, &callback)

end

#to_sString

Convert Telegram::TelegramMessage instance to the string format

Returns:

Since:

  • 0.1.0


367
368
369
# File 'lib/telegram/models.rb', line 367

def to_s
  "<TelegramMessage id=#{@id} raw=#{@raw} time=#{@time} user=#{@user} target=#{@target} raw_target=#{@raw_target}>"
end