コード例 #1
0
    public async Task BotNotStopped_Should_ReplyBotStartedMessage()
    {
        // Arrange
        var          chatConfigurationServiceMock = new Mock <IChatConfigurationService>();
        var          telegramBotClientMock        = new Mock <ITelegramBotClientWrapper>();
        var          command      = new StartCommand(chatConfigurationServiceMock.Object, telegramBotClientMock.Object);
        const string replyMessage = "Bot is already started";

        var chatConfig = new ChatConfiguration
        {
            IsBotStopped = false
        };

        var message = new Message
        {
            Text = TelegramBotCommands.Start,
            Chat = new Chat
            {
                Id = 1
            }
        };

        chatConfigurationServiceMock.Setup(v => v.GetConfigurationByChatId(message.Chat.Id))
        .ReturnsAsync(chatConfig);

        // 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, default, default, default, default));
コード例 #2
0
        public async Task BotNotStopped_Should_ReplyBotStartedMessage()
        {
            // Arrange
            var          channelConfigurationServiceMock = new Mock <IDiscordChannelConfigService>();
            var          command      = new StartCommand(channelConfigurationServiceMock.Object);
            const string replyMessage = "Bot is already started";

            var chatConfig = new DiscordChannelConfig
            {
                IsBotStopped = false
            };

            var channelMock = new Mock <IMessageChannel>();
            var message     = new Mock <IMessage>();

            message.Setup(v => v.Channel).Returns(channelMock.Object);

            channelConfigurationServiceMock.Setup(v => v.GetConfigurationByChannelId(message.Object.Channel.Id))
            .ReturnsAsync(chatConfig);

            // Act
            await command.Handle(message.Object);

            // Assert

            // Verify SendMessageAsync was called with the reply message "Bot is already started"
            channelMock.Verify(v => v.SendMessageAsync(null, false, It.Is <Embed>(e => e.Description.Contains(replyMessage)), null, null, null, null, null, null, MessageFlags.None));
        }
コード例 #3
0
        public async Task BotStoppedAndUserAdmin_Should_ChangeChatConfig_And_ReplyMessage()
        {
            // Arrange
            var          channelConfigurationServiceMock = new Mock <IDiscordChannelConfigService>();
            var          command      = new StartCommand(channelConfigurationServiceMock.Object);
            const string replyMessage = "Bot started";

            var chatConfig = new DiscordChannelConfig
            {
                IsBotStopped = true
            };

            var channelMock = new Mock <IMessageChannel>();
            var user        = new Mock <IGuildUser>();

            user.Setup(v => v.GuildPermissions).Returns(GuildPermissions.All);
            var message = new Mock <IMessage>();

            message.Setup(v => v.Author).Returns(user.Object);
            message.Setup(v => v.Channel).Returns(channelMock.Object);

            channelConfigurationServiceMock.Setup(v => v.GetConfigurationByChannelId(message.Object.Channel.Id))
            .ReturnsAsync(chatConfig);

            // Act
            await command.Handle(message.Object);

            // Assert

            // Verify SendMessageAsync was called with the reply message "Bot started"
            channelMock.Verify(v => v.SendMessageAsync(null, false, It.Is <Embed>(e => e.Description.Contains(replyMessage)), null, null, null, null, null, null, MessageFlags.None));
            Assert.False(chatConfig.IsBotStopped);
        }
コード例 #4
0
        private Task AnswerBotCommandAsync(ITelegramBotClient client, Message message, CancellationToken cancellationToken)
        {
            var command = message.Text
                          .Replace('@', ' ')
                          .Split(' ')
                          .First()
                          .Replace("/", "");

            return(L10nCommands.Normalize(command) switch
            {
                "/start" => _startCommand.Handle(client, message, cancellationToken),
                "/packages" => _packagesCommand.Handle(client, message, cancellationToken),
                "/change_password" => _updatePasswordCommand.Handle(client, message, cancellationToken),
                "/stop" => _stopCommand.Handle(client, message, cancellationToken),
                "/psa" => Task.CompletedTask,
                _ => Task.Run(() =>
                {
                    _logger.LogWarning("Unrecognized command send by user {User}. Command: {Command}", message.From, command);
                    return Task.CompletedTask;
                })
            });