Skip to content

Instantly share code, notes, and snippets.

@ahelland
Created November 15, 2016 19:57
Show Gist options
  • Save ahelland/c2a9b843f14ad7892f99c1fa5bfdc0a8 to your computer and use it in GitHub Desktop.
Save ahelland/c2a9b843f14ad7892f99c1fa5bfdc0a8 to your computer and use it in GitHub Desktop.
Azure Function for sending a message through an incoming WebHook in Microsoft Teams
#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 Teams
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("User-Agent", "AzureFunctions");
//Copy paste uri from Teams
var uri = "https://outlook.office365.com/webhook/{guid-01}/IncomingWebhook/{guid-02}";
var targetUrl = new List<string> {"https://contos.io"};
var action = new Action {context = "https://schema.org", type ="ViewAction", name = "Go to Contos.io", target=targetUrl};
var actions = new List<Action> {action};
var msg = new TeamHook {title = "Message from Bender", text = statusMsg, themeColor = "EA4300", potentialAction = actions};
StringContent TeamMsg = new StringContent(JsonConvert.SerializeObject(msg));
log.Info(TeamMsg.ReadAsStringAsync().Result);
HttpResponseMessage response = client.PostAsync(uri,TeamMsg).Result;
var responseString = response.Content.ReadAsStringAsync().Result;
log.Info(responseString);
}
}
public class TeamHook
{
public string title {get;set;}
public string text {get;set;}
public string themeColor {get;set;}
public List<Action> potentialAction {get;set;}
}
public class Action
{
[JsonProperty(PropertyName="@content")]
public string context {get;set;}
[JsonProperty(PropertyName="@type")]
public string type {get;set;}
public string name {get;set;}
public List<string> target {get;set;}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment