Search code examples
c#multithreadingapitelegram-botlong-polling

long polling api program doesn't make multi threads to make difference between users


i have a telegram bot API program using long polling to run this bot i put this function (that enable my telegram bot) in Global.asax and call it in Application_Start() when i start project telegram bot works properly until it has 1 user, when 2nd user start to work with bot the user 2nd user update doesn't make another thread and it works in the following of the 1st user progress!

For example: user #1 send a message (update) as "AnswerForm" so bot pass first condition and wait for user to enter Form Id in a long polling cycle, in this time when another user start bot and send message to him bot receive it as user 1 Form Id! i guessed it would make another thread for him but i don't know i doesn't happen. So how can I solve this?

static async void testApiAsync()
{
    try
    {
        var Bot = new Telegram.Bot.TelegramBotClient("my_token");
        var me = await Bot.GetMeAsync();
        int offset = 0;
        while (true)
        {
            var updates = await Bot.GetUpdatesAsync(offset);
            foreach (var update in updates)
            {
                offset = update.Id + 1; //new offset id
                if (update.Message != null)
                {
                    if (update.Message.Text == "AnswerForm")
                    {
                        string askFormId = "send Form Id";
                        await Bot.SendTextMessageAsync(update.Message.Chat.Id, askFormId);
                        while (true)
                        {
                            //here was my code that
                            //wait for user to enter Form Id
                        }
                    }
                }
            }
        }
    }
    catch { }
}

Solution

  • I found the problem! it was deep and big! in this api user does not submit any thing and in server i don't receive continuously thread to authenticate each user with!

    so to solve the problem i should Track each user using save their session in DB and any time they send new message i follow what they did before and let them do the following...