require "json" require "net/http" module Boty class Session include Boty::Logger attr_reader :bot def start(&block) EM.run do login @bot = initialize_bot(&block) stablish_connection end self end private def on_connect(ws) ws.on :message do |event| begin on_message event, @bot rescue StandardError => e log_error e, "Message #{event} could not be processed." end end end def login logger.debug do "Logging in against slack right now" end @slack_info = Slack.rtm.start logger.debug do "yep! logged in!" end @session_url = @slack_info["url"] end def initialize_bot(&block) Bot.new(@slack_info["self"]).tap { |bot| DSL.new(bot).instance_eval(&block) if block_given? logger.debug do "Bot is configured and ready to go!" end } end def on_message(event, bot) logger.debug do "Message arrived. Creating bot event" end bot.event JSON.parse(event.data) end def on_close logger.debug { "bye bye" } # TODO: try to reconnect (stablish_connection) unless is an em interrupt end def stablish_connection logger.debug do "Starting to listen on #{@session_url}" end ws = Faye::WebSocket::Client.new @session_url ws.on :close do on_close end on_connect ws end end end