Search code examples
c#.netazure-functionstelegramtelegram-bot

Answering consecutive inline queries makes no inline change


Only the first initialResult is being rendered on my telegram client. The second response never even happens. Is it even possible sending multiple consecutive results to a single call?

if (update.InlineQuery != null)
{
   if (update.InlineQuery.Query.Count() > 3)
   {
        var inlineQuery = update.InlineQuery;

        try
        {
            var initialResult = new InlineQueryResult[]
            {
                new InlineQueryResultArticle
                (
                    id: "1",
                    title: $"Loading...",
                    inputMessageContent: new InputTextMessageContent("test") 
                ),
            };

            await Bot.AnswerInlineQueryAsync(inlineQuery.Id, initialResult, isPersonal: true, cacheTime: 0, nextOffset: null);

            var realResponse = new InlineQueryResult[]
            {
                new InlineQueryResultArticle
                (
                    id: "2",
                    title: $"this is real content",
                    inputMessageContent: new InputTextMessageContent("test2") 
                ),
            };

            await Bot.AnswerInlineQueryAsync(inlineQuery.Id, realResponse, isPersonal: true, cacheTime: 0, nextOffset: null);
        } 

        catch(Exception ex)
        {
            
        }
        finally
        {

        }
    }
}

Solution

  • Is it even possible sending multiple consecutive results to a single call?

    It is not possible to send two separate responses to a single inline query in Telegram using a single attempt. Each time you call the AnswerInlineQueryAsync method, it will provide a single response for that particular inline query. The InlineQueryResult[] array will send it in one call to AnswerInlineQueryAsync. Each element in the array represents a single item that can be displayed to the user.

    Using the code below is one approach to get consecutive responses.

    if (update.InlineQuery != null)
    {
        var inlineQuery = update.InlineQuery;
    
        try
        {
            var multipleResults = new InlineQueryResult[]
            {
                new InlineQueryResultArticle
                (
                    id: "1",
                    title: $"Loading...",
                    inputMessageContent: new InputTextMessageContent("test") 
                ),
                new InlineQueryResultArticle
                (
                    id: "2",
                    title: $"this is real content",
                    inputMessageContent: new InputTextMessageContent("test2") 
                ),
                // Add more InlineQueryResultArticle objects if needed...
            };
    
            await Bot.AnswerInlineQueryAsync(inlineQuery.Id, multipleResults, isPersonal: true, cacheTime: 0, nextOffset: null);
        } 
        catch(Exception ex)
        {
            // Handle exceptions
        }
    }
    
    

    By using below code is another approach to get consecutive responses.

    if (update.InlineQuery != null)
    {
        if (update.InlineQuery.Query.Count() > 3)
        {
            var inlineQuery = update.InlineQuery;
    
            try
            {
                var initialResult = new InlineQueryResult[]
                {
                    new InlineQueryResultArticle
                    (
                        id: "1",
                        title: $"Loading...",
                        inputMessageContent: new InputTextMessageContent("test") 
                    ),
                };
    
                await Bot.AnswerInlineQueryAsync(inlineQuery.Id, initialResult, isPersonal: true, cacheTime: 0, nextOffset: "offset_for_pagination");
    
                var realResponse = new InlineQueryResult[]
                {
                    new InlineQueryResultArticle
                    (
                        id: "2",
                        title: $"this is real content",
                        inputMessageContent: new InputTextMessageContent("test2") 
                    ),
                };
    
                await Bot.AnswerInlineQueryAsync(inlineQuery.Id, realResponse, isPersonal: true, cacheTime: 0, nextOffset: null);
            } 
            catch(Exception ex)
            {
                // Handle exceptions
            }
            finally
            {
                // Any cleanup or final operations
            }
        }
    }
    
    

    By passing null for nextOffset, you might prevent the second set of results from being displayed. To correct this issue:

    • To set a valid nextOffset value for the first call to AnswerInlineQueryAsync.