Class: Telegram::Event
- Inherits:
-
Object
- Object
- Telegram::Event
- Defined in:
- lib/telegram/events.rb
Instance Attribute Summary collapse
- #action ⇒ ActionType readonly
- #event ⇒ EventType readonly
- #id ⇒ Number readonly
- #message ⇒ Message readonly
- #tgmessage ⇒ TelegramMessage readonly
- #time ⇒ Time readonly
Instance Method Summary collapse
- #format_message ⇒ Object
- #format_service ⇒ Object
-
#initialize(client, event = EventType::UNKNOWN_EVENT, action = ActionType::NO_ACTION, data = {}) ⇒ Event
constructor
A new instance of Event.
- #to_s ⇒ Object
Constructor Details
#initialize(client, event = EventType::UNKNOWN_EVENT, action = ActionType::NO_ACTION, data = {}) ⇒ Event
Returns a new instance of Event.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/telegram/events.rb', line 39 def initialize(client, event = EventType::UNKNOWN_EVENT, action = ActionType::NO_ACTION, data = {}) @client = client = nil = 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 = TelegramMessage.new(@client, self) when EventType::ONLINE_STATUS foramt_status end end |
Instance Attribute Details
#action ⇒ ActionType (readonly)
28 29 30 |
# File 'lib/telegram/events.rb', line 28 def action @action end |
#event ⇒ EventType (readonly)
25 26 27 |
# File 'lib/telegram/events.rb', line 25 def event @event end |
#id ⇒ Number (readonly)
22 23 24 |
# File 'lib/telegram/events.rb', line 22 def id @id end |
#time ⇒ Time (readonly)
31 32 33 |
# File 'lib/telegram/events.rb', line 31 def time @time end |
Instance Method Details
#format_message ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/telegram/events.rb', line 67 def = Message.new .text = @raw_data['text'] .type = @raw_data.has_key?('media') ? @raw_data['media']['type'] : 'text' .raw_from = @raw_data['from']['id'] .from_type = @raw_data['from']['type'] .raw_to = @raw_data['to']['id'] .to_type = @raw_data['to']['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 = if .from.nil? user = @raw_data['from'] user = TelegramContact.pick_or_new(@client, user) @client.contacts << user unless @client.contacts.include?(user) .from = user end if .to.nil? type = @raw_data['to']['type'] if type == 'chat' chat = @raw_data['to'] chat = TelegramChat.pick_or_new(@client, chat) @client.chats << chat unless @client.chats.include?(chat) .from = chat elsif type == 'user' user = @raw_data['to'] user = TelegramContact.pick_or_new(@client, user) @client.contacts << user unless @client.contacts.include?(user) .to = user elsif type == 'encr_chat' chat = @raw_data['to'] chat = TelegramChat.pick_or_new(@client, chat) @client.chats << chat unless @client.chats.include?(chat) .to = chat end end end |
#format_service ⇒ Object
63 64 65 |
# File 'lib/telegram/events.rb', line 63 def format_service end |
#to_s ⇒ Object
113 114 115 |
# File 'lib/telegram/events.rb', line 113 def to_s "<Event Type=#{@event} Action=#{@action} Time=#{@time} Message=#{@message}>" end |