Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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., where to send the put requests
# 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) ->
msg.send err
msg.send res
msg.send body
data = JSON.parse body
logs = data.hits.hits.sort (a,b) -> if > then 1 else -1
msg.send "#{new 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.leave (msg) ->[] =
robot.enter (msg) ->[] =
# Log everything in this chat room to elastic_search
robot.hear /./i, (msg) ->
data = { user:, message: msg.message.text, room:, date:}
data = JSON.stringify(data)
msg.http(elasticsearch_url + + "/" +
.header('Content-Length', data.length) # #XXX To work around NGINX 411 errors
.auth(elasticsearch_username, elasticsearch_password)
.put(data) (err, res, body) ->
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([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: ( - 3600) }}}}, size:50}}
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}
robot.respond /what did I miss/i, (msg) ->
msg.send "Oh I'll tell you what happened"
data = { query: { range: { date: { from:[] } }}, size:3600}

This comment has been minimized.

Copy link
Owner Author

@ChrisLundquist ChrisLundquist commented Nov 14, 2012

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