This repository has been archived by the owner. It is now read-only.
Fetching contributors…
Cannot retrieve contributors at this time
92 lines (80 sloc) 3.22 KB
# Description:
# Respond to custom answers
# Dependencies:
# Configuration:
# None
# Commands:
# hubot say something about <topic> - will say something he knows about the subject
# hubot when asked <regexp_of_question> answer <response> - teach your bot to answer to <regexp_of_question> with <response>
# hubot forget answers - remove every teached answer from bot brain
# Author:
module.exports = (robot) ->
basic_knowledge = {
1: {regexp: "(what( is|'s))?( your)? favorite( programming)? language", answer: 'CoffeeScript'},
2: {regexp: 'favorite (os|operating system|platform)', answer: 'Linux'}
respondToAnswer = (item) ->
robot.respond new RegExp(item.regexp, 'i'), (msg) ->
for key, item of
break if msg.match[0].replace(,'').match new RegExp(item.regexp, 'i')
msg.send item.answer if item?.answer?
knowledgeAbout = (subject) ->
for key, item of
if subject.replace(,'').match new RegExp(item.regexp, 'i')
found = true ; break
if found == true
@.key = key ; @.item = item
return @
return null
robot.brain.on 'loaded', => "Loading knowledge" ?= {} = basic_knowledge if Object.keys( == 0
for key, item of
robot.respond /(when )?asked (.*) (reply|answer|return|say) (.*)$/i, (msg) ->
question = msg.match[2]
answer = msg.match[4]
result = new knowledgeAbout(question)
if result.key?
if result.item.answer == answer
msg.send "I already know that"
msg.send "I thought \"#{result.item.answer}\" but I will now answer \"#{answer}\""[result.key].answer = answer
new_question = {regexp: question, answer: answer}
next_id = Object.keys([next_id] = new_question
msg.send "OK, I will answer \"#{answer}\" when asked \"#{question}\""
robot.respond /(forget)( all)? (answers|replies|everything)$/i, (msg) ->
for key, item of
i = 0
while i < robot.listeners.length
robot.listeners.splice(i,1) if String(item.regexp) in String(robot.listeners[i].regex)
i++ = {}
msg.send "OK, I've forgot all answers"
robot.respond /((say )?s(ome)?thing|talk( to me)?)( about (.*))?$/i, (msg) ->
subject = msg.match[6]
knowledge =
if JSON.stringify(knowledge) is '{}'
msg.send "I don't know anything, teach me something please ..."
if subject is undefined
answer = knowledge[msg.random(Object.keys(knowledge))].answer
msg.send "I would say #{answer}"
result = new knowledgeAbout(subject)
if result.key?
msg.send "If you ask #{result.item.regexp}, I would answer #{result.item.answer}"
msg.send "I don't know anything about #{subject}, please teach me something about it"