Skip to content

Tips & Tricks for MargieBot Pros

Jammerware edited this page Oct 16, 2015 · 13 revisions

ResponseContext

An instance of ResponseContext is passed to both the CanRespond and GetResponse methods of each responder, and each one is chock full of goodies that can help you make smart decisions about when and how your bot responds. Here are some of the highlights:

  • BotHasResponded - a boolean that indicates whether or not a previous responder has provided a response to the message. Since responders are evaluated sequentially in the order in which they're provided to the bot, you can use this property to get up to some nifty shenanigans.
  • Message - a simple object that expresses properties of the message triggering a response. This contains some interesting information like the ID and formatted name of the user who sent the triggering message (so your bot can mention them), which channel, group, or DM the message came from, and the boolean BotWasMentioned indicating whether the message mentions your bot - See the section below on Bot Mentions for details.
  • UserNameCache - a simple dictionary that relates userIDs to the names they chose when creating their slack accounts. This is helpful when you want to refer to someone by their Slack nickname (say, "jammer" or "ben") without specifically mentioning them in the Slack "@" sense.
  • The Get() and Set() methods - Some responders benefit from being able to share data between them (for an example, see the ScoreResponder and ScoreboardRequestResponder examples in the sample application. You can use these methods to add data that will be received by the rest of the responders in order as they're asked to respond. You can also pre-populate the dictionary that these methods interact with using the ResponseContext property of the Bot instance itself.

See the example responders in the MargieBot.UI application for examples on how to leverage ResponseContext to bring your responder to life (or just look at the MargieBot library for specifics on how ResponseContext is instantiated and populated).

##Bot Mentions One thing that you'll probably find as you're writing your responders (at least if you're hoping to write relatively non-obnoxious ones, though we'll understand if you're not) is that you'll often reference the BotWasMentioned property of the Message in ResponseContext when determining whether your responder should respond.

Out of the box, this property will only be true if someone specifically mentions (using the @ symbol) your bot OR if they use your bot's name exactly (though this test is performed case-insensitively).

So if you've named your bot "MargieBot", this message will cause the BotWasMentioned property to be true:

Talking directly to Margie

As will this one:

Informal Margie mention

However, to make your bot feel more responsive and to allow your users to interact with it more naturally, you might find it helpful to "alias" your bot so that the BotWasMentioned property will be set to true when your users enter an informal name for your bot. In the example application, notice that we add a single alias - "Margie". This allows us to respond naturally when a user forgets that our bot's full name is actually MargieBot but is expecting an answer anyway.

Oops, forgot her name

SlackConnector

If you want the core of MargieBot without some of the extra baggage I've added on, a friendly developer has ported it into a Nuget package called SlackConnector. You can check it out on Github here!