Skip to content
Jonathan Starnes edited this page Sep 21, 2013 · 8 revisions

A message handler will allow you to handle message that are produced by a message producer. In order for you to make use of your message handler you will have to create a public method that has one of several different kinds of message handler attributes. The parameters of the method are injected as they are needed. You are allowed as many handler methods in a message handler. Each method will be a different route from the producer to the handler. If both of your handlers are matched they will both be called.

Message Handler Attributes

Hear Attribute

This attribute will create a route where the content of the message matches a regular expression that is passed into the attribute.

The following will match whenever hello is in the content of the message

[Hear("hello")]

Respond Attribute

This attribute will create a route where the content of a message matches a regular expression that is directed at the robot itself.

[Respond("hello")]

This attribute will match all of the messages:

  • nbot hello
  • @nbot hello
  • nbot: hello

Message Type Attribute

This attribute will create a route that will match the type of the message that is received. This will only match for message types that your producer produces. For example if you listen for a "EnterMessage" and the producer does not produce a message of type "EnterMessage" the route will not match.

[MessageType("EnterMessage")]

Using Named Parameters

When you need to have a parameter in your regex you can go about this in one of two ways. If you have a parameter or set of parameters that can be defined by simple (.) regex you can use handle bars to define the name. In the following example the string "My name is {{name}}" is translated into "My name is (?<name>.)".

[Respond("My name is {{name}}"] // <-- put the name of the parameter in handlebars 
public void HandleMyName(Message message, IMessageClient client, string name) //<-- The value is injected by name and is converted to whatever type is specified if possible.
{
   ...
}

The second way to use Named Parameters is to do the conversion manually in your regex. This is useful when you have a more complicated situation. In the following situation we have to be more specific about the format of our parameters therefore we need to specify the regex for the named parameters.

[Respond("Say (?<phrase>\\w+) (?<times>\\d+) times")]
public void HandleSay(Message message, IMessageClient client,string phrase, int times)
{
   ...
}

Message Handler Signature

  • The method name does not matter
  • Requires an attribute
  • The parameters will be injected and are not required.

[Hear("hello {{name}}")]
public void HandleHello(Message message, IMessageClient client, IBrain brain, string name)
{
    ...
}

Parameters

Clone this wiki locally