Class: Telegram::Event

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

Overview

Event object, will be created in the process part of Client

See Also:

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, event = EventType::UNKNOWN_EVENT, action = ActionType::NO_ACTION, data = {}) ⇒ Event

Create a new Telegram::Event instance

Parameters:

  • client (Client)

    Root client instance

  • event (EventType) (defaults to: EventType::UNKNOWN_EVENT)

    Event type

  • action (ActionType) (defaults to: ActionType::NO_ACTION)

    Action type

  • data (Hash) (defaults to: {})

    Raw data

Since:

  • 0.1.0


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/telegram/events.rb', line 124

def initialize(client, event = EventType::UNKNOWN_EVENT, action = ActionType::NO_ACTION, data = {})
  @client = client
  @id = data.respond_to?(:[]) ? data['id'] : ''
  @message = nil
  @tgmessage = nil
  @raw_data = data
  @time = nil

  @event = event
  @action = action

  @time = Time.at(data['date'].to_i) if data.has_key?('date')
  @time = DateTime.strptime(data['when'], "%Y-%m-%d %H:%M:%S") if @time.nil? and data.has_key?('when')

  case event
  when EventType::SERVICE
    foramt_service
  when EventType::RECEIVE_MESSAGE, EventType::SEND_MESSAGE
    format_message
    @tgmessage = TelegramMessage.new(@client, self)
  when EventType::ONLINE_STATUS
    foramt_status
  end
end

Instance Attribute Details

#actionActionType (readonly)

Returns Action type, created from given data.

Returns:

  • (ActionType)

    Action type, created from given data

Since:

  • 0.1.0


106
107
108
# File 'lib/telegram/events.rb', line 106

def action
  @action
end

#eventEventType (readonly)

Returns Event type, created from given data.

Returns:

  • (EventType)

    Event type, created from given data

Since:

  • 0.1.0


103
104
105
# File 'lib/telegram/events.rb', line 103

def event
  @event
end

#idNumber (readonly)

Returns Event identifier.

Returns:

  • (Number)

    Event identifier

Since:

  • 0.1.0


100
101
102
# File 'lib/telegram/events.rb', line 100

def id
  @id
end

#messageMessage (readonly)

Returns Message object, created from given data.

Returns:

  • (Message)

    Message object, created from given data

Since:

  • 0.1.0


112
113
114
# File 'lib/telegram/events.rb', line 112

def message
  @message
end

#tgmessageTelegramMessage (readonly)

Returns Telegram message object, created from Message.

Returns:

Since:

  • 0.1.0


115
116
117
# File 'lib/telegram/events.rb', line 115

def tgmessage
  @tgmessage
end

#timeTime (readonly)

Returns Time event received.

Returns:

  • (Time)

    Time event received

Since:

  • 0.1.0


109
110
111
# File 'lib/telegram/events.rb', line 109

def time
  @time
end

Instance Method Details

#format_messagevoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Process raw data in which event type is message given.

Since:

  • 0.1.0


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/telegram/events.rb', line 161

def format_message
  message = Message.new

  message.id = @id
  message.text = @raw_data['text'] ||= ''
  media = @raw_data['media']
  message.type = media ? media['type'] : 'text'
  message.raw_from = @raw_data['from']['peer_id']
  message.from_type = @raw_data['from']['peer_type']
  message.raw_to = @raw_data['to']['peer_id']
  message.to_type = @raw_data['to']['peer_type']

  from = @client.contacts.find { |c| c.id == message.raw_from }
  to = @client.contacts.find { |c| c.id == message.raw_to }
  to = @client.chats.find { |c| c.id == message.raw_to } if to.nil?

  message.from = from
  message.to = to

  @message = message

  if @message.from.nil?
    user = @raw_data['from']
    user = TelegramContact.pick_or_new(@client, user)
    @client.contacts << user unless @client.contacts.include?(user)
    @message.from = user
  end

  if @message.to.nil?
    type = @raw_data['to']['peer_type']
    case type
    when 'chat', 'encr_chat'
      chat = TelegramChat.pick_or_new(@client, @raw_data['to'])
      @client.chats << chat unless @client.chats.include?(chat)
      if type == 'encr_chat' then
        @message.to = chat
      else
        @message.from = chat
      end
    when 'user'
      user = TelegramContact.pick_or_new(@client, @raw_data['to'])
      @client.contacts << user unless @client.contacts.include?(user)
      @message.to = user
    end
  end
end

#format_servicevoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Process raw data in which event type is service given.

Since:

  • 0.1.0


153
154
155
# File 'lib/telegram/events.rb', line 153

def format_service

end

#to_sString

Convert Telegram::Event instance to the string format

Returns:

Since:

  • 0.1.0


211
212
213
# File 'lib/telegram/events.rb', line 211

def to_s
  "<Event Type=#{@event} Action=#{@action} Time=#{@time} Message=#{@tgmessage}>"
end