forked from aichaos/rivescript-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.coffee
122 lines (101 loc) · 3.55 KB
/
shell.coffee
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
#!/usr/bin/env coffee
################################################################################
# Interactive RiveScript Shell for quickly testing your RiveScript bot. #
# #
# This CoffeeScript version of the shell has support for executing object #
# macros written in CoffeeScript (see eg/brain/coffee.rive). #
################################################################################
readline = require "readline"
fs = require "fs"
RiveScript = require "./src/rivescript"
CoffeeObjectHandler = require "./lib/lang/coffee"
################################################################################
# Accept command line parameters.
################################################################################
opts =
debug: false
utf8: false
watch: false
brain: undefined
process.argv.slice(2).forEach((val, index, array) ->
if val is "--debug"
opts.debug = true
else if val is "--utf8"
opts.utf8 = true
else if val is "--watch"
opts.watch = true
else if val.indexOf("-") is 0
console.error "Unknown option: #{val}"
else if opts.brain is undefined
opts.brain = val
else
console.error "Extra parameter ignored: #{val}"
)
if opts.brain is undefined
console.log "Usage: coffee shell.coffee [--debug --utf8 --watch] </path/to/brain>"
process.exit 1
################################################################################
# Initialize the RiveScript bot and print the welcome banner.
################################################################################
rl = readline.createInterface
input: process.stdin
output: process.stdout
bot = null
loadingDone = (batchNumber) ->
bot.sortReplies()
bot.ready = true
loadingError = (error, batchNumber) ->
console.error "Loading error: #{error}"
loadBot = ->
bot = new RiveScript({
debug: opts.debug
utf8: opts.utf8
})
bot.ready = false
bot.setHandler("coffee", new CoffeeObjectHandler())
bot.loadDirectory(opts.brain, loadingDone, loadingError)
loadBot()
if opts.watch?
fs.watch opts.brain, {recursive: false}, ->
console.log ""
console.log "[INFO] Brain changed, reloading bot."
rl.prompt()
loadBot()
##############################################################################
# Drop into the interactive command shell.
##############################################################################
console.log """
. .
.:...:: RiveScript Interpreter (CoffeeScript)
.:: ::. Library Version: v#{bot.version()}
..:;;. ' .;;:..
. ''' . Type '/quit' to quit.
:;,:,;: Type '/help' for more options.
: :
Using the RiveScript bot found in: #{opts.brain}
Type a message to the bot and press Return to send it.
"""
rl.setPrompt "You> "
rl.prompt()
rl.on "line", (cmd) ->
if cmd is "/help"
help()
else if cmd.indexOf("/eval ") is 0
eval(cmd.replace("/eval ", ""))
else if cmd.indexOf("/log ") is 0
console.log(eval(cmd.replace("/log ", "")))
else if cmd is "/quit"
process.exit 0
else
reply = if (bot and bot.ready) then bot.reply("localuser", cmd) else "ERR: Bot not ready yet"
console.log "Bot> #{reply}"
rl.prompt()
.on "close", () ->
console.log ""
process.exit 0
help = () ->
console.log """Supported commands:
/help : Show this text.
/eval <code> : Evaluate JavaScript code.
/log <code> : Shortcut to /eval console.log(code)
/quit : Exit the program."""