Class: Telegem::Types::Message

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

Instance Attribute Summary collapse

Attributes inherited from BaseType

#_raw_data

Instance Method Summary collapse

Methods inherited from BaseType

#method_missing, #respond_to_missing?

Constructor Details

#initialize(data) ⇒ Message

Returns a new instance of Message.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/api/types.rb', line 107

def initialize(data)
  super(data)
  
  @message_id = data['message_id']
  @from = User.new(data['from']) if data['from']
  @chat = Chat.new(data['chat']) if data['chat']
  @date = Time.at(data['date']) if data['date']
  @text = data['text']
  
  if data['entities']
    @entities = data['entities'].map { |e| MessageEntity.new(e) }
  end
  
  @reply_markup = data['reply_markup']
  @via_bot = User.new(data['via_bot']) if data['via_bot']
  @forward_from = User.new(data['forward_from']) if data['forward_from']
  @forward_from_chat = Chat.new(data['forward_from_chat']) if data['forward_from_chat']
end

Dynamic Method Handling

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

Instance Attribute Details

#chatObject (readonly)

Returns the value of attribute chat.



104
105
106
# File 'lib/api/types.rb', line 104

def chat
  @chat
end

#dateObject (readonly)

Returns the value of attribute date.



104
105
106
# File 'lib/api/types.rb', line 104

def date
  @date
end

#entitiesObject (readonly)

Returns the value of attribute entities.



104
105
106
# File 'lib/api/types.rb', line 104

def entities
  @entities
end

#forward_fromObject (readonly)

Returns the value of attribute forward_from.



104
105
106
# File 'lib/api/types.rb', line 104

def forward_from
  @forward_from
end

#forward_from_chatObject (readonly)

Returns the value of attribute forward_from_chat.



104
105
106
# File 'lib/api/types.rb', line 104

def forward_from_chat
  @forward_from_chat
end

#fromObject (readonly)

Returns the value of attribute from.



104
105
106
# File 'lib/api/types.rb', line 104

def from
  @from
end

#message_idObject (readonly)

Returns the value of attribute message_id.



104
105
106
# File 'lib/api/types.rb', line 104

def message_id
  @message_id
end

#reply_markupObject (readonly)

Returns the value of attribute reply_markup.



104
105
106
# File 'lib/api/types.rb', line 104

def reply_markup
  @reply_markup
end

#textObject (readonly)

Returns the value of attribute text.



104
105
106
# File 'lib/api/types.rb', line 104

def text
  @text
end

#via_botObject (readonly)

Returns the value of attribute via_bot.



104
105
106
# File 'lib/api/types.rb', line 104

def via_bot
  @via_bot
end

Instance Method Details

#command?Boolean

FIXED: Proper command detection

Returns:

  • (Boolean)


127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/api/types.rb', line 127

def command?
  return false unless text
  return false unless entities
  
  # Find a "bot_command" entity
  command_entity = entities.find { |e| e.type == 'bot_command' }
  return false unless command_entity
  
  # Extract the command text
  command_text = text[command_entity.offset, command_entity.length]
  command_text&.start_with?('/')
end

#command_argsObject



152
153
154
155
156
157
158
159
160
161
# File 'lib/api/types.rb', line 152

def command_args
  return nil unless command?
  
  command_entity = entities.find { |e| e.type == 'bot_command' }
  return nil unless command_entity
  
  # Text after the command entity
  args_start = command_entity.offset + command_entity.length
  text[args_start..-1]&.strip
end

#command_nameObject



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/api/types.rb', line 140

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]
  cmd = cmd[1..-1]  # Remove "/"
  cmd = cmd.split('@').first  # Remove bot username
  cmd
end