using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Newtonsoft.Json; using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; namespace TelegramBotBase.Base; public class MessageResult : ResultBase { public MessageResult(Update update) { UpdateData = update; init(); } void init() { IsAction = UpdateData.CallbackQuery != null; IsBotCommand = Message.Entities?.Any(a => a.Type == MessageEntityType.BotCommand) ?? false; if (!IsBotCommand) return; BotCommand = MessageText.Split(' ')[0]; IsBotGroupCommand = BotCommand.Contains("@"); if (IsBotGroupCommand) { BotCommand = BotCommand.Substring(0, BotCommand.LastIndexOf('@')); } } public Update UpdateData { get; private set; } /// /// Returns the Device/ChatId /// public override long DeviceId => UpdateData?.Message?.Chat?.Id ?? UpdateData?.EditedMessage?.Chat.Id ?? UpdateData?.CallbackQuery.Message?.Chat.Id ?? Device?.DeviceId ?? 0; /// /// The message id /// public override int MessageId => UpdateData?.Message?.MessageId ?? Message?.MessageId ?? UpdateData?.CallbackQuery?.Message?.MessageId ?? 0; public string Command => UpdateData?.Message?.Text ?? ""; public string MessageText => UpdateData?.Message?.Text ?? ""; public MessageType MessageType => Message?.Type ?? MessageType.Unknown; public override Message Message => UpdateData?.Message ?? UpdateData?.EditedMessage ?? UpdateData?.ChannelPost ?? UpdateData?.EditedChannelPost ?? UpdateData?.CallbackQuery?.Message; /// /// Is this an action ? (i.e. button click) /// public bool IsAction { get; private set; } /// /// Is this a command ? Starts with a slash '/' and a command /// public bool IsBotCommand { get; private set; } /// /// Is this a bot command sent from a group via @BotId ? /// public bool IsBotGroupCommand { get; private set; } /// /// Returns a List of all parameters which has been sent with the command itself (i.e. /start 123 456 789 => /// 123,456,789) /// public List BotCommandParameters { get { if (!IsBotCommand) { return new List(); } //Split by empty space and skip first entry (command itself), return as list return MessageText.Split(' ').Skip(1).ToList(); } } /// /// Returns just the command (i.e. /start 1 2 3 => /start) /// public string BotCommand { get; private set; } /// /// Returns if this message will be used on the first form or not. /// public bool IsFirstHandler { get; set; } = true; public bool Handled { get; set; } = false; public string RawData => UpdateData?.CallbackQuery?.Data; public T GetData() where T : class { T cd = null; try { cd = JsonConvert.DeserializeObject(RawData); return cd; } catch { } return null; } /// /// Confirm incoming action (i.e. Button click) /// /// /// public async Task ConfirmAction(string message = "", bool showAlert = false, string urlToOpen = null) { await Device.ConfirmAction(UpdateData.CallbackQuery.Id, message, showAlert, urlToOpen); } public override async Task DeleteMessage() { try { await base.DeleteMessage(MessageId); } catch { } } }