hermes

Messenger of the gods.

hermes

Hermes is a friendly, pluggable chat bot. He's super easy to customize, so you can write your own plugins to teach him new tricks, and automate away all of the things!

$ npm install hermes

The simplest way to use Hermes is to use the CLI paired with a hermes.json configuration file. Here's an example hermes.json file:

{
  "name": "Dr. Watson",
  "nickname": "watson",
  "plugins": {
    "hermes-math": {},
    "hermes-url-shortener": {},
    "hermes-hipchat": {
      "jid": "YOUR_HIPCHAT_JID",
      "password": "YOUR_HIPCHAT_PASSWORD",
      "apiKey": "YOUR_HIPCHAT_API_KEY"
    }
  }
}

That would be setup to create a HipChat robot named "Dr. Watson", that would know how to do basic math and shorten URLs. Then, to run your robot, just do:

$ node_modules/.bin/hermes

You'll see him connect to your HipChat room. Then you can ask him to do things:

> You: @watson shorten https://github.com/segmentio/hermes
> Dr. Watson: @you Sure thing: http://git.io/hJVhhQ

> You: @watson 52/3
> Dr. Watson: @you That would be: 17.333333333

Check out the Hermes site for a list of all the open-source plugins you can add to your robot.

Hermes also comes with a Javascript API, for complicated use cases and full control over how Hermes is configured.

Create a new Robot instance with the given name, defaulting to 'Hermes', and with its nickname defaulting to 'hermes'.

Get or set the robot's name string. The name is the full, proper english way to format the robot's name, for example 'Picasso' not 'picasso'.

Get or set the robot's nickname string. The nickname is the chat-friendly way to format the robot's, for services that distinguish the two. For example, in HipChat this would correlate to the user's "mention name", so a robot named 'Hermes Hubeau' might have a nickname of 'hermes', so that it can be mentioned with @hermes. By default, the nickname will just mirror the name.

Get or set the template for how the robot is mentioned. By default it is '@%s ', which converts via util.format to '@hermes '. (The robot's nickname is used for conversion.)

Return a mention string for the given nickname, defaulting to the robot's own nickname. This is so that plugins don't have to format the nicknames themselves. For example, robot.mention('ian') might return '@ian '.

Send the robot a message to process, with optional context. It'll decide which of its plugin's handlers to invoke. context is useful for situations where the message comes from a certain user or room, so that it can reply properly, for example:

robot.hear('@watson Hi Watson', {
  user: 'ian',
  room: 'hq'
});

The robot is an event emitter, so by default robot.on('event', callback) will act exactly as expected. It fires the following events:

  • name - with the new name string
  • nickname - with the new nickname string
  • template - with the new template string
  • hear - with match, context whenever it hears an incoming message
  • mention - with match, context whenever it's mentioned in an incoming message

But it's also got augmented functionality, so that you can filter which incoming messages you want to listen to. You can filter by either a regexp, a string or a context. This is how plugins are made. For example, here's what the URL shortening plugin is listening for:

robot.on('mention', /^shorten (\S+)$/i, callback);

Or if you just want to match a basic string:

robot.on('mention', 'surprise me', callback);

Or if you want to only match messages from a certain user:

robot.on('mention', { user: 'USER_ID' }, callback);

These filters can be combined to add pretty complex logic to your robot, for example you can wait for an incoming reply from the user you're currently conversing with, so that you don't get noise from other users in the middle:

robot.on('mention', 'shorten', function(matchcontext){
  robot.say('What URL do you want to shorten?', context);
  robot.once('mention', { user: context.user }, function(match){
    var url = match[0];
    var res = shorten(url);
    robot.say(res, context);
  });
});

The same as robot.on() but the callback will only ever be called once, and then it will be removed.

Remove an event handler from the robot.

Say a message with optional context.

Emote a message with optional context.

Reply to a user by id with a message and optional context.

Set the topic of a room by id.

Send an error message with optional context. The error message can also be a Javascript Error instance.

Send a warning message with optional context.

Send a success message with optional context.

Get or set a user by id with attrs, defaulting to a simple in-memory store.

Get all of the stored users.

Get or set a room by id with attrs, defaulting to a simple in-memory store.

Get all of the stored rooms.

Get or set a custom piece of data by key with value, defaulting to a simple in-memory store.

Register a help trigger with a description, so that when the user says @robot help we can print out a useful list of commands, like:

@robot shorten <url>
Returns a shortened goo.gl for the given <url>.

@robot math <string>
Calculate the value of a <string> of math.

The trigger should just be shorten <url> and the help command will automatically add in the mention specific to the robot's configuration, eg. @robot.

Debugging ease was one of the core concerns when building Hermes. Since you need to use stdin in the shell to actually interact with the robot, the typical node debug workflow doesn't work well.

Instead, using node-inspector you can easily call:

$ node-inspector &
$ hermes --debug-brk

Like you're used to, and everything will just work.

A huge thank you to Egor Kislitsyn for letting us use the hermes name on npm!

The MIT License (MIT)

Copyright © 2014 Segment <friends@segment.io>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.