From db5cd93977a466392d13b0e54347ec02ab016e64 Mon Sep 17 00:00:00 2001 From: Sergey Gut Date: Wed, 29 May 2024 01:48:45 +0300 Subject: [PATCH] work on commands --- .../Data/InputFile/ICustomInputFile.cs | 9 ++++ .../Data/InputFile/InputIdFile.cs | 18 ++++++++ .../Data/InputFile/InputUrlFile.cs | 18 ++++++++ CustomChatAddOns/Data/NewMessage.cs | 13 ++++++ .../Data/WebhookCommand/BaseReplyMessage.cs | 17 +++++++ .../Data/WebhookCommand/IWebhookCommand.cs | 10 +++++ .../Data/WebhookCommand/PhotoReplyMessage.cs | 20 +++++++++ .../Data/WebhookCommand/TextReplyMessage.cs | 21 +++++++++ CustomChatAddOns/Handler/MessageHandler.cs | 44 +++++++++++++++++++ 9 files changed, 170 insertions(+) create mode 100644 CustomChatAddOns/Data/InputFile/ICustomInputFile.cs create mode 100644 CustomChatAddOns/Data/InputFile/InputIdFile.cs create mode 100644 CustomChatAddOns/Data/InputFile/InputUrlFile.cs create mode 100644 CustomChatAddOns/Data/NewMessage.cs create mode 100644 CustomChatAddOns/Data/WebhookCommand/BaseReplyMessage.cs create mode 100644 CustomChatAddOns/Data/WebhookCommand/IWebhookCommand.cs create mode 100644 CustomChatAddOns/Data/WebhookCommand/PhotoReplyMessage.cs create mode 100644 CustomChatAddOns/Data/WebhookCommand/TextReplyMessage.cs create mode 100644 CustomChatAddOns/Handler/MessageHandler.cs diff --git a/CustomChatAddOns/Data/InputFile/ICustomInputFile.cs b/CustomChatAddOns/Data/InputFile/ICustomInputFile.cs new file mode 100644 index 0000000..83ccf28 --- /dev/null +++ b/CustomChatAddOns/Data/InputFile/ICustomInputFile.cs @@ -0,0 +1,9 @@ +using Telegram.Bot.Types; + +namespace Kruzya.TelegramBot.CustomChatAddOns.Data.InputFile +{ + public interface ICustomInputFile + { + public IInputFile Convert(); + } +} diff --git a/CustomChatAddOns/Data/InputFile/InputIdFile.cs b/CustomChatAddOns/Data/InputFile/InputIdFile.cs new file mode 100644 index 0000000..7e01f40 --- /dev/null +++ b/CustomChatAddOns/Data/InputFile/InputIdFile.cs @@ -0,0 +1,18 @@ +using Kruzya.TelegramBot.Core.Attributes; +using Newtonsoft.Json; +using Telegram.Bot.Types; + +namespace Kruzya.TelegramBot.CustomChatAddOns.Data.InputFile +{ + [AllowedSerializableType("input_id_file")] + public class InputIdFile : ICustomInputFile + { + [JsonProperty("id")] + public string Id { get; set; } + + public IInputFile Convert() + { + return new InputFileId(Id); + } + } +} diff --git a/CustomChatAddOns/Data/InputFile/InputUrlFile.cs b/CustomChatAddOns/Data/InputFile/InputUrlFile.cs new file mode 100644 index 0000000..1fdcf92 --- /dev/null +++ b/CustomChatAddOns/Data/InputFile/InputUrlFile.cs @@ -0,0 +1,18 @@ +using Kruzya.TelegramBot.Core.Attributes; +using Newtonsoft.Json; +using Telegram.Bot.Types; + +namespace Kruzya.TelegramBot.CustomChatAddOns.Data.InputFile +{ + [AllowedSerializableType("input_url_file")] + public class InputUrlFile : ICustomInputFile + { + [JsonProperty("url")] + public string Url { get; set; } = ""; + + public IInputFile Convert() + { + return new InputFileUrl(Url); + } + } +} diff --git a/CustomChatAddOns/Data/NewMessage.cs b/CustomChatAddOns/Data/NewMessage.cs new file mode 100644 index 0000000..5a02c6a --- /dev/null +++ b/CustomChatAddOns/Data/NewMessage.cs @@ -0,0 +1,13 @@ +using Kruzya.TelegramBot.Core.Attributes; +using Newtonsoft.Json; +using Telegram.Bot.Types; + +namespace Kruzya.TelegramBot.CustomChatAddOns.Data +{ + [AllowedSerializableType("new_message")] + public class NewMessage + { + [JsonProperty("message")] + public Message Message { get; set; } + } +} diff --git a/CustomChatAddOns/Data/WebhookCommand/BaseReplyMessage.cs b/CustomChatAddOns/Data/WebhookCommand/BaseReplyMessage.cs new file mode 100644 index 0000000..70085dc --- /dev/null +++ b/CustomChatAddOns/Data/WebhookCommand/BaseReplyMessage.cs @@ -0,0 +1,17 @@ +using Newtonsoft.Json; +using Telegram.Bot.Types.Enums; + +namespace Kruzya.TelegramBot.CustomChatAddOns.Data.WebhookCommand +{ + public abstract class BaseReplyMessage + { + [JsonProperty("content")] + public string Content { get; set; } + + [JsonProperty("parse_mode")] + public ParseMode? ParseMode { get; set; } + + [JsonProperty("disable_notification")] + public bool? DisableNotification { get; set; } + } +} diff --git a/CustomChatAddOns/Data/WebhookCommand/IWebhookCommand.cs b/CustomChatAddOns/Data/WebhookCommand/IWebhookCommand.cs new file mode 100644 index 0000000..cddf41c --- /dev/null +++ b/CustomChatAddOns/Data/WebhookCommand/IWebhookCommand.cs @@ -0,0 +1,10 @@ +using Telegram.Bot; +using Telegram.Bot.Types; + +namespace Kruzya.TelegramBot.CustomChatAddOns.Data.WebhookCommand +{ + public interface IWebhookCommand + { + public Task Execute(ITelegramBotClient botClient, Message message); + } +} diff --git a/CustomChatAddOns/Data/WebhookCommand/PhotoReplyMessage.cs b/CustomChatAddOns/Data/WebhookCommand/PhotoReplyMessage.cs new file mode 100644 index 0000000..f9f4863 --- /dev/null +++ b/CustomChatAddOns/Data/WebhookCommand/PhotoReplyMessage.cs @@ -0,0 +1,20 @@ +using Kruzya.TelegramBot.CustomChatAddOns.Data.InputFile; +using Newtonsoft.Json; +using Telegram.Bot; +using Telegram.Bot.Types; + +namespace Kruzya.TelegramBot.CustomChatAddOns.Data.WebhookCommand +{ + public class PhotoReplyMessage : BaseReplyMessage, IWebhookCommand + { + [JsonProperty("photo")] + public ICustomInputFile Photo { get; set; } + + public async Task Execute(ITelegramBotClient botClient, Message message) + { + await botClient.SendPhotoAsync(message.Chat.Id, Photo.Convert(), + caption: Content, parseMode: ParseMode, + disableNotification: DisableNotification); + } + } +} diff --git a/CustomChatAddOns/Data/WebhookCommand/TextReplyMessage.cs b/CustomChatAddOns/Data/WebhookCommand/TextReplyMessage.cs new file mode 100644 index 0000000..f0611df --- /dev/null +++ b/CustomChatAddOns/Data/WebhookCommand/TextReplyMessage.cs @@ -0,0 +1,21 @@ +using Kruzya.TelegramBot.Core.Attributes; +using Newtonsoft.Json; +using Telegram.Bot; +using Telegram.Bot.Types; + +namespace Kruzya.TelegramBot.CustomChatAddOns.Data.WebhookCommand +{ + [AllowedSerializableType("text_reply_message")] + public class TextReplyMessage : BaseReplyMessage, IWebhookCommand + { + [JsonProperty("disable_web_page_preview")] + public bool? DisableWebPagePreview { get; set; } + + public async Task Execute(ITelegramBotClient botClient, Message message) + { + await botClient.SendTextMessageAsync(message.Chat.Id, Content, + parseMode: ParseMode, disableNotification: DisableNotification, + disableWebPagePreview: DisableWebPagePreview); + } + } +} diff --git a/CustomChatAddOns/Handler/MessageHandler.cs b/CustomChatAddOns/Handler/MessageHandler.cs new file mode 100644 index 0000000..d4f7c1c --- /dev/null +++ b/CustomChatAddOns/Handler/MessageHandler.cs @@ -0,0 +1,44 @@ +using BotFramework; +using BotFramework.Attributes; +using Kruzya.TelegramBot.CustomChatAddOns.Data; +using Kruzya.TelegramBot.CustomChatAddOns.Data.WebhookCommand; +using Kruzya.TelegramBot.CustomChatAddOns.Options; +using Kruzya.TelegramBot.CustomChatAddOns.Service; + +namespace Kruzya.TelegramBot.CustomChatAddOns.Handler +{ + public class MessageHandler : BotEventHandler + { + private readonly CustomChatHandler _option; + private readonly InteropService _interopService; + + public MessageHandler(CustomChatHandler option, InteropService interopService) + { + _option = option; + _interopService = interopService; + } + + [Message] + [Priority(short.MinValue + 10)] + public async Task DeliverMessage() + { + var message = RawUpdate.Message; + if (message == null) + { + return; + } + + var url = await _option.GetValueAsync(Chat.Id); + if (string.IsNullOrWhiteSpace(url)) + { + return; + } + + var commands = await _interopService.PostMessage>(url, new() { Message = message }); + foreach (var command in commands) + { + await command.Execute(Bot, message); + } + } + } +}