MessageController.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using MafiaTelegramBot.Commands;
  5. using MafiaTelegramBot.Commands.CallbackQueries;
  6. using MafiaTelegramBot.Commands.ChannelPost;
  7. using MafiaTelegramBot.Commands.ChatMember;
  8. using MafiaTelegramBot.Commands.Messages;
  9. using MafiaTelegramBot.DataBase.Entity;
  10. using Telegram.Bot;
  11. using Telegram.Bot.Exceptions;
  12. using Telegram.Bot.Types;
  13. using Telegram.Bot.Types.Enums;
  14. using Telegram.Bot.Types.InputFiles;
  15. using Telegram.Bot.Types.ReplyMarkups;
  16. using static MafiaTelegramBot.Logs;
  17. #pragma warning disable 1998
  18. namespace MafiaTelegramBot.Controllers
  19. {
  20. public static class MessageController
  21. {
  22. public static async Task HandleUpdateAsync(ITelegramBotClient bot, Update update, CancellationToken ct)
  23. {
  24. User ChatToUser(Chat chat)
  25. {
  26. return new User
  27. {
  28. FirstName = chat.Title ?? chat.FirstName,
  29. LastName = chat.Title == null ? chat.LastName : "",
  30. Id = chat.Id,
  31. IsBot = chat.Type != ChatType.Private,
  32. LanguageCode = "ru",
  33. Username = chat.Username
  34. };
  35. }
  36. try
  37. {
  38. var handler = update.Type switch
  39. {
  40. UpdateType.Message =>
  41. await UpdateHandler.Factory(update.Message.From, update.Message.Text, new MessageHandler(null)),
  42. UpdateType.CallbackQuery =>
  43. await UpdateHandler.Factory(update.CallbackQuery.From, update.CallbackQuery.Data, new CallbackQueryHandler(null)),
  44. UpdateType.ChannelPost =>
  45. await UpdateHandler.Factory(ChatToUser(update.ChannelPost.Chat), update.ChannelPost.Text, new ChannelPost(null)),
  46. UpdateType.MyChatMember => await MyChatMemberHandler.Factory(ChatToUser(update.MyChatMember.Chat)),
  47. _ => await UnknownUpdateHandlerAsync(update)
  48. };
  49. var unused = await handler.Execute(update);
  50. }
  51. catch (Exception exception)
  52. {
  53. await HandleErrorAsync(bot, exception, ct);
  54. }
  55. }
  56. private static async Task<UpdateHandler> UnknownUpdateHandlerAsync(Update update)
  57. {
  58. LogOut(update.Type.ToString());
  59. return new UpdateHandler();
  60. }
  61. public static Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception,
  62. CancellationToken cancellationToken)
  63. {
  64. switch (exception)
  65. {
  66. case ApiRequestException apiRequestException:
  67. LogOutWarning($"API Error:[{apiRequestException.ErrorCode}] - {apiRequestException.Message}");
  68. break;
  69. default:
  70. LogOutError(exception.ToString());
  71. break;
  72. }
  73. return Task.CompletedTask;
  74. }
  75. public static async Task<Message> SendText(UserEntity info, string message, IReplyMarkup keyboard = null)
  76. {
  77. try
  78. {
  79. if (!info.IsBlocked)
  80. return await Bot.Client.SendTextMessageAsync(info.ChatId, message, replyMarkup: keyboard, disableNotification: true);
  81. }
  82. catch (Exception e)
  83. {
  84. LogOutWarning("Can't send text message " + e);
  85. }
  86. return new Message();
  87. }
  88. public static async Task<Message> SendTextWithHtml(UserEntity info, string message, IReplyMarkup keyboard = null)
  89. {
  90. try
  91. {
  92. if (!info.IsBlocked)
  93. return await Bot.Client.SendTextMessageAsync(info.ChatId, message, ParseMode.Html, replyMarkup: keyboard, disableNotification: true);
  94. }
  95. catch (Exception e)
  96. {
  97. LogOutWarning("Can't send text message with html " + e);
  98. }
  99. return new Message();
  100. }
  101. public static async Task<Message> SendSticker(UserEntity info, string fileId)
  102. {
  103. try
  104. {
  105. if (!info.IsBlocked)
  106. return await Bot.Client.SendStickerAsync(info.ChatId, fileId, true);
  107. }
  108. catch (Exception e)
  109. {
  110. LogOutWarning("Can't send sticker " + e);
  111. }
  112. return new Message();
  113. }
  114. public static async Task<Message> EditMessage(UserEntity info, int messageId, string message, InlineKeyboardMarkup keyboard = null)
  115. {
  116. try
  117. {
  118. if (!info.IsBlocked)
  119. return await Bot.Client.EditMessageTextAsync(info.ChatId, messageId, message, replyMarkup: keyboard);
  120. }
  121. catch (Exception e)
  122. {
  123. LogOutWarning("Can't edit message text " + e);
  124. }
  125. return new Message();
  126. }
  127. public static async Task<Message> EditReplyMarkup(UserEntity info, int messageId, InlineKeyboardMarkup keyboard)
  128. {
  129. try
  130. {
  131. if (!info.IsBlocked)
  132. return await Bot.Client.EditMessageReplyMarkupAsync(info.ChatId, messageId, keyboard);
  133. }
  134. catch (Exception e)
  135. {
  136. LogOutWarning("Can't edit reply markup " + e);
  137. }
  138. return new Message();
  139. }
  140. public static async Task DeleteMessage(long chatId, int messageId)
  141. {
  142. try
  143. {
  144. await Bot.Client.DeleteMessageAsync(chatId, messageId);
  145. }
  146. catch (Exception e)
  147. {
  148. LogOutWarning("Can't delete message " + e);
  149. }
  150. }
  151. public static async Task<Message> SendImage(UserEntity info, InputOnlineFile inputOnlineFile, string message, InlineKeyboardMarkup replyMarkup)
  152. {
  153. try
  154. {
  155. if (!info.IsBlocked)
  156. return await Bot.Client.SendPhotoAsync(info.ChatId, inputOnlineFile, message, replyMarkup: replyMarkup);
  157. }
  158. catch (Exception e)
  159. {
  160. LogOutWarning("Can't send photo " + e);
  161. }
  162. return new Message();
  163. }
  164. }
  165. }