pythondjangodjango-modelstelegram-botpython-telegram-bot

python telegram bot async not working simultaneously in 2 groups


I have a telegram bot written with python telegram bot package with the latest release and i have a problem, the bot runs fine everything works but when i try to run it in 2** different groups** at the same time, the bot first finishes the first request in group A then process the request from group B this is a example code


    
async def start_blackjack(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    if not await check_player(update, context):
        return
    chat_id = update.message.chat_id
    game = await sync_to_async(lambda: Game.objects.filter(chat_id=chat_id).order_by('-id').first())()

    if not game or game.ended:
        game, created = await sync_to_async(Game.objects.get_or_create)(chat_id=chat_id,  ended=False)
        game.betting = True
        game.started = False
        await sync_to_async(game.save)()

        await asyncio.sleep(2)  # Non-blocking sleep
        await context.bot.send_message(chat_id=chat_id, text="🃏 Set the bet amount for the blackjack game to start.")

        await create_poll(update, context)  # Assume this is an async function
        await asyncio.sleep(2)  # Non-blocking sleep
        await context.bot.send_message(chat_id=chat_id, text="🃏 Betting process has started!")
        return

    if game.betting:
        await update.message.reply_text("A Blackjack game is in betting progress. Wait for the betting process to end and use /join to join the game.")
    elif game.started:
        await update.message.reply_text("A game has already started. Use the /next command to be prompted when the next game starts.")
    elif game.join_time:
        await update.message.reply_text("A game is in join time. Use the /join command to join the game.")
    else:
        await update.message.reply_text("An error occurred, please try again.")

well the code wasn't written with async functions at first then i thought that why doesn't it run in 2 groups at the same time, or why doesn't it process a simple command when bot is running another command, so i re-written the whole thing with async functions and used sync_to_async for django orm , but still the problem exists as if nothing changed. a django model for the bot:


class Game(models.Model):
    chat_id = models.CharField(max_length=100)
    betting = models.BooleanField(default=False)
    started = models.BooleanField(default=False)
    join_time = models.BooleanField(default=False)
    ended = models.BooleanField(default=False)
    hands_reset = models.BooleanField(default=False)
    bet_amount = models.IntegerField(default=5)
    dealer_hand = models.JSONField(default=list)  # To store the dealer's hand
    turn_order = models.JSONField(default=list)  # To store the turn order
    current_round = models.IntegerField(default=1)  # Current round number
    total_rounds = models.IntegerField(default=5)  # Total number of rounds in the game
    pot = models.IntegerField(default=0)  # Total pot amount
    join_start_time = models.DateTimeField(null=True, blank=True)  # Time when join period starts


Solution

  • The default behavior in python-telegram-bot is to process callbacks in a blocking manner. if you haven't done so already, you need to enable concurrent updates when building the application like so:

    Application.builder().token('TOKEN').concurrent_updates(True).build()
    

    source: https://github.com/python-telegram-bot/python-telegram-bot/wiki/Concurrency#applicationconcurrent_updates