Skip to content

Instantly share code, notes, and snippets.

@ChrisLundquist
Created November 10, 2012 11:04
Show Gist options
  • Save ChrisLundquist/4050748 to your computer and use it in GitHub Desktop.
Save ChrisLundquist/4050748 to your computer and use it in GitHub Desktop.
elasticsearch plugin for Hubot
# Description:
# Log all the things to ElasticSearch then lets you ask hubot what you missed
#
# Dependencies:
# None
#
# Configuration:
# ELASTICSEARCH_HOSTNAME - E.G. elasticsearch.example.com:9200, where to send the put requests
# ELASTICSEARCH_USERNAME - OPTIONAL basic auth username
# ELASTICSEARCH_PASSWORD - OPTIONAL basic auth password
#
# Commands:
# hubot what did I miss
# hubot what did (user) say
# hubot tell me about (search term)
# hubot when did you last see (user)
#
# Author:
# clundquist
elasticsearch_hostname = process.env.ELASTICSEARCH_HOSTNAME || 'localhost:9200'
elasticsearch_username = process.env.ELASTICSEARCH_USERNAME
elasticsearch_password = process.env.ELASTICSEARCH_PASSWORD
elasticsearch_url = "http://" + elasticsearch_hostname + "/hubot/"
print_result = (err,res,body,msg) ->
if(err)
msg.send err
msg.send res
msg.send body
else
data = JSON.parse body
logs = data.hits.hits.sort (a,b) -> if a._source.date > b._source.date then 1 else -1
msg.send "#{new Date(log._source.date)} #{log._source.user}: #{log._source.message}" for log in logs
search_hubot = (msg, data) ->
data = JSON.stringify(data)
msg.http(elasticsearch_url + "_search/" )
.auth(elasticsearch_username, elasticsearch_password)
.header('Content-Length', data.length) #XXX To work around NGINX 411 errors
.post(data) (err, res, body) -> # Because we don't support GET request with a body
print_result(err, res, body, msg)
module.exports = (robot) ->
robot.brain.on 'loaded', =>
robot.brain.data.last_exit ||= {}
robot.brain.data.last_enter ||= {}
robot.leave (msg) ->
robot.brain.data.last_exit[msg.message.user.name] = Date.now()
robot.enter (msg) ->
robot.brain.data.last_enter[msg.message.user.name] = Date.now()
# Log everything in this chat room to elastic_search
robot.hear /./i, (msg) ->
data = { user: msg.message.user.name, message: msg.message.text, room: msg.message.user.room, date: Date.now()}
data = JSON.stringify(data)
msg.http(elasticsearch_url + msg.message.user.name + "/" + Date.now())
.header('Content-Length', data.length) # #XXX To work around NGINX 411 errors
.auth(elasticsearch_username, elasticsearch_password)
.put(data) (err, res, body) ->
if(err)
msg.send err
msg.send res
msg.send body
robot.respond /when did you last see (.+)/i, (msg) ->
msg.send "I saw #{msg.match[1]} at #{new Date(robot.brain.data.last_exit[msg.match[1]])}"
robot.respond /what did (.+) say/i, (msg) ->
msg.send "in the last hour #{msg.match[1]} said:"
data = { query: { bool: { must: { match: { user: msg.match[1] }, range: { date: {from: (Date.now() - 3600) }}}}, size:50}}
search_hubot(msg,data)
robot.respond /tell me about (.+)/i, (msg) ->
msg.send "searching chat logs for #{msg.match[1]}:"
data = { query: { match: { _all: msg.match[1]}}, size:50}
search_hubot(msg,data)
robot.respond /what did I miss/i, (msg) ->
msg.send "Oh I'll tell you what happened"
data = { query: { range: { date: { from: robot.brain.data.last_exit[msg.message.user.name] } }}, size:3600}
search_hubot(msg,data)
@ChrisLundquist
Copy link
Author

In the words of Samuel L Jackson:

Hubot> hubot tell me about nigga winston
Hubot> searching chat logs for nigga winston:
Hubot> Tue Nov 13 2012 17:50:52 GMT-0800 (PST) Shell: hubot tell me about nigga winston
Hubot> Tue Nov 13 2012 17:50:55 GMT-0800 (PST) Shell: hubot tell me about nigga winston
Hubot> Tue Nov 13 2012 17:54:34 GMT-0800 (PST) Shell: My money's in that office, right? If she start giving me some bullshit about it ain't there, and we got to go someplace else and get it, I'm gonna shoot you in the head then and there. Then I'm gonna shoot that bitch in the kneecaps, find out where my goddamn money is. She gonna tell me too. Hey, look at me when I'm talking to you, motherfucker. You listen: we go in there, and that nigga Winston or anybody else is in there, you the first motherfucker to get shot. You understand?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment