Created
November 17, 2016 20:17
-
-
Save DavidDeSloovere/31f188235149f08d9a657b73643aef29 to your computer and use it in GitHub Desktop.
AzureFunction-OctopusToMicrosoftTeams
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
// full repo at https://github.com/DavidDeSloovere/AzureFunction-OctopusToMicrosoftTeams | |
public static async Task<object> Run(HttpRequestMessage req, TraceWriter log) | |
{ | |
log.Info("Webhook was triggered!"); | |
string jsonContent = await req.Content.ReadAsStringAsync(); | |
log.Info(jsonContent); | |
dynamic data = JsonConvert.DeserializeObject(jsonContent); | |
if (data.Payload?.Event?.Message == null) { | |
return req.CreateResponse(HttpStatusCode.BadRequest, new { | |
error = "Payload.Event.Message missing from body." | |
}); | |
} | |
var message = data.Payload.Event.Message; | |
var occurred = data.Payload.Event.Occurred; | |
log.Info("* " + message); | |
log.Info("* " + occurred); | |
// make call to Teams WebHook Url, which is stored in the app settings | |
// https://outlook.office365.com/webhook/cb67..4b/IncomingWebhook/93... | |
var appKey = "TeamsWebHookUrl"; | |
var webHookUrl = ConfigurationManager.AppSettings[appKey]; | |
log.Info($"App Setting. {appKey}: {webHookUrl}"); | |
// Payload content at https://dev.outlook.com/Connectors/GetStarted | |
var body = new { text = $"{message} {occurred}" }; | |
using (var client = new HttpClient()) | |
{ | |
await client.PostAsJsonAsync(webHookUrl, body); | |
log.Info("Sent the JSON payload to Teams WebHook!"); | |
} | |
return req.CreateResponse(HttpStatusCode.OK); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment