Skip to content

Tips & Tricks for MargieBot Pros

Jammerware edited this page May 4, 2015 · 13 revisions

ResponseContext

An instance of ResponseContext is passed to both the CanRespond and GetResponse methods of each response processor, 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 response processor has provided a response to the message. Since processors 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.

See the example processors in the MargieBot.UI application for examples on how to leverage ResponseContext to bring your response processor 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 response processors (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 processor 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