Class: Telegram::Event
- Inherits:
-
Object
- Object
- Telegram::Event
- Defined in:
- lib/telegram/events.rb
Overview
Event object, will be created in the process part of Client
Instance Attribute Summary collapse
-
#action ⇒ ActionType
readonly
Action type, created from given data.
-
#event ⇒ EventType
readonly
Event type, created from given data.
-
#id ⇒ Number
readonly
Event identifier.
-
#message ⇒ Message
readonly
Message object, created from given data.
-
#tgmessage ⇒ TelegramMessage
readonly
Telegram message object, created from Message.
-
#time ⇒ Time
readonly
Time event received.
Instance Method Summary collapse
-
#format_message ⇒ void
private
Process raw data in which event type is message given.
-
#format_service ⇒ void
private
Process raw data in which event type is service given.
-
#initialize(client, event = EventType::UNKNOWN_EVENT, action = ActionType::NO_ACTION, data = {}) ⇒ Event
constructor
Create a new Event instance.
-
#to_s ⇒ String
Convert Event instance to the string format.
Constructor Details
#initialize(client, event = EventType::UNKNOWN_EVENT, action = ActionType::NO_ACTION, data = {}) ⇒ Event
Create a new Telegram::Event instance
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 @tgmessage = TelegramMessage.new(@client, self) when EventType::ONLINE_STATUS foramt_status end end |
Instance Attribute Details
#action ⇒ ActionType (readonly)
Returns Action type, created from given data.
106 107 108 |
# File 'lib/telegram/events.rb', line 106 def action @action end |
#event ⇒ EventType (readonly)
Returns Event type, created from given data.
103 104 105 |
# File 'lib/telegram/events.rb', line 103 def event @event end |
#id ⇒ Number (readonly)
Returns Event identifier.
100 101 102 |
# File 'lib/telegram/events.rb', line 100 def id @id end |
#message ⇒ Message (readonly)
Returns Message object, created from given data.
112 113 114 |
# File 'lib/telegram/events.rb', line 112 def @message end |
#tgmessage ⇒ TelegramMessage (readonly)
Returns Telegram message object, created from Message.
115 116 117 |
# File 'lib/telegram/events.rb', line 115 def @tgmessage end |
#time ⇒ Time (readonly)
Returns Time event received.
109 110 111 |
# File 'lib/telegram/events.rb', line 109 def time @time end |
Instance Method Details
#format_message ⇒ void
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.
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 = Message.new .id = @id .text = @raw_data['text'] ||= '' media = @raw_data['media'] .type = media ? media['type'] : 'text' .raw_from = @raw_data['from']['peer_id'] .from_type = @raw_data['from']['peer_type'] .raw_to = @raw_data['to']['peer_id'] .to_type = @raw_data['to']['peer_type'] from = @client.contacts.find { |c| c.id == .raw_from } to = @client.contacts.find { |c| c.id == .raw_to } to = @client.chats.find { |c| c.id == .raw_to } if to.nil? .from = from .to = to @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_service ⇒ void
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.
153 154 155 |
# File 'lib/telegram/events.rb', line 153 def format_service end |
#to_s ⇒ String
Convert Telegram::Event instance to the string format
211 212 213 |
# File 'lib/telegram/events.rb', line 211 def to_s "<Event Type=#{@event} Action=#{@action} Time=#{@time} Message=#{@tgmessage}>" end |