@@ -11,13 +11,16 @@ import * as https from 'https';
11
11
import * as oauth from './oauth' ;
12
12
import * as ssl from './ssl' ;
13
13
import debug from 'debug' ;
14
+ import Promise from 'bluebird' ;
14
15
15
16
// Debug log
16
17
const log = debug ( 'watsonwork-echo-app' ) ;
17
18
19
+ const post = Promise . promisify ( request . post ) ;
20
+
18
21
// Echoes Watson Work chat messages containing 'hello' or 'hey' back
19
22
// to the space they were sent to
20
- export const echo = ( appId , token ) => ( req , res ) => {
23
+ export const echo = ( appId , token ) => async ( req , res ) => {
21
24
// Respond to the Webhook right away, as the response message will
22
25
// be sent asynchronously
23
26
res . status ( 201 ) . end ( ) ;
@@ -38,52 +41,49 @@ export const echo = (appId, token) => (req, res) => {
38
41
. filter ( ( word ) => / ^ ( h e l l o | h e y ) $ / i. test ( word ) ) . length )
39
42
40
43
// Send the echo message
41
- send ( req . body . spaceId ,
44
+ await send ( req . body . spaceId ,
42
45
util . format (
43
46
'Hey %s, did you say %s?' ,
44
47
req . body . userName , req . body . content ) ,
45
- token ( ) ,
46
- ( err , res ) => {
47
- if ( ! err )
48
- log ( 'Sent message to space %s' , req . body . spaceId ) ;
49
- } ) ;
48
+ token ( ) ) ;
49
+ log ( 'Sent message to space %s' , req . body . spaceId ) ;
50
50
} ;
51
51
52
52
// Send an app message to the conversation in a space
53
- const send = ( spaceId , text , tok , cb ) => {
54
- request . post (
55
- 'https://api.watsonwork.ibm.com/v1/spaces/' + spaceId + '/messages' , {
56
- headers : {
57
- Authorization : 'Bearer ' + tok
58
- } ,
59
- json : true ,
60
- // An App message can specify a color, a title, markdown text and
61
- // an 'actor' useful to show where the message is coming from
62
- body : {
63
- type : 'appMessage' ,
64
- version : 1.0 ,
65
- annotations : [ {
66
- type : 'generic' ,
53
+ const send = async ( spaceId , text , tok ) => {
54
+ let res ;
55
+ try {
56
+ res = await post (
57
+ 'https://api.watsonwork.ibm.com/v1/spaces/' + spaceId + '/messages' , {
58
+ headers : {
59
+ Authorization : 'Bearer ' + tok
60
+ } ,
61
+ json : true ,
62
+ // An App message can specify a color, a title, markdown text and
63
+ // an 'actor' useful to show where the message is coming from
64
+ body : {
65
+ type : 'appMessage' ,
67
66
version : 1.0 ,
68
-
69
- color : '#6CB7FB' ,
70
- title : 'Echo message' ,
71
- text : text ,
72
-
73
- actor : {
74
- name : 'Sample echo app'
75
- }
76
- } ]
77
- }
78
- } , ( err , res ) => {
79
- if ( err || res . statusCode !== 201 ) {
80
- log ( 'Error sending message %o' , err || res . statusCode ) ;
81
- cb ( err || new Error ( res . statusCode ) ) ;
82
- return ;
83
- }
84
- log ( 'Send result %d, %o' , res . statusCode , res . body ) ;
85
- cb ( null , res . body ) ;
86
- } ) ;
67
+ annotations : [ {
68
+ type : 'generic' ,
69
+ version : 1.0 ,
70
+
71
+ color : '#6CB7FB' ,
72
+ title : 'Echo message' ,
73
+ text : text ,
74
+
75
+ actor : {
76
+ name : 'Sample echo app'
77
+ }
78
+ } ]
79
+ }
80
+ } ) ;
81
+ log ( 'Send result %d, %o' , res . statusCode , res . body ) ;
82
+ }
83
+ catch ( err ) {
84
+ log ( 'Error sending message %o' , err ) ;
85
+ }
86
+ return res ;
87
87
} ;
88
88
89
89
// Verify Watson Work request signature
@@ -113,67 +113,57 @@ export const challenge = (wsecret) => (req, res, next) => {
113
113
} ;
114
114
115
115
// Create Express Web app
116
- export const webapp = ( appId , secret , wsecret , cb ) => {
117
- // Authenticate the app and get an OAuth token
118
- oauth . run ( appId , secret , ( err , token ) => {
119
- if ( err ) {
120
- cb ( err ) ;
121
- return ;
122
- }
116
+ export const webapp = async ( appId , secret , wsecret ) => {
123
117
124
- // Return the Express Web app
125
- cb ( null , express ( )
118
+ // Authenticate the app and get an OAuth token
119
+ const token = await oauth . run ( appId , secret ) ;
126
120
127
- // Configure Express route for the app Webhook
128
- . post ( '/echo' ,
121
+ // Configure Express route for the app Webhook
122
+ return express ( ) . post ( '/echo' ,
129
123
130
- // Verify Watson Work request signature and parse request body
131
- bparser . json ( {
132
- type : '*/*' ,
133
- verify : verify ( wsecret )
134
- } ) ,
124
+ // Verify Watson Work request signature and parse request body
125
+ bparser . json ( {
126
+ type : '*/*' ,
127
+ verify : verify ( wsecret )
128
+ } ) ,
135
129
136
- // Handle Watson Work Webhook challenge requests
137
- challenge ( wsecret ) ,
130
+ // Handle Watson Work Webhook challenge requests
131
+ challenge ( wsecret ) ,
138
132
139
- // Handle Watson Work messages
140
- echo ( appId , token ) ) ) ;
141
- } ) ;
133
+ // Handle Watson Work messages
134
+ echo ( appId , token ) ) ;
142
135
} ;
143
136
144
137
// App main entry point
145
- const main = ( argv , env , cb ) => {
146
- // Create Express Web app
147
- webapp (
148
- env . ECHO_APP_ID , env . ECHO_APP_SECRET ,
149
- env . ECHO_WEBHOOK_SECRET , ( err , app ) => {
150
- if ( err ) {
151
- cb ( err ) ;
152
- return ;
153
- }
154
-
155
- if ( env . PORT ) {
156
- // In a hosting environment like Bluemix for example, HTTPS is
157
- // handled by a reverse proxy in front of the app, just listen
158
- // on the configured HTTP port
159
- log ( 'HTTP server listening on port %d' , env . PORT ) ;
160
- http . createServer ( app ) . listen ( env . PORT , cb ) ;
161
- }
162
-
163
- else
164
- // Listen on the configured HTTPS port, default to 443
165
- ssl . conf ( env , ( err , conf ) => {
166
- if ( err ) {
167
- cb ( err ) ;
168
- return ;
169
- }
170
- const port = env . SSLPORT || 443 ;
171
- log ( 'HTTPS server listening on port %d' , port ) ;
172
- https . createServer ( conf , app ) . listen ( port , cb ) ;
173
- } ) ;
174
- } ) ;
138
+ const main = async ( argv , env , cb ) => {
139
+ try {
140
+ // Create Express Web app
141
+ const app = await webapp (
142
+ env . ECHO_APP_ID , env . ECHO_APP_SECRET ,
143
+ env . ECHO_WEBHOOK_SECRET ) ;
144
+ cb ( null ) ;
145
+ if ( env . PORT ) {
146
+ // In a hosting environment like Bluemix for example, HTTPS is
147
+ // handled by a reverse proxy in front of the app, just listen
148
+ // on the configured HTTP port
149
+ log ( 'HTTP server listening on port %d' , env . PORT ) ;
150
+ http . createServer ( app ) . listen ( env . PORT , cb ) ;
151
+ }
152
+
153
+ else {
154
+ // Listen on the configured HTTPS port, default to 443
155
+ const sslConfig = await ssl . conf ( env ) ;
156
+ const port = env . SSLPORT || 443 ;
157
+ log ( 'HTTPS server listening on port %D' , port ) ;
158
+ https . createServer ( sslConfig , app ) . listen ( port , cb ) ;
159
+ }
160
+ }
161
+ catch ( err ) {
162
+ cb ( err ) ;
163
+ }
175
164
} ;
176
165
166
+
177
167
if ( require . main === module )
178
168
main ( process . argv , process . env , ( err ) => {
179
169
if ( err ) {
0 commit comments