14

I'm trying to debug an existing Hubot script and in the future write my own, and I need an easy way to debug it or at least print values somewhere (but not to the channel). How can I do so?

Bonus points if this can be done with just using Node in some interactive local mode. I'm really not sure where to start.

All scripts Hubot uses are written in Coffeescript.

P.S. I'm using Hubot with Hipchat.

Artem Russakovskii
  • 21,516
  • 18
  • 92
  • 115

4 Answers4

11

I don't know if this helps but I found a way to inspect objects.

Util = require "util"

module.exports = (robot) ->
  robot.hear /hi robot/i, (msg) ->
    user = robot.brain.usersForFuzzyName(msg.message.user.name)
    msg.send "#{Util.inspect(user)}"

This allowed be to see all the elements of the object so I could figure out what I was doing wrong...

radixhound
  • 2,190
  • 2
  • 18
  • 23
  • @radixhound so does `msg.send "foo"` print to the console after you've kicked off Hubot via something like `bin/hubot`? What's the step just before this to initialize your Hubot? Thanks! – mecampbellsoup Sep 02 '15 at 17:56
  • Yes, I kick off Hubot via `bin/hubot` and I put the output inside of a respond. Basically start up hubot from the command line and say `hi robot` and it outputs the info. Probably better to do something instead like `show me the users`. – radixhound Feb 22 '18 at 19:39
8

I have discovered the answer myself: console.log MSG in a .coffee Coffeescript source does exactly what I needed.

Artem Russakovskii
  • 21,516
  • 18
  • 92
  • 115
  • 7
    You can also run the REPL by leaving out the `-a ` option which will let you test and debug scripts locally. – Tom Bell Jun 12 '12 at 19:06
  • @Artem what command are you running locally to get hubot fired up and everything? I'm also looking to debug a script here :) – mecampbellsoup Sep 02 '15 at 17:53
3

You can use

robot.logger.info "your log message here"

That will log it just like the other hubot messages get logged.

0

Found this (coffeescript) snippet somewhere which logs all errors, quite helpful to add to bots in development.

robot.error (err, res) -> robot.logger.error "#{err}\n#{err.stack}" if res? res.reply "#{err}\n#{err.stack}"

teeks
  • 1