Telegram bot not responding when searching database by product name

I’m having issues with my telegram bot. Whenever a user enters the name of a product they want to find, the bot fails to reply. I suspect there could be a problem with the way I am executing the database query or processing the incoming message.

def format_product_info(results):
    output = ""
    for item in results:
        product_id = item[0]
        product_name = item[1]
        emoji = item[2]
        output += f"ID: <b>{product_id}</b> | <b>{product_name}</b> | <b>{emoji}</b>\n"
    final_message = "<b>Found 🔍</b> Product details:\n\n" + output
    return final_message

@bot.on(events.NewMessage(pattern="(?i)/search"))
async def search_handler(event):
    try:
        user = await event.get_sender()
        USER_ID = user.id
        product_query = re.match(" .*", event.message.text)
        db_query = "SELECT * FROM products WHERE product_name REGEXP (%s) LIMIT 1;"
        result = cursor.execute(db_query, (product_query,))
        connection.commit()
        
        data = cursor.fetchall()
        
        if result < 1:
            response = f"Product {product_query} not found in database"
            await bot.send_message(USER_ID, response, parse_mode='html')
        else:
            response = format_product_info(data)
            await bot.send_message(USER_ID, response, parse_mode='html')
    except Exception as error:
        print(error)
        await bot.send_message(USER_ID, "Error occurred during search", parse_mode='html')

I also attempted to break down the message into separate words, but it only retrieves the first word instead of the entire product name that users input.

Quick fix - re.match() returns a match object, not text. Use product_query = event.message.text[8:] to skip the '/search ’ part. Also, that if result < 1 check won’t work. Use if not data: since you’re already storing results in the data variable.

Your regex is the problem here. re.match(" .*", event.message.text) gives you a match object, not the actual text string you need for your database query. Use product_query = event.message.text.split(' ', 1)[1] instead - this grabs everything after /search.

Also, if result < 1 won’t work. cursor.execute() returns the cursor object or affected rows, not how many records it found. Check if len(data) < 1 or if not data after you fetch the results.

Double-check your database connection’s working and handle cases where users don’t type anything after the command.

Had the same problem a few months ago. Your regex is passing a match object to the database instead of the actual string. Use product_query = event.message.text.replace('/search ', '', 1) to get the clean search term.

Couple other things: you don’t need connection.commit() after SELECT statements since you’re not changing data. Also check for empty search terms first - people constantly send just /search with no product name.

Add some logging to see what’s actually hitting your database. Print the product_query value right before execute() and you’ll spot the issue immediately.