Search code examples
pythontelegram-botpython-telegram-botjob-queue

Python Jobqueue multiple messaging problem


Jobqueue sends the same message 2 times even though the user_last_len value is the same when the user moves fast in the program and enters the function quickly, but when the user behaves slowly and enters the function normally, it sends 1 as it should, I tried to debug the program and understand why it did it, but I do not encounter any problems while debugging and it sends the message 1 time.

user_last_len = {}

async def first(update: Update, context: CallbackContext):

    async def check(context: CallbackContext):

        await asyncio.sleep(2)

        try:
            conn = connection_pool.get_connection()
            cursor = conn.cursor()

            user_id = update.message.from_user.id
            cursor.execute("SELECT LikeID FROM Likes WHERE LikedID = %s", (user_id,))
            likes = cursor.fetchall()

            len_likes = len(likes)
            last_len = user_last_len.get(user_id, 0)

            if len_likes > 0 and last_len != len_likes:
                await update.message.reply_text(f"{len_likes}", reply_markup=show_markup)
                user_last_len[user_id] = len_likes
                context.job.data = True

            cursor.close()
            conn.close()

        except Exception as e:
            pass
            
        finally:
            await asyncio.sleep(1)

    job = context.job_queue.run_repeating(check, interval=300, first=0, data=None, name="dgc")
    await job.run(context.application)

Solution

  • The way to solve the problem was to make user_last_len[user_id] = len_likes before sending the message because I realized that when it sends the message, it goes back to the beginning of the try block and enters the if block again, so it cannot check last_len != len_likes properly because it returns to the beginning of the try block before it can do user_last_len[user_id] = len_likes.

    user_last_len = {}
    
    async def first(update: Update, context: CallbackContext):
    
        async def check(context: CallbackContext):
    
            await asyncio.sleep(2)
    
            try:
                conn = connection_pool.get_connection()
                cursor = conn.cursor()
    
                user_id = update.message.from_user.id
                cursor.execute("SELECT LikeID FROM Likes WHERE LikedID = %s", (user_id,))
                likes = cursor.fetchall()
    
                len_likes = len(likes)
                last_len = user_last_len.get(user_id, 0)
    
                if len_likes > 0 and > last_len != len_likes:
                    user_last_len[user_id] = len_likes
    
                    await update.message.reply_text(f"{len_likes}", reply_markup=show_markup)
                    
                    context.job.data = True
    
                cursor.close()
                conn.close()
    
            except Exception as e:
                pass
                
            finally:
                await asyncio.sleep(1)
    
        job = context.job_queue.run_repeating(check, interval=300, first=0, data=None, name="dgc")
        await job.run(context.application)