123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- using System;
- using System.Threading;
- using System.Threading.Tasks;
- using MafiaTelegramBot.Commands;
- using MafiaTelegramBot.Commands.CallbackQueries;
- using MafiaTelegramBot.Commands.ChannelPost;
- using MafiaTelegramBot.Commands.ChatMember;
- using MafiaTelegramBot.Commands.Messages;
- using MafiaTelegramBot.DataBase.Entity;
- using Telegram.Bot;
- using Telegram.Bot.Exceptions;
- using Telegram.Bot.Types;
- using Telegram.Bot.Types.Enums;
- using Telegram.Bot.Types.InputFiles;
- using Telegram.Bot.Types.ReplyMarkups;
- using static MafiaTelegramBot.Logs;
- #pragma warning disable 1998
- namespace MafiaTelegramBot.Controllers
- {
- public static class MessageController
- {
- public static async Task HandleUpdateAsync(ITelegramBotClient bot, Update update, CancellationToken ct)
- {
- User ChatToUser(Chat chat)
- {
- return new User
- {
- FirstName = chat.Title ?? chat.FirstName,
- LastName = chat.Title == null ? chat.LastName : "",
- Id = chat.Id,
- IsBot = chat.Type != ChatType.Private,
- LanguageCode = "ru",
- Username = chat.Username
- };
- }
-
- try
- {
- var handler = update.Type switch
- {
- UpdateType.Message =>
- await UpdateHandler.Factory(update.Message.From, update.Message.Text, new MessageHandler(null)),
- UpdateType.CallbackQuery =>
- await UpdateHandler.Factory(update.CallbackQuery.From, update.CallbackQuery.Data, new CallbackQueryHandler(null)),
- UpdateType.ChannelPost =>
- await UpdateHandler.Factory(ChatToUser(update.ChannelPost.Chat), update.ChannelPost.Text, new ChannelPost(null)),
- UpdateType.MyChatMember => await MyChatMemberHandler.Factory(ChatToUser(update.MyChatMember.Chat)),
- _ => await UnknownUpdateHandlerAsync(update)
- };
- var unused = await handler.Execute(update);
- }
- catch (Exception exception)
- {
- await HandleErrorAsync(bot, exception, ct);
- }
- }
- private static async Task<UpdateHandler> UnknownUpdateHandlerAsync(Update update)
- {
- LogOut(update.Type.ToString());
- return new UpdateHandler();
- }
- public static Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception,
- CancellationToken cancellationToken)
- {
- switch (exception)
- {
- case ApiRequestException apiRequestException:
- LogOutWarning($"API Error:[{apiRequestException.ErrorCode}] - {apiRequestException.Message}");
- break;
- default:
- LogOutError(exception.ToString());
- break;
- }
- return Task.CompletedTask;
- }
- public static async Task<Message> SendText(UserEntity info, string message, IReplyMarkup keyboard = null)
- {
- try
- {
- if (!info.IsBlocked)
- return await Bot.Client.SendTextMessageAsync(info.ChatId, message, replyMarkup: keyboard, disableNotification: true);
- }
- catch (Exception e)
- {
- LogOutWarning("Can't send text message " + e);
- }
- return new Message();
- }
-
- public static async Task<Message> SendTextWithHtml(UserEntity info, string message, IReplyMarkup keyboard = null)
- {
- try
- {
- if (!info.IsBlocked)
- return await Bot.Client.SendTextMessageAsync(info.ChatId, message, ParseMode.Html, replyMarkup: keyboard, disableNotification: true);
- }
- catch (Exception e)
- {
- LogOutWarning("Can't send text message with html " + e);
- }
- return new Message();
- }
-
- public static async Task<Message> SendSticker(UserEntity info, string fileId)
- {
- try
- {
- if (!info.IsBlocked)
- return await Bot.Client.SendStickerAsync(info.ChatId, fileId, true);
- }
- catch (Exception e)
- {
- LogOutWarning("Can't send sticker " + e);
- }
- return new Message();
- }
- public static async Task<Message> EditMessage(UserEntity info, int messageId, string message, InlineKeyboardMarkup keyboard = null)
- {
- try
- {
- if (!info.IsBlocked)
- return await Bot.Client.EditMessageTextAsync(info.ChatId, messageId, message, replyMarkup: keyboard);
- }
- catch (Exception e)
- {
- LogOutWarning("Can't edit message text " + e);
- }
- return new Message();
- }
- public static async Task<Message> EditReplyMarkup(UserEntity info, int messageId, InlineKeyboardMarkup keyboard)
- {
- try
- {
- if (!info.IsBlocked)
- return await Bot.Client.EditMessageReplyMarkupAsync(info.ChatId, messageId, keyboard);
- }
- catch (Exception e)
- {
- LogOutWarning("Can't edit reply markup " + e);
- }
- return new Message();
- }
-
- public static async Task DeleteMessage(long chatId, int messageId)
- {
- try
- {
- await Bot.Client.DeleteMessageAsync(chatId, messageId);
- }
- catch (Exception e)
- {
- LogOutWarning("Can't delete message " + e);
- }
- }
- public static async Task<Message> SendImage(UserEntity info, InputOnlineFile inputOnlineFile, string message, InlineKeyboardMarkup replyMarkup)
- {
- try
- {
- if (!info.IsBlocked)
- return await Bot.Client.SendPhotoAsync(info.ChatId, inputOnlineFile, message, replyMarkup: replyMarkup);
- }
- catch (Exception e)
- {
- LogOutWarning("Can't send photo " + e);
- }
- return new Message();
- }
- }
- }
|