Last active
May 25, 2016 14:04
-
-
Save Layoric/de5c874ca52af1f0fd5d to your computer and use it in GitHub Desktop.
A very simple webhook integration with Slack and ServiceStack response filters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); | |
} | |
} | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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