-
Notifications
You must be signed in to change notification settings - Fork 18
/
chatbot.py
328 lines (241 loc) · 7.78 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
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
import random
# from decorator import tick_tock
engine = pyttsx3.init()
def sendmail():
a = smtplib.SMTP('smtp.gmail.com', 587)
a.starttls()
a.login('[email protected]', 'PintooAvyay_11')
a.sendmail('[email protected]', receivers, 'test message')
a.quit()
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 0 <= hour < 12:
wish = 'good morning'
# speech('good morning')
# print('good morning')
elif 12 <= 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', 175)
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()
print(e)
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())))
def coin():
a = random.randint(0, 1)
if a == 0:
speech("the coin says its tail")
print("the coin says its tail")
else:
speech("the coin says its heads")
print("the coin says its heads")
def die():
a = random.randint(0, 5)
if a == 0:
speech("the die says its 1")
print("the die says its 1")
elif a == 1:
speech("the die says its 2")
print("the die says its 2")
elif a == 2:
speech("the die says its 3")
print("the die says its 3")
elif a == 3:
speech("the die says its 4")
print("the die says its 4")
elif a == 4:
speech("the die says its 5")
print("the die says its 5")
else:
speech("the die says its 6")
print("the die says its 6")
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 'send mail' in speechInput:
sendmail()
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 '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 'what is my name' in speechInput:
speech('my name is avyay jain')
# elif 'f*** you' in speechInput:
# speech('bhosdike ek kantap lagaenge sari fuck wak wali backchodi chali jaegi')
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()
elif 'coin' in speechInput:
coin()
elif 'dice' in speechInput:
die()