-
Notifications
You must be signed in to change notification settings - Fork 42
Tips & Tricks for MargieBot Pros
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 booleanBotWasMentioned
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()
andSet()
methods - Some responders benefit from being able to share data between them (for an example, see theScoreResponder
andScoreboardRequestResponder
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 theResponseContext
property of theBot
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
:
As will this one:
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.
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!