@@ -268,27 +268,24 @@ controller.on('message_received', processWatsonResponse);
268268Events are messages having type different than ` message ` .
269269
270270[ Example] ( https://github.com/howdyai/botkit/blob/master/examples/facebook_bot.js ) of handler:
271+
271272``` js
272- controller .on (' facebook_postback' , function (bot , message ) {
273- bot .reply (message, ' Great Choice!!!! ( ' + message .payload + ' ) ' );
273+ controller .on (' facebook_postback' , async (bot , message ) => {
274+ await bot .reply (message, ` Great Choice. ( ${ message .payload } ) ` );
274275});
275276```
277+
276278Since they usually have no text, events aren't processed by middleware and have no watsonData attribute.
277279If event handler wants to make use of some data from context, it has to read it first.
278280Example:
281+
279282``` js
280- controller .on (' facebook_postback' , (bot , message ) => {
281- watsonMiddleware .readContext (message .user ).
282- then ((context = {}) =>
283- // do something useful here
284- myFunction (context .field1 , context .field2 )
285- .then (result => {
286- const newMessage = clone (message);
287- newMessage .text = ' postback result' ;
288- return watsonMiddleware .sendToWatson (bot, newMessage, { postbackResult: ' success' });
289- })
290- )
291- .catch (console .error );
283+ controller .on (' facebook_postback' , async (bot , message ) => {
284+ const context = watsonMiddleware .readContext (message .user );
285+ // do something useful here
286+ const result = await myFunction (context .field1 , context .field2 );
287+ const newMessage = {... message, text: ' postback result' };
288+ await watsonMiddleware .sendToWatson (bot, newMessage, { postbackResult: ' success' });
292289});
293290```
294291
@@ -316,8 +313,8 @@ Used globally:
316313``` js
317314slackController .changeEars (watsonMiddleware .hear .bind (watsonMiddleware));
318315
319- slackController .hears ([' hello' ], [' direct_message' , ' direct_mention' , ' mention' ], function (bot , message ) {
320- bot .reply (message, message .watsonData .output .text .join (' \n ' ));
316+ slackController .hears ([' hello' ], [' direct_message' , ' direct_mention' , ' mention' ], async (bot , message ) => {
317+ await bot .reply (message, message .watsonData .output .text .join (' \n ' ));
321318 // now do something special related to the hello intent
322319});
323320```
0 commit comments