SlackInteractionController.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.Library;
  9. namespace sera_slackbot.Controllers
  10. {
  11. [ApiController]
  12. [Route("interaction")]
  13. [Produces("application/json")]
  14. public class SlackInteractionController : ControllerBase
  15. {
  16. private readonly ILogger<SlackEventController> logger;
  17. private readonly IConfiguration configuration;
  18. private SlackClient client;
  19. private BotCommandFactory factory;
  20. private JsonFileReader jsonFileReader;
  21. private JsonFileSaver jsonFileSaver;
  22. public SlackInteractionController(IConfiguration config, ILogger<SlackEventController> log)
  23. {
  24. configuration = config;
  25. logger = log;
  26. var token = configuration.GetSection("Slackbot").GetValue<string>("Bot-User-OAuth-Access-Token");
  27. this.jsonFileReader = new JsonFileReader(configuration);
  28. this.jsonFileSaver = new JsonFileSaver(configuration);
  29. client = new SlackClient(token);
  30. factory = new BotCommandFactory(this.jsonFileReader, this.jsonFileSaver);
  31. }
  32. [HttpGet]
  33. public IActionResult Get()
  34. {
  35. // Console.WriteLine("***Interaction***");
  36. // foreach (var header in Request.Headers)
  37. // {
  38. // Console.WriteLine(header.Key + ": " + header.Value);
  39. // }
  40. dynamic response = new
  41. {
  42. name = "Slack Interaction Bot",
  43. code = StatusCodes.Status200OK,
  44. datetime = DateTime.Now
  45. };
  46. return Ok(response);
  47. }
  48. [HttpPost]
  49. public IActionResult HttpPost([FromForm] string payload)
  50. {
  51. var payloadObject = JsonSerializer.Deserialize<dynamic>(payload);
  52. dynamic response = new { code = StatusCodes.Status200OK };
  53. var typeProperty = payloadObject.GetProperty("type");
  54. var type = typeProperty.ToString();
  55. switch (type)
  56. {
  57. case "block_actions":
  58. response = BlockActions(payloadObject);
  59. break;
  60. default:
  61. logger.LogError($"Unknown Interaction Type: '{type}' = {payload}");
  62. break;
  63. }
  64. return StatusCode(response.code, response);
  65. }
  66. private dynamic BlockActions(dynamic payload)
  67. {
  68. JsonElement actions = payload.GetProperty("actions");
  69. var valueProperty = "";
  70. foreach (var action in actions.EnumerateArray())
  71. {
  72. valueProperty = action.GetProperty("value").ToString();
  73. }
  74. var actionHandler = valueProperty.Split(":")[0];
  75. var actionValue = valueProperty.Split(":")[1];
  76. var command = this.factory.CreateInteraction(actionHandler);
  77. return command.Interaction(client, payload, actionValue);
  78. }
  79. }
  80. }