CurlBotCommand.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using Microsoft.AspNetCore.Http;
  2. using System.Text.RegularExpressions;
  3. using SlackAPI;
  4. using System.Text.Json;
  5. using System;
  6. using sera_slackbot.Library;
  7. using System.Web;
  8. namespace sera_slackbot.Commands
  9. {
  10. public class CurlBotCommand : IBotCommand
  11. {
  12. private JsonFileSaver saver;
  13. private RestApiRequester requester;
  14. private string channelId;
  15. private string userId;
  16. private string text;
  17. private string message;
  18. public CurlBotCommand(JsonFileReader reader, JsonFileSaver saver)
  19. {
  20. this.saver = saver;
  21. requester = new RestApiRequester();
  22. }
  23. public void ExtractValidData(JsonElement slackEvent)
  24. {
  25. this.text = slackEvent.GetProperty("text").ToString();
  26. this.channelId = slackEvent.GetProperty("channel").ToString();
  27. this.userId = slackEvent.GetProperty("user").ToString();
  28. }
  29. public bool WillTrigger(JsonElement slackEvent)
  30. {
  31. var text = slackEvent.GetProperty("text").ToString();
  32. return text.Contains("curl", StringComparison.InvariantCultureIgnoreCase);
  33. }
  34. public dynamic Run(SlackClient client)
  35. {
  36. var curlUrl = this.GetValidUrl(text);
  37. var awaiter = requester.GetJson(curlUrl).GetAwaiter();
  38. awaiter.OnCompleted(() =>
  39. {
  40. try
  41. {
  42. string jsonResult = JsonSerializer.Serialize(awaiter.GetResult(), new JsonSerializerOptions { WriteIndented = true });
  43. jsonResult = jsonResult.Substring(0, Math.Min(jsonResult.Length, 255));
  44. client.PostMessage(response => { }, channelId, $"I made the request and I got ```{jsonResult}```");
  45. }
  46. catch (Exception)
  47. {
  48. string resultString = awaiter.GetResult();
  49. resultString = HttpUtility.HtmlEncode(resultString);
  50. resultString = resultString.Substring(0, Math.Min(resultString.Length, 255));
  51. client.PostMessage(response => { }, channelId, $"I made the request and I got ```{resultString}```");
  52. }
  53. });
  54. client.PostMessage(response => { }, channelId, $"Ok, I'll see if I can get you '{curlUrl}'. Just a moment.");
  55. return new
  56. {
  57. code = StatusCodes.Status200OK,
  58. body = ""
  59. };
  60. }
  61. private string GetValidUrl(string text)
  62. {
  63. var urlMatch = new Regex("\"(.*?)\"").Match(text);
  64. var curlUrl = string.Empty;
  65. if (urlMatch.Success)
  66. {
  67. curlUrl = urlMatch.Groups[1].Value;
  68. }
  69. if (curlUrl.StartsWith("<"))
  70. {
  71. var slackFormattedMatch = new Regex("<(.*?)\\|.*?>").Match(curlUrl);
  72. if (slackFormattedMatch.Success)
  73. {
  74. curlUrl = slackFormattedMatch.Groups[1].Value;
  75. }
  76. else
  77. {
  78. slackFormattedMatch = new Regex("<(.*?)>").Match(curlUrl);
  79. if (slackFormattedMatch.Success)
  80. {
  81. curlUrl = slackFormattedMatch.Groups[1].Value;
  82. }
  83. }
  84. }
  85. else if (!curlUrl.StartsWith("http"))
  86. {
  87. curlUrl = "http://" + curlUrl;
  88. }
  89. return curlUrl;
  90. }
  91. }
  92. }