-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoBotChat.js
168 lines (125 loc) · 4.29 KB
/
AutoBotChat.js
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
/* =================================================================================
AUTO BOT CHAT
================================================================================= */
var AutoBotChat = (function() {
this.bot = {
min : 1,
max : 4
}
this.getMessage = function(message = 'init', whichBot = '') {
var replies = this.findInDictionary(message);
whichBot = this.checkBot(whichBot);
if(replies) {
if(whichBot == 'ALL' || (whichBot >= this.bot.min && whichBot <= this.bot.max)){
return this.replyBot(replies, whichBot);
} else {
return this.noReplyFound(message, whichBot);
}
}
else {
return this.noReplyFound(message, whichBot);
}
}
this.findInDictionary = function(message) {
// Filter the message. Remove all special character
message = message.toLowerCase().replace(/[^\w\s]/gi, '');
var replies;
// Find in bot dictionary
for (var i in dictionary) {
// If there is matching input string get the corresponding replies.
if ($.inArray(message, dictionary[i].inputString) >= 0) {
replies = dictionary[i].responseString;
}
}
// Retrun the replies
return replies;
}
this.replyBot = function(replies, whichBot) {
// We have now valid bots
if (whichBot >= 1 && whichBot <= 4) {
switch (whichBot) {
case '1':
return {
'Bot_1': replies[whichBot - 1]
};
break;
case '2':
return {
'Bot_2': replies[whichBot - 1]
};
break;
case '3':
return {
'Bot_3': replies[whichBot - 1]
};
break;
case '4':
return {
'Bot_4': replies[whichBot - 1]
};
break;
}
}
// User has not specified bot. So everyone replys
else if(whichBot == 'ALL'){
return {
'Bot_1': replies[0],
'Bot_2': replies[1],
'Bot_3': replies[2],
'Bot_4': replies[3]
}
}
}
this.noReplyFound = function(message, whichBot) {
if(whichBot != '' && (whichBot >= 1 && whichBot <= 4)){
switch (whichBot) {
case '1':
return {
'Bot_1': 'I did not get the message.'
};
break;
case '2':
return {
'Bot_2': 'Sorry what? Could you be more specific?'
};
break;
case '3':
return {
'Bot_3': message + ' What ? '
};
break;
case '4':
return {
'Bot_4': 'I\'m not good enough to answer that.'
};
break;
}
}
else {
return {
'Bot_1': 'I did not get the message.',
'Bot_2': message + ' What ?',
'Bot_3': 'Sorry what? Could you be more specific?',
'Bot_4': 'I\'m not good enough to answer that.'
}
}
}
this.checkBot = function(whichBot) {
if(whichBot.toLowerCase().indexOf('bot') == -1 && whichBot != '') {
return false; // not found
}
var temp = whichBot.replace(/[^0-9/-]/g, '');
if(temp >= this.bot.min && temp <= this.bot.max) {
return temp; // found and return bot number
}
if(temp <= 0 && temp != ''){
return false; // not found
}
if(temp == '-' || temp > this.bot.max){
return false; // not found
}
if(temp == ''){
return 'ALL'; // found
}
}
});