forked from wit-ai/node-wit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractive.js
38 lines (35 loc) · 839 Bytes
/
interactive.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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*/
'use strict';
const {DEFAULT_MAX_STEPS} = require('./config');
const logger = require('./log.js');
const readline = require('readline');
module.exports = (wit, handleMessage, context) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.setPrompt('> ');
const prompt = () => {
rl.prompt();
rl.write(null, {ctrl: true, name: 'e'});
};
prompt();
rl.on('line', (line) => {
line = line.trim();
if (!line) {
return prompt();
}
wit.message(line, context)
.then((rsp) => {
if (handleMessage) {
handleMessage(rsp);
} else {
console.log(JSON.stringify(rsp));
}
prompt();
})
.catch(err => console.error(err))
});
};