Created
January 11, 2020 11:31
-
-
Save justinyoo/f5208618f8a06d8b15e048094b8bafcb to your computer and use it in GitHub Desktop.
This file contains 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
private async Task ProcessAsync(string webhookUri, string summary, string title = null, string text = null, string themeColor = null, string sections = null, string actions = null) | |
{ | |
var card = new MessageCard() | |
{ | |
Title = title, | |
Summary = summary, | |
Text = text, | |
ThemeColor = themeColor, | |
Sections = ParseCollection<Section>(sections), | |
Actions = ParseCollection<BaseAction>(actions) | |
}; | |
var converted = JsonConvert.SerializeObject(card, settings); | |
var message = (string)null; | |
var requestUri = webhookUri; | |
using (var client = new HttpClient()) | |
using (var content = new StringContent(converted, Encoding.UTF8, "application/json")) | |
using (var response = await client.PostAsync(requestUri, content).ConfigureAwait(false)) | |
{ | |
response.EnsureSuccessStatusCode(); | |
} | |
} | |
private List<T> ParseCollection<T>(string value) | |
{ | |
var parsed = string.IsNullOrWhiteSpace(value) | |
? null | |
: JsonConvert.DeserializeObject<List<T>>(value, settings); | |
return parsed; | |
} |
This file contains 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
public static void Main(string[] args) | |
{ | |
var webhookUri = args[0]; | |
var summary = args[1]; | |
var title = args[2]; | |
var text = args[3]; | |
var themeColor = args[4]; | |
var sections = args[5]; | |
var actions = args[6]; | |
ProcessAsync( | |
webhookUri, | |
summary, | |
title, | |
text, | |
themeColor, | |
sections, | |
actions).Result; | |
} |
This file contains 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
[FunctionName("SendMessageHttpTrigger")] | |
public static async Task<IActionResult> Run( | |
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "teams/send")] HttpRequest req, | |
ILogger log) | |
{ | |
log.LogInformation("C# HTTP trigger function processed a request."); | |
var body = await new StreamReader(req.Body).ReadToEndAsync().ConfigureAwait(false); | |
dynamic payload = JsonConvert.DeserializeObject<object>(body); | |
await ProcessAsync( | |
payload.webhookUri, | |
payload.summary, | |
payload.title, | |
payload.text, | |
payload.themeColor, | |
payload.sections, | |
payload.actions).ConfigureAwait(false); | |
return new OkResult(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment