Old Site

This site is archived. I now update and maintain my blog here.

Post to Slack when Umbraco content is published

Slack is very of the moment isn’t it? Thanks to damianwalsh we were early adopters at Moriyama and use it as our main internal communication tool.

One of the joys of Slack is just how easy it is to add your own integration. Here is ours that shows you how to post to a slack channel when an Umbraco document is published.

Note: You’ll need the wonderful RestSharp library from nuget.

using RestSharp;
void ContentServicePublished(IPublishingStrategy sender, PublishEventArgs e)
  foreach (var entity in e.PublishedEntities)
    var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    var url = umbracoHelper.Url(entity.Id, UrlProviderMode.Relative);
    var urls = string.Format("https://slacker.moriyama.co.uk{0} or http://slacker.moriyama.int{0}", url);
    var t = "New publish : " + entity.Name + " " + urls;
    var hash = "[email protected]".HashEmailForGravatar();
    var client = new RestClient("https://slack.com/api");
    var request = new RestRequest("/chat.postMessage", Method.POST);
    request.AddParameter("token", "");
    request.AddParameter("channel", "#mychannel");
    request.AddParameter("username", "Moriyama Bot");
    request.AddParameter("text", t);
    request.AddParameter("icon_url", string.Format("http://www.gravatar.com/avatar/{0}", hash));
    var response = client.Execute(request);

The code is also available as a github gist.

I’d be interested to hear if anyone out there is doing any other interesting slack integrations, particularly Umbraco related.


Leave a comment