-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.py
More file actions
84 lines (73 loc) · 3.03 KB
/
commands.py
File metadata and controls
84 lines (73 loc) · 3.03 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import speech_rec
from datetime import date, datetime
import api_config
import requests
import sys
import pycountry
from flask import Flask, jsonify, render_template
app = Flask(__name__)
sys.stdout.reconfigure(encoding='utf-8')
today = date.today()
time = datetime.now()
str_today = today.strftime("%Y-%m-%d")
str_time = time.strftime("%I:%M%p")
# Basic commands
commands = ["hello", "what's the date", "what time is it", "what's the news", "goodbye"]
# Basic responses
responses = {
"hello": "Hi! How can I help you?",
"what's the date": "Today is " + str_today,
"what time is it": "It is " + str_time,
"what's the news": "ask again and include a country",
"goodbye": "see you later!"
}
@app.route('/')
def index():
return render_template('index.html')
conversation_history = []
@app.route('/listen', methods=['POST'])
def listen():
# Start voice recognition when the page loads
command = speech_rec.voice_recognition()
response = process_command(command)
conversation_history.append({"user": command, "chatbot": response})
return jsonify({"user": command, "chatbot": response})
def process_command(command):
for cmds in commands:
if cmds == command:
return responses[cmds]
if command and command.startswith("what's the news for"):
words = command.split()
if len(words) >= 5:
country_name = ' '.join(words[4:])
try:
country = pycountry.countries.search_fuzzy(country_name)
if country:
country_code = country[0].alpha_2
except LookupError:
print(f"chatbot: Could not find the country code for '{country}'")
url = f"http://api.mediastack.com/v1/news?access_key={api_config.api_key}&countries={country_code}"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
if "data" in data:
news_articles = data["data"]
if news_articles:
news_info = news_info = "<ul>" # Start an unordered list
for article in news_articles:
title = article.get("title", "No Title")
source = article.get("source", "Unknown Source")
url = article.get("url", "No URL")
news_info += f"<li><a href='{url}' target='_blank'>{title}</a> from {source}</li>"
news_info += "</ul>"
return "Here's the latest news for " + country_name + ":" + news_info
else:
return "No news articles found."
else:
return "'data' field not found in the API response."
else:
return f"API request failed with status code {response.status_code}"
else:
return "Command not recognized. Please try again."
if __name__ == '__main__':
app.run(debug=True, port=8001)