1
+ let mic = document . getElementById ( "mic" ) ;
2
+ let chatareamain = document . querySelector ( '.chatarea-main' ) ;
3
+ let chatareaouter = document . querySelector ( '.chatarea-outer' ) ;
4
+
5
+ let intro = [ "Hello, I am Robo" , "Hi, I am a Robo" , "Hello, My name is Robo" ] ;
6
+ let help = [ "How may i assist you?" , "How can i help you?" , "What i can do for you?" ] ;
7
+ let greetings = [ "i am good you little piece of love" , "i am fine, what about you" , "don't want to talk" , "i am good" ] ;
8
+ let hobbies = [ "i love to talk with humans" , "i like to make friends like you" , "i like cooking" ] ;
9
+ let pizzas = [ "which type of pizza do you like?" , "i can make a pizza for you" , "i would love to make a pizza for you" , "would you like cheese pizza?" ] ;
10
+ let thank = [ "Most welcome" , "Not an issue" , "Its my pleasure" , "Mention not" ] ;
11
+ let closing = [ 'Ok bye-bye' , 'As you wish, bye take-care' , 'Bye-bye, see you soon..' ]
12
+
13
+ const SpeechRecognition = window . SpeechRecognition || window . webkitSpeechRecognition ;
14
+ const recognition = new SpeechRecognition ( ) ;
15
+
16
+ function showusermsg ( usermsg ) {
17
+ let output = '' ;
18
+ output += `<div class="chatarea-inner user">${ usermsg } </div>` ;
19
+ chatareaouter . innerHTML += output ;
20
+ return chatareaouter ;
21
+ }
22
+
23
+ function showchatbotmsg ( chatbotmsg ) {
24
+ let output = '' ;
25
+ output += `<div class="chatarea-inner chatbot">${ chatbotmsg } </div>` ;
26
+ chatareaouter . innerHTML += output ;
27
+ return chatareaouter ;
28
+ }
29
+
30
+ function chatbotvoice ( message ) {
31
+ const speech = new SpeechSynthesisUtterance ( ) ;
32
+ speech . text = "Didn't got you" ;
33
+ if ( message . includes ( 'who are you' ) || ( 'hi' ) ) {
34
+ let finalresult = intro [ Math . floor ( Math . random ( ) * intro . length ) ] ;
35
+ speech . text = finalresult ;
36
+ }
37
+ if ( message . includes ( 'help' ) ) {
38
+ let finalresult = help [ Math . floor ( Math . random ( ) * help . length ) ] ;
39
+ speech . text = finalresult ;
40
+ }
41
+ if ( message . includes ( 'how are you' || 'how are you doing today' ) ) {
42
+ let finalresult = greetings [ Math . floor ( Math . random ( ) * greetings . length ) ] ;
43
+ speech . text = finalresult ;
44
+ }
45
+ if ( message . includes ( 'tell me something about you' || 'tell me something about your hobbies' ) ) {
46
+ let finalresult = hobbies [ Math . floor ( Math . random ( ) * hobbies . length ) ] ;
47
+ speech . text = finalresult ;
48
+ }
49
+ if ( message . includes ( 'food' ) ) {
50
+ let finalresult = pizzas [ Math . floor ( Math . random ( ) * pizzas . length ) ] ;
51
+ speech . text = finalresult ;
52
+ }
53
+ if ( message . includes ( 'thank you' || 'thank you so much' ) ) {
54
+ let finalresult = thank [ Math . floor ( Math . random ( ) * thank . length ) ] ;
55
+ speech . text = finalresult ;
56
+ }
57
+ if ( message . includes ( 'talk to you' || 'talk' ) ) {
58
+ let finalresult = closing [ Math . floor ( Math . random ( ) * closing . length ) ] ;
59
+ speech . text = finalresult ;
60
+ }
61
+ window . speechSynthesis . speak ( speech ) ;
62
+ chatareamain . appendChild ( showchatbotmsg ( speech . text ) ) ;
63
+ }
64
+
65
+ recognition . onresult = function ( e ) {
66
+ let resultIndex = e . resultIndex ;
67
+ let transcript = e . results [ resultIndex ] [ 0 ] . transcript ;
68
+ chatareamain . appendChild ( showusermsg ( transcript ) ) ;
69
+ chatbotvoice ( transcript ) ;
70
+ console . log ( transcript ) ;
71
+ }
72
+ recognition . onend = function ( ) {
73
+ mic . style . background = "#ff3b3b" ;
74
+ }
75
+ mic . addEventListener ( "click" , function ( ) {
76
+ mic . style . background = '#39c81f' ;
77
+ recognition . start ( ) ;
78
+ console . log ( "Activated" ) ;
79
+ } )
0 commit comments