Пример #1
0
        private async Task ScheduleSpecialMessages(CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }

            var partsOfDay = new List <PartOfDay>();

            var now       = DateTime.Now;
            var partOfDay = now.PartOfDay();

            partsOfDay.Add(partOfDay);
            partsOfDay.AddRange(partOfDay.Rest);

            var users = await _userStorage.GetAllUsersForScheduledMessagesNoTracking();

            var scheduledCount = 0;

            foreach (var user in users)
            {
                foreach (var part in partsOfDay)
                {
                    var messageTypesForPart = Const.PartOfDay.SpecialMessageTypes[part];
                    foreach (var messageType in messageTypesForPart)
                    {
                        var start = now.Date.AddSeconds(messageType.Range.Start.TotalSeconds);

                        if (now > start)
                        {
                            start = now;
                        }

                        var end = now.Date.AddSeconds(messageType.Range.End.TotalSeconds);

                        if (now > end)
                        {
                            continue;
                        }

                        var schedulingTime = _dateTimeGenerator.GenerateRandomDateTimeWithinRange(new DateTimeRange(start, end));
                        var message        = new ScheduledMessage
                        {
                            ChatId   = user.ChatId,
                            Rarity   = _rarityRoller.RollRarityForUser(user),
                            Type     = Enum.Parse <MessageType>(messageType.Type.ToUpperInvariant()),
                            Time     = schedulingTime,
                            Category = Enum.Parse <MessageCategory>(schedulingTime.PartOfDay().Name.ToUpperInvariant())
                        };

                        await _messageSchedule.AddScheduledMessage(message, cancellationToken);

                        scheduledCount++;
                    }
                }
            }

            _logger.LogInformation($"{scheduledCount} special messages were scheduled.");
        }
        public async Task <List <Message> > SendWakeUpMessageToAllUsersAsync(CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return(null);
            }

            var sentMessages = new List <Message>();

            IEnumerable <UserDbo> users = null;

            using (var context = _dbContextFactory.Create())
            {
                users = context.User
                        .Include(x => x.UserSettings)
                        .Include(x => x.RarityWeights)
                        .Where(x => x.UserSettings.RecieveReminders);
            }

            foreach (var user in users)
            {
                Message sentMessage = null;
                var     rarity      = _rarityRoller.RollRarityForUser(user);
                var     message     = await _messageTextProvider.GetMessage(MessageCategory.WAKEUP, MessageType.STANDARD, rarity, user.ChatId); // could already pass gender and so on

                if (message.ImageUrl == null)
                {
                    sentMessage = await _botClient.SendTextMessageAsync(user.ChatId, message.Text);
                }
                else
                {
                    sentMessage = await _botClient.SendPhotoAsync(user.ChatId, message.ImageUrl, caption : message.Text);
                }

                sentMessages.Add(sentMessage);
            }

            return(sentMessages);
        }
Пример #3
0
        public async Task ProcessUserInput(string userInput, long chatId)
        {
            var realUser = await _userStorage.GetUserByChatIdNoTracking(chatId);

            if (userInput.StartsWith("/"))
            {
                await _commandProcessor.ProcessCommand(userInput.TrimStart('/'), chatId);
            }
            else
            {
                var responseMessage = await _botMessageProvider.GetMessage(
                    category : MessageCategory.SIMPLERESPONSE,
                    type : MessageType.STANDARD,
                    rarity : _rarityRoller.RollRarityForUser(realUser),
                    chatId);

                await _botClient.SendTextMessageAsync(chatId, responseMessage.Text);
            }
        }