Python Forum
Help Python Bot to search a database
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help Python Bot to search a database
#1
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?
Reply
#2
Sorry for bumpin' i need this please
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Database search in python for MongoDB Abdul515001 0 420 Dec-14-2024, 12:31 PM
Last Post: Abdul515001
  Database search Columbo 2 2,682 Jul-15-2019, 03:54 PM
Last Post: Columbo
  Search the data to update in a database chris0147 7 8,385 Oct-27-2016, 03:16 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020