Class: Lita::Handlers::Responder

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/responder.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_config(config) ⇒ Object



4
5
6
# File 'lib/lita/handlers/responder.rb', line 4

def self.default_config(config)
  config.cleverbot = false
end

Instance Method Details

#add_responder(response) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/lita/handlers/responder.rb', line 23

def add_responder(response)
  question = response.matches[0][0]
  answer = response.matches[0][1]

  update_answer question, answer

  response.reply_with_mention "I have added '#{answer}' to '#{question}'"
end

#ask_responder(response) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/lita/handlers/responder.rb', line 54

def ask_responder(response)
  question = response.matches[0][0]
  answer = get_answer(question)
  if !answer && config.cleverbot
    bots = robot.instance_variable_get('@cleverbots') || {}
    bot = bots[response.user.mention_name] ||= Cleverbot::Client.new
    answer = bot.write question
    robot.instance_variable_set('@cleverbots', bots)
  end
  response.reply_with_mention answer if answer
end

#list_responder(response) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/lita/handlers/responder.rb', line 41

def list_responder(response)
  if !questions.empty?
    response.reply_with_mention '- ' + questions.map{|q| q.sub('lita-responder:', '') }.join("\n- ")
  else
    response.reply_with_mention 'I don\'t have any key-value stored'
  end
end

#remove_responder(response) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/lita/handlers/responder.rb', line 32

def remove_responder(response)
  question = response.matches[0][1]

  if redis.exists("lita-responder:#{question.downcase}")
    redis.del("lita-responder:#{question.downcase}")
  end
  response.reply_with_mention "I have removed '#{question}'"
end

#reset_responder(response) ⇒ Object



49
50
51
52
# File 'lib/lita/handlers/responder.rb', line 49

def reset_responder(response)
  count = questions.empty? ? 0 : redis.del(questions)
  response.reply_with_mention "#{count} key(s) removed"
end