Beispiel #1
0
        public string Analyse(Phrase phrase, long chatId)
        {
            if (phrase.HasAllWords("скажи", "контекст"))
            {
                string contextName = _perChatCache.Get <string>(CurrentContextCacheKey, chatId);
                return(contextName ?? "Нет контекста");
            }

            foreach (var contextAnalyzer in _contextAnalyzers)
            {
                if (phrase.HasAllWords(contextAnalyzer.ContextSwitchWords))
                {
                    _perChatCache.Set(CurrentContextCacheKey, chatId, contextAnalyzer.ContextName);
                    var response = contextAnalyzer.Analyse(phrase, chatId);

                    if (response.ClenUpCurrentContext)
                    {
                        _perChatCache.Set <string>(CurrentContextCacheKey, chatId, null);
                    }

                    return(response.Text);
                }
            }

            return(null);
        }
Beispiel #2
0
        public BrainResponse Analyse(Phrase phrase, long chatId)
        {
            var stage = _perChatCache.Get <int?>(StageCacheKey, chatId);

            switch (stage)
            {
            case null:
            case 0:
                _perChatCache.Set <int?>(StageCacheKey, chatId, 1);
                return(new BrainResponse("Скажи кто ты"));

            case 1:
                _perChatCache.Set <int?>(StageCacheKey, chatId, 2);
                return(new BrainResponse("Скажи кого ты любишь"));

            case 2:
                _perChatCache.Set <int?>(StageCacheKey, chatId, 3);
                return(new BrainResponse("Скажи свой вишлист"));

            case 3:
                _perChatCache.Set <int?>(StageCacheKey, chatId, null);
                return(new BrainResponse("Спасибо!", true));

            default:
                return(null);
            }
        }
Beispiel #3
0
        public async Task <IActionResult> Post([FromBody] Update update)
        {
            if (update == null)
            {
                _logger.LogWarning("Incoming null update.");
                return(BadRequest());
            }

            if (update.Message == null)
            {
                // It looks like this is some regular case for telegram.
                // So Ok should be returned
                _logger.LogWarning("Incoming null message.");
                return(Ok());
            }

            _logger.LogInformation($"Incoming message from {update.Message.From.FirstName} {update.Message.From.LastName} ({update.Message.From.Id}): {update.Message.Text}");

            var chatId  = update.Message.Chat.Id;
            var context = _perChatCache.Get <string>(CurrentContextCacheKey, chatId);
            var reply   = _brain.Analyse(update.Message.Text, chatId, update.Message.Type, context);

            if (reply.ClenUpCurrentContext)
            {
                _perChatCache.Set <string>(CurrentContextCacheKey, chatId, null);
            }

            var message = await _client.SendTextMessageAsync(chatId, reply.Text);

            return(NoContent());
        }