Class: Telegram::Event

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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
  @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:



28
29
30
# File 'lib/telegram/events.rb', line 28

def action
  @action
end

#eventEventType (readonly)

Returns:



25
26
27
# File 'lib/telegram/events.rb', line 25

def event
  @event
end

#idNumber (readonly)

Returns:

  • (Number)


22
23
24
# File 'lib/telegram/events.rb', line 22

def id
  @id
end

#messageMessage (readonly)

Returns:



34
35
36
# File 'lib/telegram/events.rb', line 34

def message
  @message
end

#tgmessageTelegramMessage (readonly)

Returns:



37
38
39
# File 'lib/telegram/events.rb', line 37

def tgmessage
  @tgmessage
end

#timeTime (readonly)

Returns:

  • (Time)


31
32
33
# File 'lib/telegram/events.rb', line 31

def time
  @time
end

Instance Method Details

#format_messageObject



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 format_message
  message = Message.new
  message.text = @raw_data['text']
  message.type = @raw_data.has_key?('media') ? @raw_data['media']['type'] : 'text'
  message.raw_from = @raw_data['from']['id']
  message.from_type = @raw_data['from']['type']
  message.raw_to = @raw_data['to']['id']
  message.to_type = @raw_data['to']['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']['type']
    if type == 'chat'
      chat = @raw_data['to']
      chat = TelegramChat.pick_or_new(@client, chat)
      @client.chats << chat unless @client.chats.include?(chat)
      @message.from = chat
    elsif type == 'user'
      user = @raw_data['to']
      user = TelegramContact.pick_or_new(@client, user)
      @client.contacts << user unless @client.contacts.include?(user)
      @message.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)
      @message.to = chat
    end
  end
end

#format_serviceObject



63
64
65
# File 'lib/telegram/events.rb', line 63

def format_service

end

#to_sObject



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

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