Hubot: use set command for all input

620 Views Asked by At

I have an instance of hubot running on heroku. I don't need any of the default behaviour (for example help command). Instead I'd like to choose my own commands and just have users input text with commands. In this case I just want to run pb with every user input.

So if a user inputs hello I'd like humbot to run pb hello.

The code I'm trying based on mshish suggestion is in /scripts/example.cofee

module.exports = (robot) ->
  robot.hear /.*/, (msg) ->
    pb msg
3

There are 3 best solutions below

0
On

you can also do this with middleware. I wrote this to log every message anyone ever said and where they said it. but you can get the idea. you can run any code where I am running the @robot.logger.info stuff

module.exports = (robot) ->
robot.listenerMiddleware (context, next, done) ->
if context.listener.regex.source is /(.+)/i.source
  @robot.logger.info("#{context.response.message.user.name} said: \"#{context.response.message.text}\" in Channel: #{context.response.message.room}")
next()
2
On

Sounds like you want to hear "anything". Use Hubot's hear and respond feature (hear not respond will do what you want) and use a regex that matches everything, .*

To turn off default scripts, remove them using npm uninstall --save PACKAGE_NAME from the root of the repo. PACKAGE_NAME should match the script packages you want to remove from package.json. You may also need to manually remove the script from external-scripts.json and hubot-scripts.json.

2
On

So to answer the first part of your question, removing parts you don't need. Just edit your package.json and remove the lines for commands you don't need. Relatively self explanatory, should be safe to remove the following:

  • "hubot-diagnostics": "0.0.1",
  • "hubot-google-images": "^0.1.2",
  • "hubot-help": "^0.1.1",
  • "hubot-maps": "0.0.1",
  • "hubot-pugme": "^0.1.0",
  • "hubot-rules": "^0.1.0",
  • "hubot-scripts": "^2.5.16",
  • "hubot-shipit": "^0.1.1",

Then for the second part, you have it partially right.

  robot.hear /.*/, (msg) ->
    pb msg

should be in this format:

  robot.hear /.*/, (msg) ->
    msg.send "pb #{msg.match[0]}"

msg.send is the command used to actually send messages to the chat room. and msg.match[0] contains whatever the user put as their message.

EDIT: Also, need to remove the entries from "external-scripts.json".