Class: Telegem::Types::Message

Inherits:
BaseType
  • Object
show all
Defined in:
lib/api/types.rb

Constant Summary collapse

COMMON_FIELDS =
%w[message_id from chat date edit_date 
text caption entities caption_entities 
audio document photo sticker video voice 
video_note contact location venue 
new_chat_members left_chat_member 
new_chat_title new_chat_photo 
delete_chat_photo group_chat_created 
supergroup_chat_created channel_chat_created 
migrate_to_chat_id migrate_from_chat_id 
pinned_message invoice successful_payment 
connected_website reply_markup via_bot 
forward_from forward_from_chat 
forward_from_message_id forward_signature 
forward_sender_name forward_date reply_to_message 
media_group_id author_signature 
has_protected_content].freeze

Instance Attribute Summary

Attributes inherited from BaseType

#_raw_data

Instance Method Summary collapse

Methods inherited from BaseType

#inspect, #method_missing, #respond_to_missing?, #to_h, #to_s

Constructor Details

#initialize(data) ⇒ Message

Returns a new instance of Message.



196
197
198
199
200
201
202
203
204
# File 'lib/api/types.rb', line 196

def initialize(data)
  super(data)
  
  COMMON_FIELDS.each do |field|
    define_accessor(field.to_sym)
  end
  
  convert_complex_fields
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Telegem::Types::BaseType

Instance Method Details

#command?Boolean

Returns:

  • (Boolean)


206
207
208
209
210
211
# File 'lib/api/types.rb', line 206

def command?
  return false unless text && entities
  
  entities.any? { |e| e.type == 'bot_command' && 
                     text[e.offset, e.length]&.start_with?('/') }
end

#command_argsObject



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/api/types.rb', line 226

def command_args
  return nil unless command?
  
  command_entity = entities.find { |e| e.type == 'bot_command' }
  return nil unless command_entity
  
  args_start = command_entity.offset + command_entity.length
  remaining = text[args_start..-1]
  
  next_entity = entities.select { |e| e.offset >= args_start }
                        .min_by(&:offset)
  
  if next_entity
    args_end = next_entity.offset - 1
    text[args_start..args_end]&.strip
  else
    remaining&.strip
  end
end

#command_nameObject



213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/api/types.rb', line 213

def command_name
  return nil unless command?
  
  command_entity = entities.find { |e| e.type == 'bot_command' }
  return nil unless command_entity
  
  cmd = text[command_entity.offset, command_entity.length]
  return nil if cmd.nil? || cmd.length <= 1
  
  cmd = cmd[1..-1]
  cmd.split('@').first.strip
end

#has_media?Boolean

Returns:

  • (Boolean)


250
251
252
# File 'lib/api/types.rb', line 250

def has_media?
  !!(audio || document || photo || video || voice || video_note || sticker)
end

#media_typeObject



254
255
256
257
258
259
260
261
262
263
# File 'lib/api/types.rb', line 254

def media_type
  return :audio if audio
  return :document if document
  return :photo if photo
  return :video if video
  return :voice if voice
  return :video_note if video_note
  return :sticker if sticker
  nil
end

#reply?Boolean

Returns:

  • (Boolean)


246
247
248
# File 'lib/api/types.rb', line 246

def reply?
  !!reply_to_message
end