Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
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;
}
[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