예제 #1
0
    public async Task <IResult> Execute(Message msg, BotUser user)
    {
        if (string.IsNullOrWhiteSpace(user.WeatherCity))
        {
            return(new FailedResult("Для получения погоды установите город (нужно написать следующее - установить город Москва)."));
        }

        var dict     = msg.ParsedPayload;
        var isExists = dict.TryGetValue(Trigger, out var day);

        if (!isExists)
        {
            return(new FailedResult("Невозможно получить значение даты"));
        }

        var isCorrectDate = DateTime.TryParse(day, out var dateTime);

        if (!isCorrectDate)
        {
            return(new FailedResult("Некорректное значение даты"));
        }

        var weather = await _weatherService.GetDailyWeather(user.WeatherCity, dateTime);

        weather.Keyboard = DefaultKeyboards.GetDailyWeatherKeyboard();

        return(weather);
    }
예제 #2
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);
        }
    }
예제 #3
0
    public async Task <IResult> GetSchedule(int narfuGroup, DateTime date)
    {
        if (!_narfuApi.Students.IsCorrectGroup(narfuGroup))
        {
            return(new FailedResult($"Группа {narfuGroup} не найдена"));
        }

        try
        {
            var schedule = await _narfuApi.Students.GetScheduleAtDate(narfuGroup, date);

            return(new SuccessfulResult
            {
                Message = schedule.ToString(),
                Keyboard = DefaultKeyboards.GetScheduleKeyboard()
            });
        }
        catch (HttpRequestException)
        {
            return(new FailedResult(DefaultErrors.NarfuSiteIsUnavailable));
        }
        catch (Exception ex)
        {
            Log.ForContext <NarfuApi>().Fatal(ex, "Ошибка при получении расписания на день");
            return(new FailedResult(DefaultErrors.NarfuUnexpectedError));
        }
    }
예제 #4
0
 public Task <IResult> Execute(Message msg, BotUser user)
 {
     return(Task.FromResult <IResult>(new SuccessfulResult
     {
         Message = "Выберите действие:",
         Keyboard = DefaultKeyboards.GetDefaultKeyboard()
     }));
 }
예제 #5
0
    private IScheduleService GetScheduleService()
    {
        var mock = new Mock <IScheduleService>();

        mock.Setup(x => x.GetSchedule(It.IsAny <int>(), It.IsAny <DateTime>()))
        .ReturnsAsync(new SuccessfulResult
        {
            Message  = "Расписание",
            Keyboard = DefaultKeyboards.GetScheduleKeyboard()
        });

        return(mock.Object);
    }
예제 #6
0
    public string[] Aliases => new[] { "ежедневная" }; //TODO: lol

    public Task <IResult> Execute(Message msg, BotUser user)
    {
        if (string.IsNullOrWhiteSpace(user.WeatherCity))
        {
            return(Task.FromResult <IResult>(new FailedResult(DefaultErrors.CityNotSet)));
        }

        return(Task.FromResult <IResult>(new SuccessfulResult
        {
            Message = "Выберите день для получения погоды:",
            Keyboard = DefaultKeyboards.GetDailyWeatherKeyboard()
        }));
    }
예제 #7
0
    public Task <IResult> Execute(Message msg, BotUser user)
    {
        if (user.NarfuGroup == 0)
        {
            return(Task.FromResult <IResult>(new FailedResult(DefaultErrors.GroupNotSet)));
        }

        return(Task.FromResult <IResult>(new SuccessfulResult
        {
            Message = "Выберите день",
            Keyboard = DefaultKeyboards.GetScheduleKeyboard()
        }));
    }
예제 #8
0
    private async Task <IResult> SetWeatherMailing(BotUser user, bool isWeather)
    {
        if (string.IsNullOrWhiteSpace(user.WeatherCity))
        {
            return(new FailedResult(DefaultErrors.CityNotSet));
        }

        user.SetHasWeather(!isWeather);
        await _db.SaveChangesAsync();

        return(new SuccessfulResult
        {
            Message = Success,
            Keyboard = DefaultKeyboards.GetMailingKeyboard(user)
        });
    }
예제 #9
0
    private async Task <IResult> SetScheduleMailing(BotUser user, bool isSchedule)
    {
        if (user.NarfuGroup == 0)
        {
            return(new FailedResult(DefaultErrors.GroupNotSet));
        }

        user.SetHasSchedule(!isSchedule);
        await _db.SaveChangesAsync();

        return(new SuccessfulResult
        {
            Message = Success,
            Keyboard = DefaultKeyboards.GetMailingKeyboard(user)
        });
    }
예제 #10
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);
        }
    }