Ejemplo n.º 1
0
        private async Task ProcessMessageAsync(string message, Message rawMessage = null)
        {
            if (message == "/cancel")
            {
                _awaitingResponseCallback = null;
                await BotClient.SendTextMessageAsync(ChatId, "Send /start for a list of commands.",
                                                     replyMarkup : new ReplyKeyboardRemove());

                return;
            }

            if (_awaitingResponseCallback != null)
            {
                var invokable = _awaitingResponseCallback.Invoke(message);
                _awaitingResponseCallback = null;
                await invokable;
                return;
            }

            _logger.LogInformation("@{username} sent {message} on root conversation.", _user.Username, message);

            var userPermissions = await GetPermissionsAsync();

            if (message == "/start")
            {
                var availableCommands = _commands
                                        .Where(cmd => HasPermission(userPermissions, cmd.RequiredPermission))
                                        .OrderBy(a => a.Command)
                                        .ToList();

                var response = new StringBuilder();

                if (availableCommands.Count == 0)
                {
                    response.AppendLine(
                        "Sorry, I don't have any commands for you that you have access to. If you think this is in error, contact @Pinselohrkater");
                }
                else
                {
                    response.AppendLine("You have access to the following commands:\n");
                    foreach (var cmd in availableCommands)
                    {
                        response.AppendLine($"{cmd.Command} - {cmd.Description}");
                    }
                }
                await ReplyAsync(response.ToString());

                return;
            }

            var matchingCommand = _commands.SingleOrDefault(a => a.Command.Equals(message, StringComparison.CurrentCultureIgnoreCase));

            if (matchingCommand != null && HasPermission(userPermissions, matchingCommand.RequiredPermission))
            {
                await matchingCommand.CommandHandler();

                return;
            }

            if (rawMessage != null && HasPermission(PermissionFlags.All, userPermissions))
            {
                if (message.StartsWith("/chatid@", StringComparison.CurrentCultureIgnoreCase))
                {
                    await BotClient.SendTextMessageAsync(rawMessage.Chat.Id,
                                                         $"ChatId={rawMessage.Chat.Id} ({rawMessage.Chat.Title})");
                }
                if (message.StartsWith("/leave@", StringComparison.CurrentCultureIgnoreCase))
                {
                    await BotClient.LeaveChatAsync(rawMessage.Chat.Id);
                }
            }
        }