Skip to content

Standalone Responders

Jammerware edited this page Aug 6, 2016 · 3 revisions

This method of configuring your bot is by far the most verbose, but if you're old school, looking to make a slightly more sophisticated breed of bot, or if classes were good enough for your dad and they're good enough for you, read on to learn the power of standalone responders.

public class PatchNotesResponder : IResponder
{
    public bool CanRespond(ResponseContext context)
    {
        return context.Message.BotWasMentioned && Regex.IsMatch(context.Message.Text, @"\b(what's new)\b", RegexOptions.IgnoreCase);
    }

    public BotMessage GetResponse(ResponseContext context)
    {
        var version = Assembly.GetExecutingAssembly().GetName().Version;
        var patchNotes = File.ReadAllLines("my-patch-notes.txt");

        var builder = new StringBuilder();
        builder.AppendLine(@"I'm Margiebot v." + version.Major.ToString() + "." + version.Minor.ToString() + "." + version.Build.ToString() + "!");
        builder.Append("Here's what all's been goin' on with me lately.");

        builder.AppendLine("```");
        foreach (var line in patchNotes) {
            builder.AppendLine(line);
        }
        builder.AppendLine("```");

        return new BotMessage() { Text = builder.ToString() };
    }
}

myBot.Responders.Add(new PatchNotesResponder());

All it takes to build your own fully customizable responder is a class that implements the interface IResponder which you'll find in MargieBot's Nuget package. Implement the two required methods CanRespond and GetResponse, and you're off to the races.

Don't you love it when your software tells your users what's been going on for you?

Now, if you're saying to yourself, "Sure, that's fine, but I could do that with an inline lambda responder, I have one thing to say to you:

Get out.

Just kidding. You're right. But if you want to see an example of a responder that couldn't have been realized any other way, check out the WeatherRequestResponder in the example application in the repository. The bottom line is that if a C# or VB class can do it, your responder can too if you create it this way. Go nuts!

Notes about Standalone Responders

  • the argument context of type ResponseContext is passed to both the CanRespond and GetResponse methods of your processor. It contains helpful state information about your bot and the details of the message triggering the evaluation. You can find more details on ResponseContext on the Tips & Tricks page.
  • If you've worked with either of the other two types of responders before trying a Standalone, you might be a little confused to see that the GetResponse method doesn't return a string, but instead returns an object of type BotMessage. This allows you to return slightly more complex responses, including things like file attachments, or to respond in a different Slack channel than the original message came from. These features are powerful, but remember what Uncle Ben said: with great power comes great responsibility.