admin管理员组

文章数量:1122846

I'm trying to make a phone directory on Telegram as a bot with Python and here's what I have so far:

import pandas as pd
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, filters, ContextTypes

# Load the phone directory from CSV
phone_directory = pd.read_csv('phone_directory.csv')

async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    await update.message.reply_text('Welcome to the Phone Directory Bot! Send me a name and a zipcode.')

async def get_phone_number(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    user_input = update.message.text
    try:
        name, zipcode = user_input.split(',')
        name = name.strip()
        zipcode = zipcode.strip()
        
        # Search for the phone number in the directory
        result = phone_directory[(phone_directory['name'].str.lower() == name.lower()) & 
                                 (phone_directory['zipcode'] == zipcode)]
        
        if not result.empty:
            phone_number = result.iloc[0]['phone_number']
            await update.message.reply_text(f'The phone number for {name} in zipcode {zipcode} is: {phone_number}')
        else:
            await update.message.reply_text('No matching record found. Please check the name and zipcode.')
    except ValueError:
        await update.message.reply_text('Please send the input in the format: Name, Zipcode')

def main() -> None:
    # Replace 'YOUR_TOKEN' with your actual bot token
    application = ApplicationBuilder().token('YOUR_TOKEN').build()

    application.add_handler(CommandHandler("start", start))
    application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, get_phone_number))

    application.run_polling()

if __name__ == '__main__':
    main()

And I already have the phone_directory.csv like this:

name,zipcode,phone_number
John Doe,12345,555-1234
Jane Smith,67890,555-5678
Alice Johnson,12345,555-8765

The problem is even if I type in the right format "name,zipcode" it won't tell me the phone number. If I type the right format it tells me this:

Screenshot of the bot's reponses

本文标签: pythonScript won39t respond even with correct formatStack Overflow