-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
57 lines (48 loc) · 2.17 KB
/
Copy pathapp.py
File metadata and controls
57 lines (48 loc) · 2.17 KB
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
from flask import Flask, request, jsonify
from flask_cors import CORS
# Initialize Flask app
app = Flask(__name__)
# Enable CORS for the frontend running on a different port/location
CORS(app)
# --- SIMULATED EXTERNAL API FUNCTION ---
def get_weather_data(city):
"""Simulates calling a real external weather API."""
city = city.lower()
if "london" in city:
return f"The current temperature in London is 12°C with light rain."
elif "tokyo" in city:
return f"Tokyo is sunny with a temperature of 25°C."
else:
return None
# --- CHATBOT API ENDPOINT ---
@app.route('/api/chat', methods=['POST'])
def chat():
"""Receives user message and provides a response."""
data = request.get_json()
user_message = data.get('message', '').lower().strip()
response_text = ""
# 1. Intent Recognition and API Integration Logic
if "weather" in user_message:
# Extract city (simple logic)
city = next((word for word in user_message.split() if word in ["london", "tokyo", "mumbai"]), None)
if city:
# Call the simulated external API
api_response = get_weather_data(city)
if api_response:
response_text = api_response
else:
response_text = "Sorry, I don't have weather data for that city."
else:
response_text = "Which city's weather are you asking about?"
# 2. General Responses (Fallback)
elif "hello" in user_message or "hi" in user_message:
response_text = "Hello! I am your API-integrated chatbot. How can I assist you today?"
elif "help" in user_message:
response_text = "I can answer general questions and fetch weather data for major cities like London or Tokyo. Try asking: 'What is the weather in London?'"
else:
response_text = "I'm not sure how to respond to that. Try asking about the weather or asking for 'help'."
return jsonify({"response": response_text})
# --- RUN SERVER ---
if __name__ == '__main__':
# Runs the server on port 5000 (common Flask port)
app.run(debug=True, port=5000)