使用Python版Microsoft Bot Framework运行示例报错:405方法不允许
Hey there, let’s break down why you’re hitting that 405 error and walk through how to get your bot up and running properly!
First, Why the 405 Error?
This is actually a common gotcha: Bot Framework samples are built to accept only POST requests (since they’re designed to receive messages from chat platforms or the Bot Emulator). When you type http://localhost:9000 into a browser, you’re sending a GET request—something the bot’s endpoint isn’t configured to handle. That’s why you see the 405 response.
Do You Need to Modify Code or Create a Bot Service?
Let’s tackle both questions:
1. Bot Service Keys (App ID/Password)
Most basic samples (like the Echo Bot) don’t require an Azure Bot Service key to run locally. The only time you need these credentials is if you’re using cloud features like LUIS, QnA Maker, or connecting your bot to channels like Teams/WhatsApp.
If your sample has a config.py or .env file with fields for MICROSOFT_APP_ID and MICROSOFT_APP_PASSWORD, you can leave them blank or comment them out for local testing—no need to create an Azure Bot Service yet.
2. Testing the Bot Correctly
You can’t test a bot directly in a browser. Instead, use the Bot Framework Emulator:
- Install the emulator (it’s free and made for this exact purpose).
- When you open the emulator, enter your bot’s endpoint—this is almost always
http://localhost:9000/api/messages(note the/api/messagespath; most samples don’t expose the root URL for bot traffic). - If you didn’t set up app credentials, just click "Connect"—you’ll be able to chat with your bot right away.
If You Still Run Into Issues
Double-check these code details:
- Open your main app file (usually
app.py) and verify the route configuration. For Flask-based samples, it should look like this:
Make sure@app.route("/api/messages", methods=["POST"]) def messages(): # Bot logic heremethods=["POST"]is explicitly set—if it’s missing or includes GET, you’ll get 405 errors. - Confirm the port number in your console output matches the one you’re trying to access (some samples use port 3978 instead of 9000 by default).
内容的提问来源于stack exchange,提问作者Alejandro Peña

