-
Notifications
You must be signed in to change notification settings - Fork 482
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 714fb6c
Showing
8 changed files
with
519 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Copyright (c) 2016, Wit.ai, Inc. All rights reserved. | ||
* | ||
* You are hereby granted a non-exclusive, worldwide, royalty-free license to | ||
* use, copy, modify, and distribute this software in source code or binary | ||
* form for use in connection with the web services and APIs provided by | ||
* Wit.ai. | ||
* | ||
* As with any software that integrates with the Wit.ai platform, your use | ||
* of this software is subject to the Wit.ai Terms of Service | ||
* [https://wit.ai/terms]. This copyright notice shall be included in all | ||
* copies or substantial portions of the software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
* DEALINGS IN THE SOFTWARE. | ||
* | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Wit Node.js SDK | ||
|
||
`node-wit` is the Node.js SDK for [Wit.ai](https://wit.ai). | ||
|
||
## Install | ||
|
||
In your Node.js project, run: | ||
|
||
```bash | ||
npm install --save node-wit | ||
``` | ||
|
||
## Quickstart | ||
|
||
```nodejs | ||
'use strict'; | ||
const Wit = require('node-wit').Wit; | ||
const actions = { | ||
say: (sessionId, msg, cb) => { | ||
console.log(msg); | ||
cb(); | ||
}, | ||
merge: (context, entities, cb) => { | ||
cb(context); | ||
}, | ||
error: (sessionId, msg) => { | ||
console.log('Oops, I don\'t know what to do.'); | ||
}, | ||
'my-action': (context, cb) => { | ||
context['name'] = 'Julien'; | ||
cb(context); | ||
}, | ||
}; | ||
const client = new Wit('YOUR_TOKEN', actions); | ||
client.interactive(); | ||
``` | ||
|
||
See `examples` folder for more examples. | ||
|
||
## API | ||
|
||
The Wit module provides a Wit class with the following methods: | ||
* `message` - the Wit message API | ||
* `converse` - the low-level Wit converse API | ||
* `runActions` - a higher-level method to the Wit converse API | ||
* `interactive` - starts an interactive conversation with your bot | ||
|
||
See the [docs](https://wit.ai/docs) for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
'use strict'; | ||
|
||
const Wit = require('node-wit').Wit; | ||
|
||
const token = (() => { | ||
if (process.argv.length !== 3) { | ||
console.log('usage: node examples/joke.js <wit-token>'); | ||
process.exit(1); | ||
} | ||
return process.argv[2]; | ||
})(); | ||
|
||
const allJokes = { | ||
chuck: [ | ||
'Chuck Norris counted to infinity - twice.', | ||
'Death once had a near-Chuck Norris experience.', | ||
], | ||
tech: [ | ||
'Did you hear about the two antennas that got married? The ceremony was long and boring, but the reception was great!', | ||
'Why do geeks mistake Halloween and Christmas? Because Oct 31 === Dec 25.', | ||
], | ||
default: [ | ||
'Why was the Math book sad? Because it had so many problems.', | ||
], | ||
}; | ||
|
||
const firstEntityValue = (entities, entity) => { | ||
const val = entities && entities[entity] && | ||
Array.isArray(entities[entity]) && | ||
entities[entity].length > 0 && | ||
entities[entity][0].value | ||
; | ||
if (!val) { | ||
return null; | ||
} | ||
return typeof val === 'object' ? val.value : val; | ||
}; | ||
|
||
const actions = { | ||
say: (sessionId, msg, cb) => { | ||
console.log(msg); | ||
cb(); | ||
}, | ||
merge: (context, entities, cb) => { | ||
delete context.joke; | ||
const category = firstEntityValue(entities, 'category'); | ||
if (category) { | ||
context.cat = category; | ||
} | ||
const sentiment = firstEntityValue(entities, 'sentiment'); | ||
if (sentiment) { | ||
context.ack = sentiment === 'positive' ? 'Glad you liked it.' : 'Hmm.'; | ||
} else { | ||
delete context.ack; | ||
} | ||
cb(context); | ||
}, | ||
error: (sessionId, msg) => { | ||
console.log('Oops, I don\'t know what to do.'); | ||
}, | ||
'select-joke': (context, cb) => { | ||
const jokes = allJokes[context.cat || 'default']; | ||
context.joke = jokes[Math.floor(Math.random() * jokes.length)]; | ||
cb(context); | ||
}, | ||
}; | ||
|
||
const client = new Wit(token, actions); | ||
client.interactive(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
const Wit = require('node-wit').Wit; | ||
|
||
const token = (() => { | ||
if (process.argv.length !== 3) { | ||
console.log('usage: node examples/weather.js <wit-token>'); | ||
process.exit(1); | ||
} | ||
return process.argv[2]; | ||
})(); | ||
|
||
const actions = { | ||
say: (sessionId, msg, cb) => { | ||
console.log(msg); | ||
cb(); | ||
}, | ||
merge: (context, entities, cb) => { | ||
cb(context); | ||
}, | ||
error: (sessionId, msg) => { | ||
console.log('Oops, I don\'t know what to do.'); | ||
}, | ||
'fetch-forecast': (context, cb) => { | ||
// Here should go the api call, e.g.: | ||
// context.forecast = apiCall(context.location) | ||
context.forecast = 'cloudy'; | ||
cb(context); | ||
}, | ||
}; | ||
|
||
const client = new Wit(token, actions); | ||
client.interactive(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
Logger: require('./lib/logger.js').Logger, | ||
logLevels: require('./lib/logger.js').logLevels, | ||
Wit: require('./lib/wit.js').Wit, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict'; | ||
|
||
const LEVELS = { | ||
DEBUG: 0, | ||
LOG: 1, | ||
WARN: 2, | ||
ERROR: 3, | ||
}; | ||
|
||
const log = (message, label) => { | ||
console.log( | ||
label ? '[' + label + '] ' + message : message | ||
); | ||
} | ||
|
||
const Logger = function(lvl) { | ||
this.level = lvl === undefined ? LEVELS.LOG : lvl; | ||
|
||
this.debug = (message) => { | ||
if (LEVELS.DEBUG >= this.level) { | ||
log(message, 'debug'); | ||
} | ||
}; | ||
|
||
this.log = (message) => { | ||
if (LEVELS.LOG >= this.level) { | ||
log(message); | ||
} | ||
}; | ||
|
||
this.warn = (message) => { | ||
if (LEVELS.WARN >= this.level) { | ||
log(message, 'warn'); | ||
} | ||
}; | ||
|
||
this.error = (message) => { | ||
if (LEVELS.ERROR >= this.level) { | ||
log(message, 'error'); | ||
} | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
Logger: Logger, | ||
logLevels: LEVELS, | ||
}; |
Oops, something went wrong.