Bot.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using MafiaTelegramBot.Models.Commands;
  4. using MafiaTelegramBot.Models.Inlines;
  5. using MafiaTelegramBot.Models.Replies;
  6. using MafiaTelegramBot.Resources;
  7. using Telegram.Bot;
  8. using Telegram.Bot.Types;
  9. using Telegram.Bot.Types.Enums;
  10. using Telegram.Bot.Types.ReplyMarkups;
  11. namespace MafiaTelegramBot.Models
  12. {
  13. public static class Bot
  14. {
  15. private static TelegramBotClient _client;
  16. private static List<Command> _commandsList;
  17. private static List<Query> _queriesList;
  18. private static List<Reply> _repliesList;
  19. public static IReadOnlyList<Command> Commands => _commandsList.AsReadOnly();
  20. public static IReadOnlyList<Query> Queries => _queriesList.AsReadOnly();
  21. public static IReadOnlyList<Reply> Replies => _repliesList.AsReadOnly();
  22. public static readonly List<long> UsersThatChangesNickname = new();
  23. public static readonly Dictionary<long, string[]> UsersThatCreatesRoom = new();
  24. public static readonly List<long> UserThatEntersPrivateCode = new();
  25. public static TelegramBotClient Get()
  26. {
  27. if (_client != null) return _client;
  28. InitCommands();
  29. InitQueries();
  30. InitReplies();
  31. _client = new TelegramBotClient(AppSettings.Token);
  32. //var hook = string.Format(AppSettings.Url, "api/message/update");
  33. //await _client.SetWebhookAsync(hook);
  34. return _client;
  35. }
  36. private static void InitCommands()
  37. {
  38. //TODO fill commands array
  39. _commandsList = new List<Command>
  40. {
  41. new StartCommand(),
  42. new CreateGameCommand(),
  43. new ConnectGameCommand(),
  44. new ShowProfileCommand(),
  45. new RoomSettingsCommand(),
  46. new LeaveCommand(),
  47. //it always in end of array, handle all messages, that doesn't contains command string
  48. new CustomMessageHandlingCommand()
  49. };
  50. }
  51. private static void InitQueries()
  52. {
  53. //TODO fill inline keyboard array
  54. _queriesList = new List<Query>
  55. {
  56. new MyRolesQuery(),
  57. new ShopQuery(),
  58. new MakePrivateRoomQuery(),
  59. new MakePublicRoomQuery(),
  60. new MakeStandartGameQuery(),
  61. new MakeExtendedGameQuery(),
  62. new MakePublicRoomQuery(),
  63. new SettingsQuery(),
  64. new ChangeNameQuery(),
  65. new SettingsRoomQuery(),
  66. new ConnectToPrivateRoomQuery(),
  67. new LookUsersListQuery(),
  68. new IncreaseRoomMaxCapacityQuery(),
  69. new DecreaseRoomMaxCapacity(),
  70. new ConnectToPublicRoomQuery(),
  71. new ConnectToSelectedRoomQuery()
  72. };
  73. }
  74. private static void InitReplies()
  75. {
  76. //TODO fill inline keyboard array
  77. _repliesList = new List<Reply>
  78. {
  79. };
  80. }
  81. public static async Task<Message> SendWithMarkdown2(long chatId, string message, IReplyMarkup replyMarkup = null)
  82. {
  83. var newMessage = await Task.Run(()=> message
  84. .Replace(".", "\\.")
  85. .Replace("@", "\\@")
  86. .Replace("#", "\\#")
  87. .Replace("!", "\\!")
  88. .Replace("-", "\\-")
  89. );
  90. return replyMarkup == null
  91. ? await Get().SendTextMessageAsync(chatId, newMessage, ParseMode.MarkdownV2)
  92. : await Get().SendTextMessageAsync(chatId, newMessage, ParseMode.MarkdownV2, replyMarkup: replyMarkup);
  93. }
  94. }
  95. }