예제 #1
0
    public async Task ExecuteCommand(Message msg,
                                     Func <IResult, Task> onSuccess,
                                     Func <IResult, Task> onFailed)
    {
        IResult result;
        var     user = await GetBotUser(msg.UserId, msg.ConsumerType);

        if (!string.IsNullOrWhiteSpace(msg.Payload))
        {
            result = await ExecuteKeyboardCommand(msg, user);
        }
        else
        {
            result = await ExecuteTextCommand(msg, user);
        }

        result.Keyboard ??= DefaultKeyboards.GetDefaultKeyboard();

        if (!result.IsSuccessful)
        {
            if (result is CommandNotFoundResult && !user.IsErrorsEnabled)
            {
                // если команда не найдена, и у пользователя отключены ошибки
                return;
            }

            await onFailed(result);
        }
        else
        {
            await onSuccess(result);
        }
    }
예제 #2
0
 public Task <IResult> Execute(Message msg, BotUser user)
 {
     return(Task.FromResult <IResult>(new SuccessfulResult
     {
         Message = "Выберите действие:",
         Keyboard = DefaultKeyboards.GetDefaultKeyboard()
     }));
 }
예제 #3
0
    public async Task SendToAll(string text, ICollection <string> attachments, bool isSendKeyboard, ConsumerType type)
    {
        var keyboard = isSendKeyboard ? DefaultKeyboards.GetDefaultKeyboard() : null;

        if (type == ConsumerType.AllInOne)
        {
            var groupedByConsumerType = _db.BotUsers.GroupBy(x => x.ConsumerType);
            foreach (var consumersGroup in groupedByConsumerType)
            {
                var users  = consumersGroup.Select(x => x.Id).AsEnumerable();
                var sender = _senders.FirstOrDefault(x => x.ConsumerType == consumersGroup.Key);
                await sender.SendToMany(users, text, keyboard, attachments);
            }
        }
        else
        {
            var users  = _db.BotUsers.Where(x => x.ConsumerType == type).Select(x => x.Id).AsEnumerable();
            var sender = _senders.FirstOrDefault(x => x.ConsumerType == type);
            await sender.SendToMany(users, text, keyboard, attachments);
        }
    }