-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.py
132 lines (101 loc) · 3.23 KB
/
chatbot.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import pyttsx3
import datetime
import pyaudio
import speech_recognition as sr
import wikipedia
import webbrowser
import os
import smtplib
from email.message import EmailMessage
from pytube import YouTube
engine=pyttsx3.init("sapi5")
voices=engine.getProperty("voices")
engine.setProperty("voice",voices[1].id)
def speak(audio):
engine.say(audio)
engine.runAndWait()
def wishMe():
hour=int(datetime.datetime.now().hour)
if hour>=0 and hour<12:
speak("Good Morning")
elif hour>=12 and hour<16:
speak("Good Afternoon")
else:
speak("Good Evening")
speak("I am your assistant, How can I help you?")
def takecommand():
r=sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshhold=1
r.adjust_for_ambient_noise(source)
audio=r.listen(source)
try:
print("Recognizing...")
query=r.recognize_google(audio,language="en-in")
print("You Said : ", query)
except Exception as e:
print(e)
print("Say that again please...")
return "None"
return query
def download():
SAVE_PATH="E:/"
link="https://www.youtube.com/watch?v=_QwMVSg89mo"
yt=YouTube(link)
try:
yt=YouTube(link)
except:
print("Connection Error")
print(yt.title)
st=yt.streams.first()
try:
st.download(SAVE_PATH)
except:
print("Some Error Found")
if __name__=="__main__":
wishMe()
while True:
query=takecommand().lower()
if "wikipedia" in query:
print("Searching Wikipedia...")
speak("Searching wikipedia")
query=query.replace("Wikipedia","")
result=wikipedia.summary(query,sentences=3)
print(result)
speak(result)
elif "open youtube" in query:
speak("Opening YouTube")
webbrowser.open("www.youtube.com")
elif "play music" in query:
music_dir="F:\Best Songs 6"
songs=os.listdir(music_dir)
print(songs)
os.startfile(os.path.join(music_dir, songs[0]))
elif "the time" in query:
strftime=datetime.datetime.now().strftime("%H:%M:%S")
print(f"The time is {strftime}")
speak(f"The time is {strftime}")
elif "open word file" in query:
text = "E:\jarvis chat bot.docx"
os.startfile(text)
elif "email" in query:
sender = '[email protected]'
receivers = ['[email protected]']
message = f"""\
Subject: Hi Mailtrap
This is my first message with Python."""
try:
server = smtplib.SMTP('localhost')
server.sendmail(sender, receivers, message)
print("Successfully sent email")
except Exception:
print("Error: unable to send email")
elif "download youtube video" in query:
speak("downloading youtube video")
download()
print("Task Completed !!")
speak("Task Completed !!")
elif "exit" in query:
break
speak("Thanks for Using, Have a nice day !!")