Skip to content

Commit af4c6c1

Browse files
authored
Update to use Papaparse lib
Change in Google API broke our use of tabletop.js....
1 parent dec49fb commit af4c6c1

File tree

1 file changed

+57
-39
lines changed

1 file changed

+57
-39
lines changed

src/chatbot.js

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,67 @@ var bot;
55
var chatbot = {
66
db: [],
77
replyDelay: 800,
8-
loadDB: function(link) {
9-
Tabletop.init({
10-
key: link,
11-
callback: data => {
12-
this.db = data["Sheet1"].elements;
13-
console.log("Your Database Rows Loaded: ", this.db.length);
14-
}
15-
});
16-
},
17-
getDB: function(link, sheetName) {
8+
getDB: function (link) {
189
return new Promise((resolve, reject) => {
19-
Tabletop.init({
20-
key: link,
21-
callback: data => {
22-
var sheetNames = Object.keys(data);
23-
var thisdb = [];
24-
if (sheetName)
25-
thisdb = data[sheetName] ? data[sheetName].elements : [];
26-
else
27-
thisdb = data[sheetNames[0]] ? data[sheetNames[0]].elements : [];
28-
console.log("Your Database Rows Loaded: ", thisdb.length);
29-
resolve(thisdb);
10+
var self = this;
11+
Papa.parse(link, {
12+
download: true,
13+
header: true,
14+
complete: function (results) {
15+
resolve(results.data);
3016
}
31-
});
17+
})
3218
});
3319
},
34-
dbFilter: function (db,col,val) {
35-
var filtered = db.filter(function(row) {
20+
// loadDB: function(link) {
21+
// Tabletop.init({
22+
// key: link,
23+
// callback: data => {
24+
// this.db = data["Sheet1"].elements;
25+
// console.log("Your Database Rows Loaded: ", this.db.length);
26+
// }
27+
// });
28+
// },
29+
// getDB: function(link, sheetName) {
30+
// return new Promise((resolve, reject) => {
31+
// Tabletop.init({
32+
// key: link,
33+
// callback: data => {
34+
// var sheetNames = Object.keys(data);
35+
// var thisdb = [];
36+
// if (sheetName)
37+
// thisdb = data[sheetName] ? data[sheetName].elements : [];
38+
// else
39+
// thisdb = data[sheetNames[0]] ? data[sheetNames[0]].elements : [];
40+
// console.log("Your Database Rows Loaded: ", thisdb.length);
41+
// resolve(thisdb);
42+
// }
43+
// });
44+
// });
45+
// },
46+
dbFilter: function (db, col, val) {
47+
var filtered = db.filter(function (row) {
48+
return row[col].toLowerCase().indexOf(val.toLowerCase()) > -1;
49+
});
50+
return filtered;
51+
},
52+
dbFilter2: function (db, col, val) {
53+
var filtered = db.filter(function (row) {
3654
var match = true;
3755
if (!Array.isArray(val)) val = [val];
38-
val.forEach(v=>{
56+
val.forEach(v => {
3957
if (row[col].toLowerCase().indexOf(v.toLowerCase()) == -1)
40-
match = false;
58+
match = false;
4159
})
42-
return match;
60+
return match;
4361
});
4462
return filtered;
4563
},
46-
loadFiles: function(filenames) {
64+
loadFiles: function (filenames) {
4765
bot = new RiveScript();
4866
bot.loadFile(filenames).then(on_load_success).catch(on_load_error);
4967
},
50-
getReply: function(text) {
68+
getReply: function (text) {
5169
bot.reply(null, text).then(
5270
reply => {
5371
reply = reply.replace(/\n/g, "<br>");
@@ -58,16 +76,16 @@ var chatbot = {
5876
}
5977
);
6078
},
61-
postReply: function(reply, delay) {
79+
postReply: function (reply, delay) {
6280
if (!delay) delay = this.replyDelay;
6381
var rand = Math.round(Math.random() * 10000);
64-
setTimeout(function() {
82+
setTimeout(function () {
6583
$("#dialogue").append(
6684
"<div class='bot-row' id='" +
67-
rand +
68-
"'><span class='bot'>" +
69-
reply +
70-
"</span></div>"
85+
rand +
86+
"'><span class='bot'>" +
87+
reply +
88+
"</span></div>"
7189
);
7290
if (typeof pop !== "undefined") pop.play();
7391
if (typeof onChatbotReply === "function") onChatbotReply();
@@ -80,20 +98,20 @@ var chatbot = {
8098
);
8199
}, delay);
82100
},
83-
sendMessage: function() {
101+
sendMessage: function () {
84102
var text = $("#message").val();
85103
if (text.length === 0) return false;
86104
$("#message").val("");
87105
$("#dialogue").append(
88106
"<div class='user-row'><span class='user'>" +
89-
this.escapeHtml(text) +
90-
"</span></div>"
107+
this.escapeHtml(text) +
108+
"</span></div>"
91109
);
92110
$("#dialogue").animate({ scrollTop: $("#dialogue")[0].scrollHeight }, 200);
93111
this.getReply(text);
94112
return false;
95113
},
96-
escapeHtml: function(text) {
114+
escapeHtml: function (text) {
97115
return text
98116
.replace(/&/g, "&amp;")
99117
.replace(/</g, "&lt;")

0 commit comments

Comments
 (0)