Skip to content

Instantly share code, notes, and snippets.

@ahelland
Created July 12, 2016 07:48
Show Gist options
  • Save ahelland/ef4ae9f39921a2f2bfdbdb427698ebf9 to your computer and use it in GitHub Desktop.
Save ahelland/ef4ae9f39921a2f2bfdbdb427698ebf9 to your computer and use it in GitHub Desktop.
Azure Function for sending a message through an incoming WebHook in Slack
#r "Newtonsoft.Json"
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net.Http.Headers;
using System.Collections.Generic;
public static void Run(string input, TraceWriter log)
{
var statusMsg = "Bender says: Get up meatbag!";
//Push message through Slack
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("User-Agent", "AzureFunctions");
//Copy paste uri from Slack
var uri = "https://hooks.slack.com/services/";
// emoji list - http://www.emoji-cheat-sheet.com/
var msg = new SlackHook {text = statusMsg, icon_emoji = ":beer:"};
StringContent SlackMsg = new StringContent(JsonConvert.SerializeObject(msg));
HttpResponseMessage response = client.PostAsync(uri,SlackMsg).Result;
var responseString = response.Content.ReadAsStringAsync().Result;
log.Info(responseString);
}
}
public class SlackHook
{
public string text {get;set;}
public string icon_emoji {get;set;}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment