Method: ChatBot#handle

Defined in:
lib/chatx/hooks.rb

#handle(data, server:) ⇒ Object

Note:

This method is strictly internal.

The immediate exit point when a message is recieved from a websocket. It grabs the relevant hooks, creates the event, and passes the event to the hooks.

It also spawns a new thread for every hook. This could lead to errors later, but it prevents 409 errors which shut the bot up for a while.

Parameters:

  • data (Hash)

    It’s the JSON passed by the websocket



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/chatx/hooks.rb', line 11

def handle(data, server:)
  data.each do |room, evt|
    next if evt.keys.first != 'e'
    evt['e'].each do |e|
      event_type = e['event_type'].to_i - 1
      room_id = room[1..-1].to_i
      event = ChatX::Event.new e, server, self
      @ws_json_logger.info "#{event.type_long}: #{event.hash}"
      @ws_json_logger.info "Currently in rooms #{@rooms.keys} / #{current_rooms}"
      next if @rooms[room_id].nil?
      @rooms[room_id.to_i][:events].push(event)
      @hooks[server] ||= {}
      (Array(@hooks[server][event_type.to_i])+Array(@hooks[server]['*'])+Array(@hooks['*'][event_type.to_i])).each do |rm_id, hook|
        Thread.new do
          @hook.current_room = room_id
          hook.call(event, room_id) if rm_id == room_id || rm_id == '*'
        end
      end
    end
  end
end