forked from aichaos/rivescript-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.js
executable file
·146 lines (121 loc) · 3.86 KB
/
shell.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
#!/usr/bin/env node
/******************************************************************************
* Interactive RiveScript Shell for quickly testing your RiveScript bot. *
* *
* Usage: node shell.js /path/to/brain *
******************************************************************************/
var readline = require("readline"),
fs = require("fs"),
RiveScript = require("./lib/rivescript");
//------------------------------------------------------------------------------
// Accept command line parameters.
//------------------------------------------------------------------------------
var opts = {
debug: false,
utf8: false,
watch: false,
brain: undefined
};
process.argv.slice(2).forEach(function(val, index, array) {
if (val === "--debug") {
opts.debug = true;
}
else if (val === "--utf8") {
opts.utf8 = true;
}
else if (val === "--watch") {
opts.watch = true;
}
else if (val.indexOf("-") === 0) {
console.error("Unknown option: %s", val);
}
else if (opts.brain === undefined) {
opts.brain = val;
}
else {
console.error("Extra parameter ignored: %s", val);
}
});
if (opts.brain === undefined) {
console.log("Usage: node shell.js [--debug --utf8 --watch] </path/to/brain>");
process.exit(1);
}
//------------------------------------------------------------------------------
// Initialize the RiveScript bot and print the welcome banner.
//------------------------------------------------------------------------------
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var bot = null;
function loadBot() {
bot = new RiveScript({
debug: opts.debug,
utf8: opts.utf8,
});
bot.ready = false;
bot.loadDirectory(opts.brain, loadingDone, loadingError);
}
loadBot();
if (opts.watch) {
fs.watch(opts.brain, {recursive: false}, function() {
console.log("");
console.log('[INFO] Brain changed, reloading bot.');
rl.prompt();
loadBot();
});
}
//--------------------------------------------------------------------------
// Drop into the interactive command shell.
//--------------------------------------------------------------------------
console.log(" . . ");
console.log(" .:...:: RiveScript Interpreter (JavaScript)");
console.log(" .:: ::. Library Version: v" + bot.version());
console.log(" ..:;;. ' .;;:.. ");
console.log(" . ''' . Type '/quit' to quit.");
console.log(" :;,:,;: Type '/help' for more options.");
console.log(" : : ");
console.log("");
console.log("Using the RiveScript bot found in: " + opts.brain);
console.log("Type a message to the bot and press Return to send it.");
console.log("");
rl.setPrompt("You> ");
rl.prompt();
rl.on('line', function(cmd) {
// Handle commands.
if (cmd === "/help") {
help();
} else if (cmd.indexOf("/data") === 0) {
console.log(bot.getUservars("localuser"));
} else if (cmd.indexOf("/eval ") === 0) {
console.log(eval(cmd.replace("/eval ", "")));
} else if (cmd.indexOf("/log ") === 0) {
console.log(eval(cmd.replace("/log ", "")));
} else if (cmd === "/quit") {
process.exit(0);
} else {
// Get a reply from the bot.
var reply = (bot && bot.ready)
? bot.reply("localuser", cmd)
: "ERR: Bot Not Ready Yet";
console.log("Bot>", reply);
}
rl.prompt();
}).on('close', function() {
console.log("");
process.exit(0);
});
function loadingDone(batchNumber) {
bot.sortReplies();
bot.ready = true;
}
function loadingError(error, batchNumber) {
console.error("Loading error: " + error);
}
function help() {
console.log("Supported commands:");
console.log("/help : Show this text.");
console.log("/eval <code> : Evaluate JavaScript code.");
console.log("/log <code> : Shortcut to /eval console.log(code).");
console.log("/quit : Exit the program.");
}