-
-
Notifications
You must be signed in to change notification settings - Fork 262
/
Copy pathchatbot.py
257 lines (193 loc) · 6.14 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import speech_recognition as sr
import datetime
import webbrowser as wb
import os
import pyttsx3
import wikipedia
import pywhatkit
import pyjokes as pj
import smtplib
import requests
from bs4 import BeautifulSoup
import winsound
import operator
engine = pyttsx3.init()
def search():
speech("enter the topic you want to search on wikipedia ")
query= takeCommand().lower()
wresult = str(wikipedia.search(query, results = 1))
results = wikipedia.summary(wresult, sentences=2)
speech("According to Wikipedia")
print(results)
speech(results)
def wishMe():
hour = int(datetime.datetime.now().hour)
if(hour >= 0 and hour <12 ):
wish = 'good morning'
#speech('good morning')
#print('good morning')
elif(hour >=12 and hour<= 18):
wish = 'good afternoon'
#speech('good afternoon')
#print('good afternoon')
else:
wish = 'good evening'
#speech('good evening')
# print('good evening')
return wish
def Whatsapp():
speech('opening whatsApp')
os.startfile('C:\\Users\\avyay\\AppData\\Local\\WhatsApp\\WhatsApp.exe')
def chrome():
speech('opening chrome')
os.startfile('C:\Program Files\Google\Chrome\Application\chrome.exe')
def speech(audio):
engine.setProperty('rate', 200)
voices = engine.getProperty('voices') #getting details of current voice
#engine.setProperty('voice', voices[1].id) #changing index, changes voices. o for male
engine.setProperty('voice', voices[0].id)
engine.say(audio)
engine.runAndWait()
def dateAndTime():
time = datetime.datetime.now().strftime("%Y\n %B\n %A\n %I %M %S %Z")
print(time)
speech(time)
def google():
wb.open('google.com')
speech("opening google")
def takeCommand():
#It takes microphone input from the user and returns string output
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source,duration=1)
print()
print("Listening...")
print()
#r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing...")
print()
query = r.recognize_google(audio, language='en-in')
print(f"you said: {query}\n")
except Exception as e:
print("Say that again please...")
print()
return "None"
return query
def playmusic():
speech("what would you like to play ")
song = takeCommand()
speech('playing '+song)
pywhatkit.playonyt(song)
def joke():
speech('telling a joke')
a = pj.get_joke(language='en',category='neutral')
speech(a)
print(a)
def searchOnGoogle():
speech('what would you like to search on google')
print('what would you like to search on google')
a= takeCommand()
pywhatkit.search(a)
def weather():
speech("please tell city name ")
city = takeCommand().lower()
print(city)
url = "https://google.com/search?q="+"weather"+city
html = requests.get(url).content
soup = BeautifulSoup(html,'html.parser')
temp = soup.find('div',attrs={'class': 'BNeawe iBp4i AP7Wnd'}).text
time_skyDescription = soup.find('div',attrs={'class': 'BNeawe tAd8D AP7Wnd'}).text
data = time_skyDescription.split('\n')
time = data[0]
sky = data[1]
listdiv = soup.findAll('div',attrs={'class': 'BNeawe s3v9rd AP7Wnd'})
strd = listdiv[5].text
pos = strd.find('wind')
otherData= strd[pos:]
print("Temperature is ",temp)
speech("Temperature is "+temp)
print("time is ",time)
speech("time is "+time)
print("sky description: ",sky)
speech("sky description: "+sky)
print(otherData)
def alarm(Timing):
altime = str(datetime.datetime.now().strptime(Timing,"%I:%M %p"))
altime = altime[11:-3]
print(altime)
Horeal = altime[:2]
Horeal = int(Horeal)
Mireal = altime[3:5]
Mireal = int(Mireal)
print(f"Done, alarm is set for {Timing}")
while True:
if Horeal == datetime.datetime.now().hour and Mireal == datetime.datetime.now().minute:
print("alarm is running")
winsound.PlaySound('abc',winsound.SND_LOOP)
elif Mireal<datetime.datetime.now().minute:
break
def calculate():
r = sr.Recognizer()
with sr.Microphone( )as source:
speech("say what you want to calculate, example 3 plus 3")
print("Listening...")
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
my_string = r.recognize_google(audio)
print(my_string)
def get_operator_fn(op):
return{
'+':operator.add,
'-':operator.sub,
'*':operator.mul,
'divided':operator.__truediv__,
}[op]
def evel_binary(op1,oper,op2):
op1,op2 = int(op1),int(op2)
return get_operator_fn(oper)(op1,op2)
speech("your result is ")
speech(evel_binary(*(my_string.split())))
wish = wishMe()
print(wish)
speech(f"{wish} sir, i am jarvis what would you like to do")
while True:
speechinput= takeCommand().lower()
#print(speechinput)
if 'time' in speechinput :
dateAndTime()
elif 'google' in speechinput:
google()
elif 'play music' in speechinput:
playmusic()
elif 'open whatsapp' in speechinput:
Whatsapp()
elif 'open chrome' in speechinput:
chrome()
elif 'search' in speechinput:#wikipedia
search()
elif 'tell me a joke' in speechinput:
joke()
elif 'exit' in speechinput:
speech("goodbye sir going off duty")
print("goodbye sir going off duty")
exit()
elif 'search on web' in speechinput:
searchOnGoogle()
elif 'what is the weather' in speechinput:
weather()
elif 'what is your name' in speechinput:
speech('my name is jarvis your personal voice assistant')
elif'who made you' in speechinput:
speech('i was made by avyay jain')
elif 'alarm' in speechinput:
speech("say set alarm for 5:30 am ")
print("say set alarm for 5:30 am")
tt = takeCommand()
tt = tt.replace("set alarm to ","")
tt = tt.replace(".","")
tt = tt.upper()
alarm(tt)
elif'calculate' in speechinput:
calculate()