Python Forum

Full Version: Help Python Bot to search a database
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello guys.. I'm new here, and I hope I'm posting this at the right section. anyway, Im creating a bot for a platform with this code:

from typing import Final
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes

TOKEN: Final = '*T*O*K*E*N*'
BOT_USERNAME: Final = '@My_Bot'

async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await update.message.reply_text("Please follow the format when typing in your search: ProductType, ProductName, StoreName")


async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await update.message.reply_text("Please send a message to @My_Bot_Admin")


async def custom_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await update.message.reply_text("I Love This Bot!")



def handle_response(text: str) -> str:
    processed: str = text.lower()

    if 'hello' in processed:
        return 'Hey there!'

    if 'how are you' in processed:
        return 'I am good'

    return 'I do not understand...'

async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
    message_type: str = update.message.chat.type
    text: str = update.message.text

    print(f'User ({update.message.chat.id}) in {message_type}: "{text}"')

    if message_type == 'group':
        if BOT_USERNAME in text:
            new_text: str = text.replace(BOT_USERNAME, '').strip()
            response: str = handle_response(new_text)
        else:
            return
    else:
        response: str = handle_response(text)

    print('Bot:', response)
    await update.message.reply_text(response)

async def error(update: Update, context: ContextTypes.DEFAULT_TYPE):
    print(f'Update {update} caused error {context.error}')


if __name__ == '__main__':
    print('Starting bot...')
    app = Application.builder().token(TOKEN).build()


    app.add_handler(CommandHandler('start', start_command))
    app.add_handler(CommandHandler('help', help_command))
    app.add_handler(CommandHandler('custom', custom_command))

    app.add_handler(MessageHandler(filters.TEXT, handle_message))

    app.add_error_handler(error)
    print('Polling...')
    app.run_polling(poll_interval=3)
Its already responding to "hello" and "how are you".. what I want it to do is when the user sends the right format of message, it'll search through a database which is uploaded online, and when a match is found, it'll respond with the row of the matched information.. my database is a csv file with multiple rows and each rows has multiple values separated with a comma: example:

Quote:Item#, ProductType, ProductName, Brand, Flavor, StoreName, Quantity, Price

so if a user sends the bot a message like this:
Quote:ProductType, ProductName, StoreName

The bot should respond with:
Quote:303, Fruit, Avocado, xBrand, , sMarket, 5, $2
304, Fruit, Avocado, xBrand, , sMarket, 6, $1

And if there is multiple matches it will just create a new line and write the next matching line beneath the last matching line even if one value is different.. can anyone help me with this?
Sorry for bumpin' i need this please