node-wit: Fix errors and add new tests

This commit is contained in:
JulienDuf
2017-01-03 03:07:30 -05:00
parent b1e4a08ea0
commit 386659ab1c
2 changed files with 56 additions and 25 deletions

28
node-wit/index.d.ts vendored
View File

@@ -3,13 +3,9 @@
// Definitions by: Julien Dufresne <https://github.com/julienduf>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import * as Promise from "bluebird";
export namespace log {
class Logger {
constructor(lvl: string);
constructor(level: string);
}
const DEBUG: string;
@@ -19,19 +15,16 @@ export namespace log {
}
export interface WitEntityValue {
value?: string;
expressions?: string[];
}
export interface WitEntity {
id?: string;
values?: WitEntityValue[];
}
export interface WitContext {
state?: string[];
reference_time?: string;
timezone?: string;
@@ -40,8 +33,7 @@ export interface WitContext {
}
export interface WitRequest {
sessinId?: string;
sessionId?: string;
context?: WitContext;
text?: string;
entities?: WitEntity[];
@@ -49,33 +41,29 @@ export interface WitRequest {
export interface WitResponse {
text?: string;
qickReplies?: any;
quickReplies?: any;
}
export interface WitOption {
accessToken?: string;
accessToken: string;
actions?: any;
logger?: log.Logger;
}
export interface MessageResponseEntity {
confidence?: number;
value?: string;
confidence: number;
value: string;
}
export interface MessageResponse {
msg_id: string;
_text: string;
entities: MessageResponseEntity[];
}
export declare class Wit {
constructor(option: WitOption);
message(message: string, context: WitContext): Promise<MessageResponse>;
converse(sessinId: string, message: string, context: WitContext, reset?: boolean): Promise<MessageResponse>;
runActions(sessinId: string, message: string, context: WitContext, maxSteps?: number): Promise<WitContext>;
converse(sessionId: string, message: string, context: WitContext, reset?: boolean): Promise<MessageResponse>;
runActions(sessionId: string, message: string, context: WitContext, maxSteps?: number): Promise<WitContext>;
}

View File

@@ -1,14 +1,57 @@
import { Wit, log } from "node-wit";
import { Wit, log, WitRequest, WitResponse, WitContext } from "node-wit";
var wit = new Wit({accessToken: "", logger: new log.Logger(log.DEBUG)});
wit.message("Hello", {}).then((res) => {
var wit = new Wit({
accessToken: "",
actions: {
send(request: WitRequest, response: WitResponse) {
return new Promise(function(resolve, reject) {
console.log(response.text);
console.log(request.entities);
return resolve();
});
},
myAction(request: WitRequest) {
console.log(`Session ${request.sessionId} received ${request.text}`);
console.log(`The current context is ${JSON.stringify(request.context)}`);
console.log(`Wit extracted ${JSON.stringify(request.entities)}`);
return Promise.resolve(request.context);
}
},
logger: new log.Logger(log.DEBUG)
});
wit.message("what is the weather in London?", {}).then((res) => {
console.log(res._text);
for (let entity of res.entities) {
console.log(entity.value + ": " + entity.confidence * 100 + "%");
}
}).catch((err) => {
console.log(err);
});
});
var sessionId = 'my-user-session-42';
var context0: WitContext = {};
wit.runActions(sessionId, 'what is the weather in London?', context0)
.then((context1) => {
console.log('The session state is now: ' + JSON.stringify(context1));
return wit.runActions(sessionId, 'and in Brussels?', context1);
})
.then((context2) => {
console.log(context2.entities + ", ");
console.log(context2.location + ", ");
console.log(context2.reference_time + ", ");
console.log(context2.state + ", ");
console.log(context2.timezone);
})
.catch((e) => {
console.log('Oops! Got an error: ' + e);
});
wit.converse('my-user-session-42', 'what is the weather in London?', {})
.then((data) => {
console.log('Yay, got Wit.ai response: ' + JSON.stringify(data));
console.log(data.entities);
})
.catch(console.error);