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
版权声明:本文标题:python - Script won't respond even with correct format - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736311121a1934626.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论