Beispiel #1
0
        public async Task InlineQuery(InlineQuery inlineQuery)
        {
            var userId = inlineQuery.From.Id;
            var q      = inlineQuery.Query;

            if (!_checker.AllowUser(userId))
            {
                return;
            }

            if (q.Length > MaxInlineQueryLength)
            {
                return;
            }

            if (!_repository.IsUserExist(userId))
            {
                _repository.AddUser(new Bot.Rest.Model.User
                {
                    IdUser = userId,
                    Name   = $"{inlineQuery.From.FirstName} {inlineQuery.From.LastName}",
                    Mode   = EGettingWordsStrategy.Random.ToString()
                });
            }

            WordSearchResult[] results;

            // ReSharper disable once ConvertIfStatementToConditionalTernaryExpression
            if (q.Any())
            {
                results = _repository.FindFlashCard(q, userId).ToArray();
            }
            else
            {
                results = _repository.GetLastWords(userId, MaxInlineSearchResult).ToArray();
            }

            if (!results.Any())
            {
                await _client.AnswerInlineQueryAsync(inlineQuery.Id, new InlineQueryResultBase[]
                {
                    new InlineQueryResultArticle(inlineQuery.Id, "Please, type a chinese word to view it's flashcard",
                                                 new InputTextMessageContent("Still can't show you a flashcard")
                    {
                        DisableWebPagePreview = true,
                        ParseMode             = ParseMode.Default
                    })
                });

                _checker.UserQueryProcessed(userId);
                return;
            }


            var inlineQueryResults = results.Select(
                a =>
                new InlineQueryResultPhoto(Guid.NewGuid().ToString(), _flashCardUrl + a.File.Id,
                                           _flashCardUrl + a.File.Id)
            {
                Caption     = a.OriginalWord,
                PhotoHeight = a.File.Height,
                PhotoWidth  = a.File.Width
            });


            await _client.AnswerInlineQueryAsync(inlineQuery.Id, inlineQueryResults.ToArray(), 0);

            _checker.UserQueryProcessed(userId);
        }