Skip to content

Commit 714fb6c

Browse files
committed
Node.js SDK v3.0.0
0 parents  commit 714fb6c

File tree

8 files changed

+519
-0
lines changed

8 files changed

+519
-0
lines changed

LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Copyright (c) 2016, Wit.ai, Inc. All rights reserved.
3+
*
4+
* You are hereby granted a non-exclusive, worldwide, royalty-free license to
5+
* use, copy, modify, and distribute this software in source code or binary
6+
* form for use in connection with the web services and APIs provided by
7+
* Wit.ai.
8+
*
9+
* As with any software that integrates with the Wit.ai platform, your use
10+
* of this software is subject to the Wit.ai Terms of Service
11+
* [https://wit.ai/terms]. This copyright notice shall be included in all
12+
* copies or substantial portions of the software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20+
* DEALINGS IN THE SOFTWARE.
21+
*
22+
*/

README.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Wit Node.js SDK
2+
3+
`node-wit` is the Node.js SDK for [Wit.ai](https://wit.ai).
4+
5+
## Install
6+
7+
In your Node.js project, run:
8+
9+
```bash
10+
npm install --save node-wit
11+
```
12+
13+
## Quickstart
14+
15+
```nodejs
16+
'use strict';
17+
const Wit = require('node-wit').Wit;
18+
19+
const actions = {
20+
say: (sessionId, msg, cb) => {
21+
console.log(msg);
22+
cb();
23+
},
24+
merge: (context, entities, cb) => {
25+
cb(context);
26+
},
27+
error: (sessionId, msg) => {
28+
console.log('Oops, I don\'t know what to do.');
29+
},
30+
'my-action': (context, cb) => {
31+
context['name'] = 'Julien';
32+
cb(context);
33+
},
34+
};
35+
36+
const client = new Wit('YOUR_TOKEN', actions);
37+
client.interactive();
38+
```
39+
40+
See `examples` folder for more examples.
41+
42+
## API
43+
44+
The Wit module provides a Wit class with the following methods:
45+
* `message` - the Wit message API
46+
* `converse` - the low-level Wit converse API
47+
* `runActions` - a higher-level method to the Wit converse API
48+
* `interactive` - starts an interactive conversation with your bot
49+
50+
See the [docs](https://wit.ai/docs) for more information.

examples/joke.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict';
2+
3+
const Wit = require('node-wit').Wit;
4+
5+
const token = (() => {
6+
if (process.argv.length !== 3) {
7+
console.log('usage: node examples/joke.js <wit-token>');
8+
process.exit(1);
9+
}
10+
return process.argv[2];
11+
})();
12+
13+
const allJokes = {
14+
chuck: [
15+
'Chuck Norris counted to infinity - twice.',
16+
'Death once had a near-Chuck Norris experience.',
17+
],
18+
tech: [
19+
'Did you hear about the two antennas that got married? The ceremony was long and boring, but the reception was great!',
20+
'Why do geeks mistake Halloween and Christmas? Because Oct 31 === Dec 25.',
21+
],
22+
default: [
23+
'Why was the Math book sad? Because it had so many problems.',
24+
],
25+
};
26+
27+
const firstEntityValue = (entities, entity) => {
28+
const val = entities && entities[entity] &&
29+
Array.isArray(entities[entity]) &&
30+
entities[entity].length > 0 &&
31+
entities[entity][0].value
32+
;
33+
if (!val) {
34+
return null;
35+
}
36+
return typeof val === 'object' ? val.value : val;
37+
};
38+
39+
const actions = {
40+
say: (sessionId, msg, cb) => {
41+
console.log(msg);
42+
cb();
43+
},
44+
merge: (context, entities, cb) => {
45+
delete context.joke;
46+
const category = firstEntityValue(entities, 'category');
47+
if (category) {
48+
context.cat = category;
49+
}
50+
const sentiment = firstEntityValue(entities, 'sentiment');
51+
if (sentiment) {
52+
context.ack = sentiment === 'positive' ? 'Glad you liked it.' : 'Hmm.';
53+
} else {
54+
delete context.ack;
55+
}
56+
cb(context);
57+
},
58+
error: (sessionId, msg) => {
59+
console.log('Oops, I don\'t know what to do.');
60+
},
61+
'select-joke': (context, cb) => {
62+
const jokes = allJokes[context.cat || 'default'];
63+
context.joke = jokes[Math.floor(Math.random() * jokes.length)];
64+
cb(context);
65+
},
66+
};
67+
68+
const client = new Wit(token, actions);
69+
client.interactive();

examples/weather.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
const Wit = require('node-wit').Wit;
4+
5+
const token = (() => {
6+
if (process.argv.length !== 3) {
7+
console.log('usage: node examples/weather.js <wit-token>');
8+
process.exit(1);
9+
}
10+
return process.argv[2];
11+
})();
12+
13+
const actions = {
14+
say: (sessionId, msg, cb) => {
15+
console.log(msg);
16+
cb();
17+
},
18+
merge: (context, entities, cb) => {
19+
cb(context);
20+
},
21+
error: (sessionId, msg) => {
22+
console.log('Oops, I don\'t know what to do.');
23+
},
24+
'fetch-forecast': (context, cb) => {
25+
// Here should go the api call, e.g.:
26+
// context.forecast = apiCall(context.location)
27+
context.forecast = 'cloudy';
28+
cb(context);
29+
},
30+
};
31+
32+
const client = new Wit(token, actions);
33+
client.interactive();

index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
Logger: require('./lib/logger.js').Logger,
3+
logLevels: require('./lib/logger.js').logLevels,
4+
Wit: require('./lib/wit.js').Wit,
5+
}

lib/logger.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
const LEVELS = {
4+
DEBUG: 0,
5+
LOG: 1,
6+
WARN: 2,
7+
ERROR: 3,
8+
};
9+
10+
const log = (message, label) => {
11+
console.log(
12+
label ? '[' + label + '] ' + message : message
13+
);
14+
}
15+
16+
const Logger = function(lvl) {
17+
this.level = lvl === undefined ? LEVELS.LOG : lvl;
18+
19+
this.debug = (message) => {
20+
if (LEVELS.DEBUG >= this.level) {
21+
log(message, 'debug');
22+
}
23+
};
24+
25+
this.log = (message) => {
26+
if (LEVELS.LOG >= this.level) {
27+
log(message);
28+
}
29+
};
30+
31+
this.warn = (message) => {
32+
if (LEVELS.WARN >= this.level) {
33+
log(message, 'warn');
34+
}
35+
};
36+
37+
this.error = (message) => {
38+
if (LEVELS.ERROR >= this.level) {
39+
log(message, 'error');
40+
}
41+
};
42+
};
43+
44+
module.exports = {
45+
Logger: Logger,
46+
logLevels: LEVELS,
47+
};

0 commit comments

Comments
 (0)