How do I build a simple chatbot using Python and Flask?
Asked on Oct 15, 2025
Answer
To build a simple chatbot using Python and Flask, you'll need to set up a basic Flask application that handles incoming messages and generates responses. Flask is a lightweight web framework that makes it easy to create web applications, including chatbots.
<!-- BEGIN COPY / PASTE -->
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/chat', methods=['POST'])
def chat():
user_input = request.json.get('message')
# Simple logic to generate a response
if user_input.lower() == "hello":
response = "Hi there! How can I help you today?"
else:
response = "I'm not sure how to respond to that."
return jsonify({'response': response})
if __name__ == '__main__':
app.run(debug=True)
<!-- END COPY / PASTE -->Additional Comment:
- Install Flask using pip with the command:
pip install Flask. - Run the Flask app with
python app.pyand test the chatbot by sending POST requests to the/chatendpoint. - Enhance the chatbot by adding more sophisticated NLP processing or integrating with an external AI service like OpenAI's GPT API.
Recommended Links: