-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.py
More file actions
91 lines (77 loc) · 2.78 KB
/
chat.py
File metadata and controls
91 lines (77 loc) · 2.78 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
85
86
87
88
89
90
91
import webbrowser
import time
import wikipediaapi
import pyttsx3
import pyautogui
import speech_recognition as sr
try:
from googlesearch import search
except ImportError:
print("No module named 'google' found")
engine = pyttsx3.init()
engine.setProperty('rate', 150)
engine.setProperty('volume', 0.8)
wiki = wikipediaapi.Wikipedia('en')
class Chatbot:
def __init__(self):
self.commands = {
'open youtube': self.open_youtube,
'open google': self.open_google,
'what is the time': self.get_time,
'exit': self.get_exit,
'previous tab': self.previous_tab,
'next tab': self.next_tab,
}
def process_command(self, command):
response = ''
command = command.lower()
if command in self.commands:
response = self.commands[command]()
return response
elif 'look for' in command:
topic = command.replace('look for', '')
page = wiki.page(topic)
if page.exists():
summary = page.summary[0:500]
return (f"According to Wikipedia: {summary}")
print(f"text: {summary}\n")
else:
return "Sorry, I could not find any matching results in Wikipedia."
elif 'look up' in command:
command1 = command.replace('look up', '')
lookup_flag = False
for j in search(command1):
webbrowser.open(j)
lookup_flag = True
break
if lookup_flag:
return f'Results for {command1} opened in the browser.'
else:
return f'Sorry, I could not find any results for {command1}.'
def open_website(url):
webbrowser.open(url)
def open_youtube(self):
webbrowser.open('https://www.youtube.com')
return "opening youtube"
def open_amazon(self):
webbrowser.open('https://www.amazon.com')
return "opening amazon"
def open_google(self):
webbrowser.open('https://www.google.com')
return "opening google"
def get_time(self):
current_time = time.strftime('%I:%M %p')
print(f'The time is {current_time}')
return (f'The time is {current_time}')
def next_tab(self):
# Send a single hotkey to switch to the previous tab
pyautogui.hotkey('ctrl', 'tab')
return ("switching to next tab")
def previous_tab(self):
# Send a single hotkey to switch to the next tab
pyautogui.hotkey('ctrl', 'shift', 'tab')
return ("switching to previous tab")
def get_exit(self):
engine.say("Goodbye master")
engine.runAndWait()
exit()