@@ -20,27 +20,20 @@ var AssistantV1 = require('ibm-watson/assistant/v1');
2020var watsonUtils = require ( './utils' ) ;
2121var deepMerge = require ( 'deepmerge' ) ;
2222
23- var readContext = Promise . promisify ( watsonUtils . readContext ) ;
24- var updateContext = Promise . promisify ( watsonUtils . updateContext ) ;
23+ var readContext = watsonUtils . readContext ;
24+ var updateContext = watsonUtils . updateContext ;
2525var postMessage = Promise . promisify ( watsonUtils . postMessage ) ;
2626
2727// These are initiated by Slack itself and not from the end-user. Won't send these to WCS.
2828var ignoreType = [ 'presence_change' , 'reconnect_url' ] ;
2929
30- module . exports = function ( config ) {
30+ var factory = function ( config ) {
3131
3232 if ( ! config ) {
3333 throw new Error ( 'Watson Assistant config parameters absent.' ) ;
3434 }
3535
36- if ( config . version_date ) {
37- console . warn ( '"version_date" is deprecated and will be removed in the next major release. Use "version"' ) ;
38- config . version = config . version_date ;
39- delete config . version_date ;
40- }
41-
4236 var middleware = {
43-
4437 minimum_confidence : ( config . minimum_confidence || 0.75 ) ,
4538
4639 hear : function ( patterns , message ) {
@@ -66,15 +59,10 @@ module.exports = function (config) {
6659 callback ( null , response ) ;
6760 } ,
6861
69- sendToWatson : function ( bot , message , contextDelta , next ) {
62+ sendToWatsonAsync : function ( bot , message , contextDelta ) {
7063 var before = Promise . promisify ( middleware . before ) ;
7164 var after = Promise . promisify ( middleware . after ) ;
7265
73- if ( ! next && typeof contextDelta === 'function' ) {
74- next = contextDelta ;
75- contextDelta = null ;
76- }
77-
7866 if ( ! middleware . conversation ) {
7967 debug ( 'Creating Assistant object with parameters: ' + JSON . stringify ( config , null , 2 ) ) ;
8068 middleware . conversation = new AssistantV1 ( config ) ;
@@ -87,12 +75,12 @@ module.exports = function (config) {
8775 text : [ ]
8876 }
8977 } ;
90- return next ( ) ;
78+ return Promise . resolve ( ) ;
9179 }
9280
93- middleware . storage = bot . botkit . storage ;
81+ middleware . storage = bot . controller . storage ;
9482
95- readContext ( message . user , middleware . storage ) . then ( function ( userContext ) {
83+ return readContext ( message . user , middleware . storage ) . then ( function ( userContext ) {
9684 var payload = {
9785 workspace_id : config . workspace_id
9886 } ;
@@ -130,52 +118,55 @@ module.exports = function (config) {
130118 } ) . catch ( function ( error ) {
131119 message . watsonError = error ;
132120 debug ( 'Error: %s' , JSON . stringify ( error , Object . getOwnPropertyNames ( error ) , 2 ) ) ;
133- } ) . done ( function ( ) {
134- next ( ) ;
135121 } ) ;
136122 }
137123 } ;
138124
139- middleware . receive = function ( bot , message , next ) {
140- return middleware . sendToWatson ( bot , message , null , next ) ;
125+ middleware . receiveAsync = function ( bot , message ) {
126+ return middleware . sendToWatsonAsync ( bot , message , null ) ;
141127 } ;
142128
143- middleware . interpret = middleware . receive ;
129+ middleware . interpretAsync = middleware . receiveAsync ;
144130
145131
146- middleware . readContext = function ( user , callback ) {
132+ middleware . readContextAsync = function ( user ) {
147133 if ( ! middleware . storage ) {
148- return callback ( new Error ( 'readContext is called before the first middleware.receive call' ) ) ;
134+ throw new Error ( 'readContext is called before the first middleware.receive call' ) ;
149135 }
150- return watsonUtils . readContext ( user , middleware . storage , callback ) ;
136+ return watsonUtils . readContext ( user , middleware . storage ) ;
151137 } ;
152138
153- middleware . updateContext = function ( user , context , callback ) {
139+ middleware . updateContextAsync = function ( user ) {
154140 if ( ! middleware . storage ) {
155- return callback ( new Error ( 'updateContext is called before the first middleware.receive call' ) ) ;
141+ throw new Error ( 'updateContext is called before the first middleware.receive call' ) ;
156142 }
157- watsonUtils . updateContext (
143+ return watsonUtils . updateContext (
158144 user ,
159145 middleware . storage , {
160146 context : context
161- } ,
162- callback
147+ }
163148 ) ;
164149 } ;
165150
166- middleware . interpretAsync = Promise . promisify ( middleware . interpret , {
167- context : middleware
168- } ) ;
169- middleware . sendToWatsonAsync = Promise . promisify ( middleware . sendToWatson , {
170- context : middleware
171- } ) ;
172- middleware . readContextAsync = Promise . promisify ( middleware . readContext , {
173- context : middleware
174- } ) ;
175- middleware . updateContextAsync = Promise . promisify ( middleware . updateContext , {
176- context : middleware
177- } ) ;
151+ middleware . receive = middleware . interpret = function ( bot , message , callback ) {
152+ return watsonUtils . asCallback ( middleware . interpretAsync ( bot , message ) , callback ) ;
153+ } ;
154+ middleware . sendToWatson = function ( bot , message , contextDelta , callback ) {
155+ if ( ! callback && typeof contextDelta === 'function' ) {
156+ callback = contextDelta ;
157+ contextDelta = null ;
158+ }
159+ return watsonUtils . asCallback ( middleware . sendToWatsonAsync ( bot , message , contextDelta ) , callback ) ;
160+ } ;
161+ middleware . readContext = function ( user , callback ) {
162+ return watsonUtils . asCallback ( middleware . readContextAsync ( user ) , callback ) ;
163+ } ;
164+ middleware . updateContext = function ( user , contextDelta , callback ) {
165+ return watsonUtils . asCallback ( middleware . updateContextAsync ( user , contextDelta ) , callback ) ;
166+ } ;
178167
179168 debug ( 'Middleware: ' + JSON . stringify ( middleware , null , 2 ) ) ;
180169 return middleware ;
181170} ;
171+
172+ module . exports = factory ;
0 commit comments