Nov-04-2024, 12:44 AM
(This post was last modified: Nov-04-2024, 10:59 PM by stream3366.)
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:
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:
so if a user sends the bot a message like this:
The bot should respond with:
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?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
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 ) |
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?