From 386659ab1ceea89d12ea039274c705ea6fcedcf4 Mon Sep 17 00:00:00 2001 From: JulienDuf Date: Tue, 3 Jan 2017 03:07:30 -0500 Subject: [PATCH] node-wit: Fix errors and add new tests --- node-wit/index.d.ts | 28 ++++++-------------- node-wit/node-wit-tests.ts | 53 ++++++++++++++++++++++++++++++++++---- 2 files changed, 56 insertions(+), 25 deletions(-) diff --git a/node-wit/index.d.ts b/node-wit/index.d.ts index 511fde0913..33f0bd8cb9 100644 --- a/node-wit/index.d.ts +++ b/node-wit/index.d.ts @@ -3,13 +3,9 @@ // Definitions by: Julien Dufresne // 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; - converse(sessinId: string, message: string, context: WitContext, reset?: boolean): Promise; - runActions(sessinId: string, message: string, context: WitContext, maxSteps?: number): Promise; + converse(sessionId: string, message: string, context: WitContext, reset?: boolean): Promise; + runActions(sessionId: string, message: string, context: WitContext, maxSteps?: number): Promise; } diff --git a/node-wit/node-wit-tests.ts b/node-wit/node-wit-tests.ts index 9bf0f1184e..d71c1d0cad 100644 --- a/node-wit/node-wit-tests.ts +++ b/node-wit/node-wit-tests.ts @@ -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); +}); -}); \ No newline at end of file +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); \ No newline at end of file