SlackEventController.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Text.Json;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.Extensions.Logging;
  6. using Microsoft.Extensions.Configuration;
  7. using SlackAPI;
  8. using sera_slackbot.Commands;
  9. using sera_slackbot.Library;
  10. namespace sera_slackbot.Controllers
  11. {
  12. [ApiController]
  13. [Route("event")]
  14. [Produces("application/json")]
  15. public class SlackEventController : ControllerBase
  16. {
  17. private readonly ILogger<SlackEventController> logger;
  18. private readonly IConfiguration configuration;
  19. private SlackClient client;
  20. private BotCommandFactory factory;
  21. private JsonFileReader jsonFileReader;
  22. private JsonFileSaver jsonFileSaver;
  23. public SlackEventController(IConfiguration config, ILogger<SlackEventController> log)
  24. {
  25. configuration = config;
  26. logger = log;
  27. var token = configuration.GetSection("Slackbot").GetValue<string>("Bot-User-OAuth-Access-Token");
  28. this.jsonFileReader = new JsonFileReader(configuration);
  29. this.jsonFileSaver = new JsonFileSaver(configuration);
  30. client = new SlackClient(token);
  31. factory = new BotCommandFactory(this.jsonFileReader, this.jsonFileSaver);
  32. }
  33. [HttpGet]
  34. public IActionResult Get()
  35. {
  36. // Console.WriteLine("***Event***");
  37. // foreach (var header in Request.Headers)
  38. // {
  39. // Console.WriteLine(header.Key + ": " + header.Value);
  40. // }
  41. dynamic response = new
  42. {
  43. name = "Slack Event Bot",
  44. code = StatusCodes.Status200OK,
  45. datetime = DateTime.Now
  46. };
  47. return Ok(response);
  48. }
  49. [HttpPost]
  50. public IActionResult HttpPost([FromBody] dynamic body)
  51. {
  52. // Console.WriteLine("***Event***");
  53. // foreach (var header in Request.Headers)
  54. // {
  55. // Console.WriteLine(header.Key + ": " + header.Value);
  56. // }
  57. // Console.WriteLine(JsonSerializer.Serialize(body, new JsonSerializerOptions { WriteIndented = true }));
  58. var typeProperty = body.GetProperty("type");
  59. var type = typeProperty.ToString();
  60. dynamic response = new { code = StatusCodes.Status200OK, body = "" };
  61. switch (type)
  62. {
  63. case "url_verification":
  64. response = UrlVerification(body);
  65. break;
  66. case "event_callback":
  67. response = EventCallback(body);
  68. break;
  69. default:
  70. var bodyString = ((object)body).ToString();
  71. logger.LogError($"Unknown Request Type: '{type}' = {bodyString}");
  72. break;
  73. }
  74. return StatusCode(response.code, response);
  75. }
  76. private dynamic UrlVerification(dynamic body)
  77. {
  78. var challenge = body.GetProperty("challenge");
  79. var token = body.GetProperty("token");
  80. //TODO: should save off the token?
  81. logger.LogInformation($"Url Verification: Got Token: {token}");
  82. return new
  83. {
  84. code = StatusCodes.Status200OK,
  85. challenge = challenge,
  86. body = ""
  87. };
  88. }
  89. private dynamic EventCallback(dynamic body)
  90. {
  91. this.jsonFileSaver.Save("lastevent.json", body);
  92. var slackEvent = body.GetProperty("event");
  93. dynamic response = new
  94. {
  95. code = StatusCodes.Status200OK,
  96. body = ""
  97. };
  98. var eventType = slackEvent.GetProperty("type").ToString();
  99. var eventString = JsonSerializer.Serialize(slackEvent, new JsonSerializerOptions { WriteIndented = true });
  100. switch (eventType)
  101. {
  102. case "app_mention":
  103. response = AppMention(slackEvent);
  104. break;
  105. case "message":
  106. // logger.LogInformation($"Got Message: '{eventType}' = {eventString}");
  107. break;
  108. case "reaction_added":
  109. // logger.LogInformation($"Got Reaction Added: '{eventType}' = {eventString}");
  110. break;
  111. default:
  112. logger.LogError($"Unknown Event Type: '{eventType}' = {eventString}");
  113. break;
  114. }
  115. return response;
  116. }
  117. private dynamic AppMention(JsonElement slackEvent)
  118. {
  119. var command = ParseText(slackEvent);
  120. return command.Run(client);
  121. }
  122. private IBotCommand ParseText(JsonElement slackEvent)
  123. {
  124. JsonElement commandList = this.jsonFileReader.Read("commandlist.json");
  125. IBotCommand command;
  126. foreach (var commandString in commandList.EnumerateArray())
  127. {
  128. command = this.factory.CreateCommand(commandString.ToString());
  129. if (command.WillTrigger(slackEvent))
  130. {
  131. command.ExtractValidData(slackEvent);
  132. return command;
  133. }
  134. }
  135. command = new UnhelpfulBotCommand();
  136. command.ExtractValidData(slackEvent);
  137. return command;
  138. }
  139. }
  140. }