-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
591 lines (499 loc) ยท 25.5 KB
/
bot.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
import random
import re
import sqlite3
import data
import bs4
from urllib.request import urlopen as request
from bs4 import BeautifulSoup as soup
import datetime
import operator
from tkinter import *
import time
import tkinter.messagebox
import pyttsx3
import threading
import speech_recognition as sr
from PIL import ImageTk as itk, Image
user_response=['0']
window_size="400x400"
class Bot(Frame):
def __init__(self,master=None):
Frame.__init__(self, master)
self.master = master
# sets default bg for top level windows
self.tl_bg = "#EEEEEE"
self.tl_bg2 = "#EEEEEE" #message box background colour
self.tl_fg = "#000000" #message box foreground colour
self.font = "Verdana 10"
menu = Menu(self.master)
self.master.config(menu=menu, bd=5) #bd is for specifying the width around the menu options
# Menu bar
# File
file = Menu(menu, tearoff=0) #tearoff is used to position the menu options, 1 is default.
menu.add_cascade(label="File", menu=file)
# file.add_command(label="Save Chat Log", command=self.save_chat)
file.add_command(label="Clear Chat", command=self.clear_chat)
# file.add_separator()
file.add_command(label="Exit",command=self.chatexit)
# Options
options = Menu(menu, tearoff=0)
menu.add_cascade(label="Options", menu=options)
# username
# font
font = Menu(options, tearoff=0)
options.add_cascade(label="Font", menu=font)
font.add_command(label="Default",command=self.font_change_default)
font.add_command(label="Times",command=self.font_change_times)
font.add_command(label="System",command=self.font_change_system)
font.add_command(label="Helvetica",command=self.font_change_helvetica)
font.add_command(label="Fixedsys",command=self.font_change_fixedsys)
# color theme
color_theme = Menu(options, tearoff=0)
options.add_cascade(label="Color Theme", menu=color_theme)
color_theme.add_command(label="Default",command=self.color_theme_default)
# color_theme.add_command(label="Night",command=self.)
color_theme.add_command(label="Grey",command=self.color_theme_grey)
color_theme.add_command(label="Blue",command=self.color_theme_dark_blue)
color_theme.add_command(label="Torque",command=self.color_theme_turquoise)
color_theme.add_command(label="Hacker",command=self.color_theme_hacker)
# color_theme.add_command(label='Mkbhd',command=self.MKBHD)
help_option = Menu(menu, tearoff=0)
menu.add_cascade(label="Help", menu=help_option)
#help_option.add_command(label="Features", command=self.features_msg)
help_option.add_command(label="About PyBot", command=self.msg)
help_option.add_command(label="Developers", command=self.about)
self.text_frame = Frame(self.master, bd=6) # the space surrounding the text box
self.text_frame.pack(expand=True, fill=BOTH)
# scrollbar for text box
self.text_box_scrollbar = Scrollbar(self.text_frame, bd=0)
self.text_box_scrollbar.pack(fill=Y, side=RIGHT)
# contains messages
self.text_box = Text(self.text_frame, yscrollcommand=self.text_box_scrollbar.set, state=DISABLED,
bd=1, padx=6, pady=6, spacing3=8, wrap=WORD, bg=None, font="Verdana 10", relief=GROOVE,
width=10, height=1)
self.text_box.pack(expand=True, fill=BOTH)
self.text_box_scrollbar.config(command=self.text_box.yview)
# frame containing user entry field
self.entry_frame = Frame(self.master, bd=1)
self.entry_frame.pack(side=LEFT, fill=BOTH, expand=True)
# entry field
self.entry_field = Entry(self.entry_frame, bd=1, justify=LEFT)
#self.entry_field.place(x=10,y=10,width=1000,height=30)
self.entry_field.pack(fill=X, padx=6, pady=6, ipady=3)
# self.users_message = self.entry_field.get()
image=Image.open('voice.png')
image = image.resize((40, 40), Image.ANTIALIAS)
self.my_img = itk.PhotoImage(image)
self.roundedbutton = Button(self.master, image=self.my_img,command=lambda: self.voice_message_insert(None))
self.roundedbutton["bg"] = "white"
self.roundedbutton["border"] = "0"
self.roundedbutton.place(x=20,y=10)
self.roundedbutton.pack(side="top")
# frame containing send button and emoji button
#self.send_button_frame = Frame(self.master, bd=0)
#self.send_button_frame.pack(fill=BOTH)
# send button
'''self.send_button = Button(self.send_button_frame, text="Send", width=5, relief=GROOVE, bg='white',
bd=1, command=lambda: self.send_message_insert(None), activebackground="#FFFFFF",
activeforeground="#000000")'''
#self.send_button.place(x=10,y=40)
#self.send_button.pack(side=LEFT, ipady=8)
self.master.bind("<Return>", self.send_message_insert)
self.last_sent_label(date="No messages sent.")
#t2 = threading.Thread(target=self.send_message_insert(, name='t1')
#t2.start()
welcome="Zara:Hi I am a chatbot developed for college enquiry system. How may I help you\n"
self.text_box.configure(state=NORMAL)
self.text_box.insert(END, welcome)
self.text_box.configure(state=DISABLED)
self.text_box.see(END)
#text to speech part
def playResponce(self,responce):
x=pyttsx3.init()
#print(responce)
li = []
if len(responce) > 100:
if responce.find('--') == -1:
b = responce.split('--')
#print(b)
x.setProperty('rate',120)
x.setProperty('volume',100)
x.say(responce)
x.runAndWait()
#print("Played Successfully......")
def last_sent_label(self, date):
try:
self.sent_label.destroy()
except AttributeError:
pass
self.sent_label = Label(self.entry_frame, font="Verdana 7", text=date, bg=self.tl_bg2, fg=self.tl_fg)
self.sent_label.pack(side=LEFT, fill=X, padx=3)
def clear_chat(self):
self.text_box.config(state=NORMAL)
self.last_sent_label(date="No messages sent.")
self.text_box.delete(1.0, END)
self.text_box.delete(1.0, END)
self.text_box.config(state=DISABLED)
def chatexit(self):
exit()
def msg(self):
tkinter.messagebox.showinfo("Zara v1.0",'Zara is a chatbot for answering queries related to Pondicherry Engineering College.\nIt is developed with the help of Python.\nGUI is based on Tkinter\nIt is mainly designed to answer questions regarding our college for the first year students')
def about(self):
tkinter.messagebox.showinfo("Zara Developers","T.BAVANI \n 3RD YEAR CSE DEPARTMENT \n PONDICHERRY ENGINEERING COLLEGE")
def font_change_default(self):
self.text_box.config(font="Verdana 10")
self.entry_field.config(font="Verdana 10")
self.font = "Verdana 10"
def font_change_times(self):
self.text_box.config(font="Times")
self.entry_field.config(font="Times")
self.font = "Times"
def font_change_system(self):
self.text_box.config(font="System")
self.entry_field.config(font="System")
self.font = "System"
def font_change_helvetica(self):
self.text_box.config(font="helvetica 10")
self.entry_field.config(font="helvetica 10")
self.font = "helvetica 10"
def font_change_fixedsys(self):
self.text_box.config(font="fixedsys")
self.entry_field.config(font="fixedsys")
self.font = "fixedsys"
def color_theme_default(self):
self.master.config(bg="#EEEEEE")
self.text_frame.config(bg="#EEEEEE")
self.entry_frame.config(bg="#EEEEEE")
self.text_box.config(bg="#FFFFFF", fg="#000000")
self.entry_field.config(bg="#FFFFFF", fg="#000000", insertbackground="#000000")
self.send_button_frame.config(bg="#EEEEEE")
self.send_button.config(bg="#FFFFFF", fg="#000000", activebackground="#FFFFFF", activeforeground="#000000")
#self.emoji_button.config(bg="#FFFFFF", fg="#000000", activebackground="#FFFFFF", activeforeground="#000000")
self.sent_label.config(bg="#EEEEEE", fg="#000000")
self.tl_bg = "#FFFFFF"
self.tl_bg2 = "#EEEEEE"
self.tl_fg = "#000000"
# Dark
def color_theme_dark(self):
self.master.config(bg="#2a2b2d")
self.text_frame.config(bg="#2a2b2d")
self.text_box.config(bg="#212121", fg="#FFFFFF")
self.entry_frame.config(bg="#2a2b2d")
self.entry_field.config(bg="#212121", fg="#FFFFFF", insertbackground="#FFFFFF")
self.send_button_frame.config(bg="#2a2b2d")
self.send_button.config(bg="#212121", fg="#FFFFFF", activebackground="#212121", activeforeground="#FFFFFF")
# self.emoji_button.config(bg="#212121", fg="#FFFFFF", activebackground="#212121", activeforeground="#FFFFFF")
self.sent_label.config(bg="#2a2b2d", fg="#FFFFFF")
self.tl_bg = "#212121"
self.tl_bg2 = "#2a2b2d"
self.tl_fg = "#FFFFFF"
# Grey
def color_theme_grey(self):
self.master.config(bg="#444444")
self.text_frame.config(bg="#444444")
self.text_box.config(bg="#4f4f4f", fg="#ffffff")
self.entry_frame.config(bg="#444444")
self.entry_field.config(bg="#4f4f4f", fg="#ffffff", insertbackground="#ffffff")
self.send_button_frame.config(bg="#444444")
self.send_button.config(bg="#4f4f4f", fg="#ffffff", activebackground="#4f4f4f", activeforeground="#ffffff")
#self.emoji_button.config(bg="#4f4f4f", fg="#ffffff", activebackground="#4f4f4f", activeforeground="#ffffff")
self.sent_label.config(bg="#444444", fg="#ffffff")
self.tl_bg = "#4f4f4f"
self.tl_bg2 = "#444444"
self.tl_fg = "#ffffff"
def color_theme_turquoise(self):
self.master.config(bg="#003333")
self.text_frame.config(bg="#003333")
self.text_box.config(bg="#669999", fg="#FFFFFF")
self.entry_frame.config(bg="#003333")
self.entry_field.config(bg="#669999", fg="#FFFFFF", insertbackground="#FFFFFF")
#self.send_button_frame.config(bg="#003333")
#self.send_button.config(bg="#669999", fg="#FFFFFF", activebackground="#669999", activeforeground="#FFFFFF")
#self.emoji_button.config(bg="#669999", fg="#FFFFFF", activebackground="#669999", activeforeground="#FFFFFF")
self.sent_label.config(bg="#003333", fg="#FFFFFF")
self.tl_bg = "#669999"
self.tl_bg2 = "#003333"
self.tl_fg = "#FFFFFF"
# Blue
def color_theme_dark_blue(self):
self.master.config(bg="#263b54")
self.text_frame.config(bg="#263b54")
self.text_box.config(bg="#1c2e44", fg="#FFFFFF")
self.entry_frame.config(bg="#263b54")
self.entry_field.config(bg="#1c2e44", fg="#FFFFFF", insertbackground="#FFFFFF")
#self.send_button_frame.config(bg="#263b54")
#self.send_button.config(bg="#1c2e44", fg="#FFFFFF", activebackground="#1c2e44", activeforeground="#FFFFFF")
#self.emoji_button.config(bg="#1c2e44", fg="#FFFFFF", activebackground="#1c2e44", activeforeground="#FFFFFF")
self.sent_label.config(bg="#263b54", fg="#FFFFFF")
self.tl_bg = "#1c2e44"
self.tl_bg2 = "#263b54"
self.tl_fg = "#FFFFFF"
# Torque
def color_theme_turquoise(self):
self.master.config(bg="#003333")
self.text_frame.config(bg="#003333")
self.text_box.config(bg="#669999", fg="#FFFFFF")
self.entry_frame.config(bg="#003333")
self.entry_field.config(bg="#669999", fg="#FFFFFF", insertbackground="#FFFFFF")
#self.send_button_frame.config(bg="#003333")
#self.send_button.config(bg="#669999", fg="#FFFFFF", activebackground="#669999", activeforeground="#FFFFFF")
#self.emoji_button.config(bg="#669999", fg="#FFFFFF", activebackground="#669999", activeforeground="#FFFFFF")
self.sent_label.config(bg="#003333", fg="#FFFFFF")
self.tl_bg = "#669999"
self.tl_bg2 = "#003333"
self.tl_fg = "#FFFFFF"
# Hacker
def color_theme_hacker(self):
self.master.config(bg="#0F0F0F")
self.text_frame.config(bg="#0F0F0F")
self.entry_frame.config(bg="#0F0F0F")
self.text_box.config(bg="#0F0F0F", fg="#33FF33")
self.entry_field.config(bg="#0F0F0F", fg="#33FF33", insertbackground="#33FF33")
#self.send_button_frame.config(bg="#0F0F0F")
#self.send_button.config(bg="#0F0F0F", fg="#FFFFFF", activebackground="#0F0F0F", activeforeground="#FFFFFF")
#self.emoji_button.config(bg="#0F0F0F", fg="#FFFFFF", activebackground="#0F0F0F", activeforeground="#FFFFFF")
self.sent_label.config(bg="#0F0F0F", fg="#33FF33")
self.tl_bg = "#0F0F0F"
self.tl_bg2 = "#0F0F0F"
self.tl_fg = "#33FF33"
# Default font and color theme
def default_format(self):
self.font_change_default()
self.color_theme_default()
def mail_id(self,faculty_name):
#faculty_name=input("Bot: Please enter the faculty name as per the record:")
conn = sqlite3.connect("chatbot.db")
cursor = conn.cursor()
cursor.execute("select email from faculty where name =?",(faculty_name,))
mail_id=cursor.fetchone()
#print("Bot:",end=" ")
#print(mail_id[0])
conn.commit()
def news(self):
news_lst=[]
url="http://www.pec.edu/"
client=request(url)
page_html=client.read()
client.close()
page_soup=soup(page_html,"html.parser")
containers=page_soup.findAll("div",{"class":"newsbox"})
for container in containers:
print(container.text)
news_lst.append(container.text)
link=container.find("a")
if(link):
if(link["href"]):
if(" " in link["href"]):
link["href"].replace(" ","%20")
if("https:" in link["href"]):
print(link["href"])
print("\n")
elif("http" in link["href"]):
news_lst.append(link["href"])
#print(link["href"])
#print("\n")
else:
pec_link="http://www.pec.edu/"+link["href"]
news_lst.append(pec_link)
#print(pec_link)
#print("\n")
else:
pass
return news_lst
def ccgc(self):
ccgc_lst=[]
url="http://ccgc.pec.edu/"
client=request(url)
page_html=client.read()
client.close()
page_soup=soup(page_html,"html.parser")
containers=page_soup.findAll("p",{"class":["MsoNormal","MsoNormalCxSpMiddle"]})
del containers[0:7]
print("Bot:",end=" ")
for container in containers:
if containers.index(container)%6!=0:
ccgc_lst.append(container.text)
print(container.text,end=" ")
else:
ccgc_lst.append(container.text)
print("\n")
print(container.text)
return ccgc_lst
def send_message_insert(self, message):
user_input = self.entry_field.get()
pr1 = "User : " + user_input + "\n"
self.text_box.configure(state=NORMAL)
self.text_box.insert(END, pr1)
self.text_box.configure(state=DISABLED)
self.text_box.see(END)
t1 = threading.Thread(target=self.playResponce, args=(user_input,))
t1.start()
time.sleep(1)
#ob=chat(user_input)
ob=self.userresponse(user_input)
pr="Zara : " + ob + "\n"
self.text_box.configure(state=NORMAL)
self.text_box.insert(END, pr)
self.text_box.configure(state=DISABLED)
self.text_box.see(END)
self.last_sent_label(str(time.strftime( "Last message sent: " + '%B %d, %Y' + ' at ' + '%I:%M %p')))
self.entry_field.delete(0,END)
#time.sleep(0)
#t2 = threading.Thread(target=self.playResponce, args=(ob,))
#t2.start()
return ob
def voice_message_insert(self,message):
r=sr.Recognizer()
with sr.Microphone() as source:
print("Speak anything")
r.adjust_for_ambient_noise(source)
audio=r.listen(source)
try:
text=r.recognize_google(audio)
vb=self.userresponse(text)
pr1 = "Human : " + text + "\n"
self.text_box.configure(state=NORMAL)
self.text_box.insert(END, pr1)
self.text_box.configure(state=DISABLED)
self.text_box.see(END)
print('you said:{}'.format(text))
pr="PyBot : " + vb + "\n"
self.text_box.configure(state=NORMAL)
self.text_box.insert(END, pr)
self.text_box.configure(state=DISABLED)
self.text_box.see(END)
self.last_sent_label(str(time.strftime( "Last message sent: " + '%B %d, %Y' + ' at ' + '%I:%M %p')))
self.entry_field.delete(0,END)
'''time.sleep(0)
t2 = threading.Thread(target=self.playResponce, args=(vb,))
t2.start()
return vb'''
except:
print("Sorry could not recognize your voice")
tag_name=" "
def userresponse(self,pr1):
#print("hello")
e_list=['๐','๐','๐','๐','๐ฅ','โก','๐','๐','๐','โจ','๐ฒ','๐','๐','๐','โค๏ธ','๐','๐','๐ป','๐','๐','โฅ๏ธ','โฃ๏ธ','๐','๐ผ','๐','๐ฅฐ','๐','๐','๐ค','๐','๐','๐','๐โโ๏ธ','๐โโ๏ธ','๐งก','๐ค','๐ค','๐','๐ค','๐','๐ค','๐','๐ค ','๐','๐คฉ','๐ค','๐ฅณ','๐','๐','๐ฏ','โ','๐ค','๐ค','๐ค','๐ค','๐','๐','๐ฆพ','๐','๐','๐บ','๐','๐','๐','๐','๐','๐ค','๐คจ','๐','๐','๐ถ','๐','๐','๐','๐ฌ','๐คฅ','๐','๐ช','๐ด','๐ท','๐ค','๐คข','๐คฎ','๐คง','๐ฅด','๐ต','๐คฆ','๐คฆโโ๏ธ','๐คฏ','๐','๐','๐','โน','๐','๐ค','๐ก','๐ ','๐คฌ','๐ฆ','๐','๐']
response=pr1.lower()
if len(response)>1:
res = [ele for ele in e_list if(ele in response)]
if len(res)!=0:
print(res[0])
response=response.replace(res[0],"")
print(response)
flag=True
value=1
#tag_name=""
#response=" "
while(flag==True):
#print(response)
#print("User:", end=" ")
#response = input().lower()
if(response!='bye'):
exp=re.compile("dr.\D+.\D")
match=exp.search(response)
if(response=='thanks' or response=='thank you' ):
flag=False
u_res="You are welcome.."
return u_res
break
elif any(regex in response for regex in ['๐
','๐','๐','โฐ','โ','โฒ','๐ฐ','โ','โ',"today's date","current date","current time","time now","time","date"]):
u_res=str(datetime.datetime.now())
return u_res
break
else:
tag=1
u_res=" "
if re.search("news of pec*",response) or re.search("happenings of pec*",response):
value = 1
new=self.news()
for news in new:
u_res+=news
u_res+='\n'
return u_res
elif match:
faculty_name=match.group()
conn = sqlite3.connect("chatbot.db")
cursor = conn.cursor()
cursor.execute("select name,email,designation,department from faculty3 where name =?",(faculty_name,))
mail_id=cursor.fetchall()
u_res="The faculty details for "+faculty_name+" is:\n"
for faculty in mail_id:
for string in faculty:
u_res+=str(string)
u_res+='\n'
return u_res
break
while value==1:
for intent in data.intents["intents"]:
for entity in intent["entities"]:
tag_result=any(tag_lst in response for tag_lst in ["pec","dsc","women's cell","nss","hostel","first year","cultural club","iis","tnp","ccgc","cse","grreting","bot"])
if tag_result:
pass
else:
response+=" of "
response+=self.tag_name
if re.search("members of ccgc*",response):
ccg=self.ccgc()
for news in ccg:
u_res+=news
u_res+='\n'
return u_res
self.userresponse()
if re.search("email*",response) or re.search("mail id*",response):
match=["pec","dsc","women's cell","nss","hostel","first year","cultural club","iis","tnp","ccgc","cse","grreting","bot"]
if any(x in response for x in match):
pass
else:
faculty_name="Please enter the faculty name as per the record:"
fn=self.entry_field.get()
print(fn)
self.mail_id(fn)
return faculty_name
for pattern in entity["patterns"]:
#print(tag_name,end=" ")
#print(response)
if pattern in response:
user_response.append(random.choice(entity["responses"]))
self.tag_name=intent["tag"]
value=0
continue
else:
continue
if user_response[-1]!='0' :
#print(user_response)
fl=1
if fl==1:
#print(user_response[-1])
u_res=user_response[-1]
#print("Bot:"+ u_res )
value=0
user_response.clear()
user_response.append('0')
return u_res
break
else:
u_res="Sorry!!! I can't understand this query"
#print("Sorry!!! I can't understand this query")
value=0
return u_res
break
break
else:
flag=False
for intent in data.intents["intents"]:
for entity in intent["entities"]:
for pattern in entity["patterns"]:
if pattern in response:
#print("Bot:"+ random.choice(entity["responses"]))
u_res=random.choice(entity["responses"])
value=1
return u_res
break
root=Tk()
a = Bot(root)
#a.userresponse()
root.geometry(window_size)
root.title("PyBot")
#root.iconbitmap('i.ico')
root.mainloop()