Skip to content

Inline Lambda Responders

Jammerware edited this page Aug 6, 2016 · 4 revisions

The syntax to define these responders is slightly wordier than that of SFMEZSSRs, but they afford the programmer a more open sandbox in which to play.

If you've always dreamed of a world in which your bot occasionally hits on Slackbot when he speaks, this one goes out to you.

var myResponder = SimpleResponder.Create
(
    (ResponseContext context) => { 
        return (context.Message.User.IsSlackbot && new Random().Next(8) <= 1); 
    },
    (ResponseContext context) => { 
        return "Slackbot, your place in server memory looks even more handsome than usual today!" 
    }
));
myBot.Responders.Add(myResponder);

The first parameter, canRespond, should be a lambda expression that returns a simple boolean indicating whether your responder wants to respond to the incoming message. The second parameter, getResponse, should be another lambda that returns a string containing the response.

As you can see, inline lambda responders expose the flexible world of VB or C# to you, allowing you to drop in the lambda of your choice and go to town.

Down, girl!

Notes about Inline Lambda Responders

  • the argument context of type ResponseContext is passed to both the canRespond and getResponse parameterized methods of the processor created using this syntax. 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.

Clone this wiki locally