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.