MessageController.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using CardCollector.DataBase.Entity;
  6. using CardCollector.Resources;
  7. using Telegram.Bot;
  8. using Telegram.Bot.Exceptions;
  9. using Telegram.Bot.Types;
  10. using Telegram.Bot.Types.Enums;
  11. using Telegram.Bot.Types.InlineQueryResults;
  12. using Telegram.Bot.Types.InputFiles;
  13. using Telegram.Bot.Types.ReplyMarkups;
  14. using Message = CardCollector.Commands.Message.Message;
  15. using CallBackQuery = CardCollector.Commands.CallbackQuery.CallbackQuery;
  16. using MyChatMember = CardCollector.Commands.MyChatMember.MyChatMember;
  17. using InlineQuery = CardCollector.Commands.InlineQuery.InlineQuery;
  18. using ChosenInlineResult = CardCollector.Commands.ChosenInlineResult.ChosenInlineResult;
  19. using TgMessage = Telegram.Bot.Types.Message;
  20. namespace CardCollector.Controllers
  21. {
  22. using static Logs;
  23. public static class MessageController
  24. {
  25. private static readonly Dictionary<long, List<int>> DeletingMessagePool = new();
  26. public static async Task HandleUpdateAsync(ITelegramBotClient client, Update update, CancellationToken ct)
  27. {
  28. try
  29. {
  30. var executor = update.Type switch
  31. {
  32. UpdateType.Message => await Message.Factory(update),
  33. UpdateType.CallbackQuery => await CallBackQuery.Factory(update),
  34. UpdateType.MyChatMember => await MyChatMember.Factory(update),
  35. UpdateType.InlineQuery => await InlineQuery.Factory(update),
  36. UpdateType.ChosenInlineResult => await ChosenInlineResult.Factory(update),
  37. _ => throw new ArgumentOutOfRangeException()
  38. };
  39. await executor.Execute();
  40. }
  41. catch (Exception e)
  42. {
  43. switch (e)
  44. {
  45. case ArgumentOutOfRangeException:
  46. LogOut(update.Type);
  47. break;
  48. case ApiRequestException:
  49. LogOutWarning(e.Message);
  50. break;
  51. default:
  52. LogOutError(e);
  53. break;
  54. }
  55. }
  56. }
  57. public static Task HandleErrorAsync(ITelegramBotClient client, Exception e, CancellationToken ct)
  58. {
  59. switch (e)
  60. {
  61. case ApiRequestException:
  62. LogOutWarning(e.Message);
  63. break;
  64. default:
  65. LogOutError(e);
  66. break;
  67. }
  68. return Task.CompletedTask;
  69. }
  70. public static void AddNewMessageToPool(UserEntity user, int messageId)
  71. {
  72. try
  73. {
  74. DeletingMessagePool[user.ChatId].Add(messageId);
  75. }
  76. catch (Exception)
  77. {
  78. DeletingMessagePool.Add(user.ChatId, new List<int>());
  79. DeletingMessagePool[user.ChatId].Add(messageId);
  80. }
  81. }
  82. public static async Task DeleteMessagesFromPool(UserEntity user)
  83. {
  84. try
  85. {
  86. foreach (var id in DeletingMessagePool[user.ChatId])
  87. await DeleteMessage(user, id);
  88. DeletingMessagePool[user.ChatId].Clear();
  89. DeletingMessagePool.Remove(user.ChatId);
  90. }
  91. catch (Exception)
  92. {
  93. /* ignore */
  94. }
  95. }
  96. public static async Task<TgMessage> SendMessage(UserEntity user, string message, IReplyMarkup keyboard = null)
  97. {
  98. try
  99. {
  100. if (!user.IsBlocked)
  101. return await Bot.Client.SendTextMessageAsync(user.ChatId, message, replyMarkup: keyboard, disableNotification: true);
  102. }
  103. catch (Exception e)
  104. {
  105. LogOutWarning("Can't send text message " + e);
  106. }
  107. return new TgMessage();
  108. }
  109. public static async Task<TgMessage> SendTextWithHtml(UserEntity info, string message, IReplyMarkup keyboard = null)
  110. {
  111. try
  112. {
  113. if (!info.IsBlocked)
  114. return await Bot.Client.SendTextMessageAsync(info.ChatId, message, ParseMode.Html, replyMarkup: keyboard, disableNotification: true);
  115. }
  116. catch (Exception e)
  117. {
  118. LogOutWarning("Can't send text message with html " + e);
  119. }
  120. return new TgMessage();
  121. }
  122. public static async Task<TgMessage> SendSticker(UserEntity info, string fileId)
  123. {
  124. try
  125. {
  126. if (!info.IsBlocked)
  127. return await Bot.Client.SendStickerAsync(info.ChatId, fileId, true);
  128. }
  129. catch (Exception e)
  130. {
  131. LogOutWarning("Can't send sticker " + e);
  132. }
  133. return new TgMessage();
  134. }
  135. public static async Task<TgMessage> EditMessage(UserEntity info, int messageId, string message, InlineKeyboardMarkup keyboard = null)
  136. {
  137. try
  138. {
  139. if (!info.IsBlocked)
  140. return await Bot.Client.EditMessageTextAsync(info.ChatId, messageId, message, replyMarkup: keyboard);
  141. }
  142. catch (Exception e)
  143. {
  144. LogOutWarning("Can't edit message text " + e);
  145. }
  146. return new TgMessage();
  147. }
  148. public static async Task<TgMessage> EditReplyMarkup(UserEntity info, int messageId, InlineKeyboardMarkup keyboard)
  149. {
  150. try
  151. {
  152. if (!info.IsBlocked)
  153. return await Bot.Client.EditMessageReplyMarkupAsync(info.ChatId, messageId, keyboard);
  154. }
  155. catch (Exception e)
  156. {
  157. LogOutWarning("Can't edit reply markup " + e);
  158. }
  159. return new TgMessage();
  160. }
  161. public static async Task DeleteMessage(UserEntity user, int messageId)
  162. {
  163. try
  164. {
  165. if (!user.IsBlocked)
  166. await Bot.Client.DeleteMessageAsync(user.ChatId, messageId);
  167. }
  168. catch (Exception e)
  169. {
  170. LogOutWarning("Can't delete message " + e);
  171. }
  172. }
  173. public static async Task<TgMessage> SendImage(UserEntity info, InputOnlineFile inputOnlineFile, string message = null, InlineKeyboardMarkup replyMarkup = null)
  174. {
  175. try
  176. {
  177. if (!info.IsBlocked)
  178. return await Bot.Client.SendPhotoAsync(info.ChatId, inputOnlineFile, message, replyMarkup: replyMarkup, disableNotification: true);
  179. }
  180. catch (Exception e)
  181. {
  182. LogOutWarning("Can't send photo " + e);
  183. }
  184. return new TgMessage();
  185. }
  186. public static async Task AnswerInlineQuery(string queryId, IEnumerable<InlineQueryResult> results, string offset = null)
  187. {
  188. await Bot.Client.AnswerInlineQueryAsync(queryId, results, isPersonal: true, nextOffset: offset, cacheTime: Constants.INLINE_RESULTS_CACHE_TIME);
  189. }
  190. }
  191. }