Sha256: 59a43feb3c8e6fcde67be94f7cb91f3695d6a92fb41f1cec9097899b2bafa348

Contents?: true

Size: 1.96 KB

Versions: 5

Compression:

Stored size: 1.96 KB

Contents

class SlackSmartBot
  def listen_simulate
    @salutations = [config[:nick], "<@#{config[:nick_id]}>", "bot", "smart"]
    @pings = []
    get_bots_created()
      @buffer_complete = [] unless defined?(@buffer_complete)
      b = File.read("#{config.path}/buffer_complete.log")
      result = b.scan(/^\|(\w+)\|(\w+)\|([^~]+)~~~/m)
      result.delete(nil)
      new_messages = result[@buffer_complete.size..-1]
      unless new_messages.nil? or new_messages.empty?
        @buffer_complete = result
        new_messages.each do |message|
          channel = message[0].strip
          user = message[1].strip
          command = message[2].to_s.strip
          # take in consideration that on simulation we are treating all messages even those that are not populated on real cases like when the message is not populated to the specific bot connection when message is sent with the bot
          @logger.info "treat message: #{message}" if config.testing
          treat_message({channel: channel, user: user, text: command})
        end
      end
  end

  def listen
    @pings = []
    get_bots_created()

    client.on :message do |data|
      unless data.user == "USLACKBOT"
        treat_message(data)
      end
    end

    restarts = 0
    started = false
    while restarts < 200 and !started
      begin
        @logger.info "Bot starting: #{config.inspect}"
        client.start!
      rescue Slack::RealTime::Client::ClientAlreadyStartedError
        @logger.info "ClientAlreadyStarted so we continue with execution"
        started = true
      rescue Exception => e
        started = false
        restarts += 1
        if restarts < 200
          @logger.info "*" * 50
          @logger.fatal "Rescued on starting: #{e.inspect}"
          @logger.info "Waiting 60 seconds to retry. restarts: #{restarts}"
          puts "#{Time.now}: Not able to start client. Waiting 60 seconds to retry: #{config.inspect}"
          sleep 60
        else
          exit!
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
slack-smart-bot-1.5.0 lib/slack/smart-bot/listen.rb
slack-smart-bot-1.4.3 lib/slack/smart-bot/listen.rb
slack-smart-bot-1.4.2 lib/slack/smart-bot/listen.rb
slack-smart-bot-1.4.1 lib/slack/smart-bot/listen.rb
slack-smart-bot-1.4.0 lib/slack/smart-bot/listen.rb