-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWhosOff.cs
51 lines (48 loc) · 1.92 KB
/
WhosOff.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.IO;
using Microsoft.Azure.WebJobs.ServiceBus;
using Microsoft.Azure.ServiceBus;
using System.Text;
namespace PTO
{
public static class WhosOff
{
[FunctionName("WhosOff")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log,
[ServiceBus("whosoff", Connection = "SERVICEBUS_CONNECTION_STRING", EntityType = EntityType.Queue)] ICollector<Message> queueCollector)
{
try
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var command = Command.Parse(requestBody);
var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(requestBody));
var message = new Message(bytes) { MessageId = $"{command.TeamId}-{command.UserId}"};
queueCollector.Add(message);
return new OkObjectResult(string.Empty); //TODO: Send "is typing message"
}
catch (System.Exception ex)
{
log.LogError($"Error: {ex.StackTrace}");
return new OkObjectResult(@"Oopsie! Something went wrong. Please try `/whoisoff` command again in a little bit.");
}
}
private static void LogHeaders(HttpRequest req, ILogger log)
{
string h = null;
foreach (var header in req.Headers)
{
string headerContent = string.Join(",", header.Value.ToArray());
h += $"{header.Key}: {headerContent}{Environment.NewLine}";
}
log.LogInformation(h);
}
}
}