Ejemplo n.º 1
0
        public static async Task TestTelegramNotAdminUser(ITelegramBotCommand command, Mock <ITelegramBotClientWrapper> telegramBotClientMock)
        {
            // Arrange
            const string replyMessage = "Only admins can use this command.";

            var message = new Message
            {
                Text = command.Command,
                From = new User {
                    Id = 2
                },
                Chat = new Chat
                {
                    Id   = 1,
                    Type = ChatType.Group
                }
            };

            telegramBotClientMock.Setup(v => v.GetChatAdministratorsAsync(message.Chat.Id, default))
            .ReturnsAsync(new ChatMember[0]);

            // Act
            await command.Handle(message);

            // Assert
            telegramBotClientMock.Verify(v => v.SendTextMessageAsync(message.Chat.Id, It.Is <string>(s => s.Contains(replyMessage)), default, default, default, default, message.MessageId, default, default, default));
        /// <summary>
        ///
        /// </summary>
        /// <param name="messageContext"></param>
        /// <param name="command"></param>
        protected virtual void TryExecuteCommand(MessageContext messageContext, ITelegramBotCommand command)
        {
            messageContext.IsCanExecuteNextHandler = false;
            messageContext.HandlerCommand          = command;

            command?.Execute();
        }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="command"></param>
        /// <param name="servicesProvider"></param>
        public TelegramBotCommandService(ITelegramBotCommand command, ITelegramBotServicesProvider servicesProvider)
        {
            Command = command;
            TelegramBotServicesProvider = servicesProvider;
            TelegramBotCommandFactory   = servicesProvider.GetService <ITelegramBotCommandFactory>();

            Info           = TelegramBotCommandFactory.GetCommandInfo(command.GetType());
            MessageService = TelegramBotServicesProvider.GetService <ITelegramBotMessageService>();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="messageContext"></param>
        /// <param name="ex"></param>
        protected virtual void CatchExecuteCommand(MessageContext messageContext, ITelegramBotCommand command, Exception ex)
        {
            if (command is ICommandMessageExceptionHandler exceptionHandler)
            {
                exceptionHandler.HandleExecuteException(messageContext, ex);
            }

            var handlerService = ServicesProvider.GetService <ICommandMessageExceptionHandler>();

            handlerService?.HandleExecuteException(messageContext, ex);
        }
 /// <summary>
 ///
 /// </summary>
 protected virtual void ExecuteCommand(MessageContext messageContext, ITelegramBotCommand command)
 {
     try
     {
         TryExecuteCommand(messageContext, command);
     }
     catch (Exception ex)
     {
         CatchExecuteCommand(messageContext, command, ex);
     }
 }