InvalidBotCommand.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.Text.Json;
  2. using Microsoft.AspNetCore.Http;
  3. using SlackAPI;
  4. using sera_slackbot.Library;
  5. namespace sera_slackbot.Commands
  6. {
  7. public class InvalidBotCommand : IBotCommand, IBotInteraction
  8. {
  9. private string channelId;
  10. private string userId;
  11. private string message;
  12. private string missingCommand;
  13. public InvalidBotCommand(string missingCommand)
  14. {
  15. this.missingCommand = missingCommand;
  16. }
  17. public void ExtractValidData(JsonElement slackEvent)
  18. {
  19. this.channelId = slackEvent.GetProperty("channel").ToString();
  20. this.userId = slackEvent.GetProperty("user").ToString();
  21. }
  22. public bool WillTrigger(JsonElement slackEvent)
  23. {
  24. return true;
  25. }
  26. public dynamic Run(SlackClient client)
  27. {
  28. message = $"Whoops! I'm sorry, \u003C@{userId}\u003E, but something is wrong in my command list.";
  29. client.PostMessage(response => { }, channelId, message);
  30. message = $"I can't seem to find '{missingCommand}'.";
  31. client.PostMessage(response => { }, channelId, message);
  32. return new
  33. {
  34. code = StatusCodes.Status200OK,
  35. body = ""
  36. };
  37. }
  38. public dynamic Interaction(SlackClient client, JsonElement payload, string actionValue)
  39. {
  40. var channelId = payload.GetProperty("container").GetProperty("channel_id").ToString();
  41. var userId = payload.GetProperty("user").GetProperty("id").ToString();
  42. client.PostMessage(response =>
  43. {
  44. client.PostMessage(response => { }, channelId, $"I'm so sorry, \u003C@{userId}\u003E, but something is wrong with this interaction.");
  45. }, channelId, $"Oh no! I can't seem to find '{missingCommand}'!");
  46. return new
  47. {
  48. code = StatusCodes.Status405MethodNotAllowed,
  49. body = ""
  50. };
  51. }
  52. }
  53. }