Skip to content

Instantly share code, notes, and snippets.

@Layoric
Last active May 25, 2016 14:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Layoric/de5c874ca52af1f0fd5d to your computer and use it in GitHub Desktop.
Save Layoric/de5c874ca52af1f0fd5d to your computer and use it in GitHub Desktop.
A very simple webhook integration with Slack and ServiceStack response filters
//Example use AppHost
public class AppHost : AppHostBase
{
public AppHost()
: base("SlackPluginExample", typeof(MyServices).Assembly) { }
public override void Configure(Container container)
{
this.Plugins.Add(new SlackNotifierFeature
{
IncommingWebHookUrl =
"{Your Incoming WebHooks URL from slack, eg https://hooks.slack.com/services/....}",
MessageFactories = new Dictionary<Type, Func<object, string>>
{
{
typeof(HelloResponse),
responseObject => {
var res = (HelloResponse) responseObject;
return "Service said {0}".Fmt(res.Result);
}
}
}
});
}
}
//Plugin itself
public class SlackNotifierFeature : IPlugin
{
public string IncommingWebHookUrl { get; set; }
public Dictionary<Type, Func<object, string>> MessageFactories =
new Dictionary<Type, Func<object, string>>();
public void Register(IAppHost appHost)
{
appHost.GlobalResponseFilters.Add((request, response, responseObject) =>
{
if (!MessageFactories.ContainsKey(responseObject.GetType())) return;
var messageToSlack = MessageFactories[responseObject.GetType()].Invoke(responseObject);
IncommingWebHookUrl.PostJsonToUrl(new {Text = messageToSlack});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment