Last active
August 19, 2018 14:38
-
-
Save ksasao/24cd6c00be012f5d617a0f0000d44e4a to your computer and use it in GitHub Desktop.
Receive synapseWear data and post the data to Slack using Azure Functions (HTTP Trigger/C#)
This file contains hidden or 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
#r "Newtonsoft.Json" | |
using System; | |
using System.Text; | |
using System.Net; | |
using Newtonsoft.Json; | |
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log) | |
{ | |
log.Info("C# HTTP trigger function processed a request."); | |
dynamic data = await req.Content.ReadAsStringAsync(); | |
data = data ?? "{\"message\":\"No Data\"}"; | |
var str = WebUtility.UrlDecode(data); | |
log.Info(str); | |
// Post to Slack | |
using(var wc = new WebClient()) | |
{ | |
var slackWebHookUrl = "https://hooks.slack.com/services/[YOUR Slack incoming webhook URL]"; | |
var json = JsonConvert.SerializeObject(new | |
{ | |
text = str, | |
icon_emoji = ":thermometer:", | |
username = "synapseWear", | |
link_names = "1" | |
}); | |
wc.Headers.Add(HttpRequestHeader.ContentType, "application/json;charset=UTF-8"); | |
wc.Encoding=Encoding.UTF8; | |
wc.UploadString(slackWebHookUrl, json); | |
} | |
return req.CreateResponse(HttpStatusCode.OK, "Posted to Slack."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment