@@ -17,79 +17,88 @@ limitations under the License.
1717This is a sample Slack bot built with Botkit.
1818*/
1919
20- const { Botkit, BotkitConversation } = require ( 'botkit' )
21- const { SlackAdapter, SlackEventMiddleware } = require (
22- 'botbuilder-adapter-slack' )
23- const { SecretManagerServiceClient } = require ( '@google-cloud/secret-manager' )
20+ const { Botkit, BotkitConversation } = require ( 'botkit' ) ;
21+ const {
22+ SlackAdapter,
23+ SlackEventMiddleware,
24+ } = require ( 'botbuilder-adapter-slack' ) ;
25+ const { SecretManagerServiceClient } = require ( '@google-cloud/secret-manager' ) ;
2426
2527/**
2628 * Returns the secret string from Google Cloud Secret Manager
2729 * @param {string } name The name of the secret.
28- * @return {string } The string value of the secret.
30+ * @return {Promise< string> } The string value of the secret.
2931 */
30- async function accessSecretVersion ( name ) {
31- const client = new SecretManagerServiceClient ( )
32- const projectId = process . env . PROJECT_ID
32+ async function accessSecretVersion ( name ) {
33+ const client = new SecretManagerServiceClient ( ) ;
34+ const projectId = process . env . PROJECT_ID ;
3335 const [ version ] = await client . accessSecretVersion ( {
34- name : `projects/${ projectId } /secrets/${ name } /versions/1`
35- } )
36+ name : `projects/${ projectId } /secrets/${ name } /versions/1` ,
37+ } ) ;
3638
3739 // Extract the payload as a string.
38- const payload = version . payload . data . toString ( 'utf8' )
40+ const payload = version . payload . data . toString ( 'utf8' ) ;
3941
40- return payload
42+ return payload ;
4143}
4244
4345/**
44- * Asynchronous function to initialize kittenbot.
46+ * Function to initialize kittenbot.
4547 */
46- async function kittenbotInit ( ) {
48+ async function kittenbotInit ( ) {
4749 const adapter = new SlackAdapter ( {
4850 clientSigningSecret : await accessSecretVersion ( 'client-signing-secret' ) ,
49- botToken : await accessSecretVersion ( 'bot-token' )
50- } )
51+ botToken : await accessSecretVersion ( 'bot-token' ) ,
52+ } ) ;
5153
52- adapter . use ( new SlackEventMiddleware ( ) )
54+ adapter . use ( new SlackEventMiddleware ( ) ) ;
5355
5456 const controller = new Botkit ( {
5557 webhook_uri : '/api/messages' ,
56- adapter : adapter
57- } )
58+ adapter : adapter ,
59+ } ) ;
5860
5961 // Add Kitten Dialog
60- const convo = createKittenDialog ( controller )
61- controller . addDialog ( convo )
62+ const convo = createKittenDialog ( controller ) ;
63+ controller . addDialog ( convo ) ;
6264
6365 // Controller is ready
6466 controller . ready ( ( ) => {
65- controller . hears ( [ 'hello' , 'hi' ] , [ 'message' , 'direct_message' ] ,
67+ controller . hears (
68+ [ 'hello' , 'hi' , 'hey' ] ,
69+ [ 'message' , 'direct_message' ] ,
6670 async ( bot , message ) => {
67- return bot . reply ( message , 'Meow. :smile_cat:' )
68- } )
71+ await bot . reply ( message , 'Meow. :smile_cat:' ) ;
72+ return ;
73+ }
74+ ) ;
6975
7076 // START: listen for cat emoji delivery
71- controller . hears ( [ 'cat' , 'cats' , 'kitten' , 'kittens' ] ,
77+ controller . hears (
78+ [ 'cat' , 'cats' , 'kitten' , 'kittens' ] ,
7279 [ 'message' , 'direct_message' ] ,
7380 async ( bot , message ) => {
7481 // Don't respond to self
7582 if ( message . bot_id !== message . user ) {
76- await bot . startConversationInChannel ( message . channel , message . user )
77- return bot . beginDialog ( 'kitten-delivery' )
83+ await bot . startConversationInChannel ( message . channel , message . user ) ;
84+ await bot . beginDialog ( 'kitten-delivery' ) ;
85+ return ;
7886 }
79- } )
87+ }
88+ ) ;
8089 // END: listen for cat emoji delivery
8190
8291 // START: slash commands
8392 controller . on ( 'slash_command' , async ( bot , message ) => {
84- const numCats = parseInt ( message . text )
85- const response = makeCatMessage ( numCats )
86- bot . httpBody ( { text : response } )
87- } )
93+ const numCats = parseInt ( message . text ) ;
94+ const response = makeCatMessage ( numCats ) ;
95+ bot . httpBody ( { text : response } ) ;
96+ } ) ;
8897 // END: slash commands
89- } )
98+ } ) ;
9099}
91100
92- const maxCats = 20
101+ const maxCats = 20 ;
93102const catEmojis = [
94103 ':smile_cat:' ,
95104 ':smiley_cat:' ,
@@ -105,110 +114,117 @@ const catEmojis = [
105114 ':leopard:' ,
106115 ':lion_face:' ,
107116 ':tiger:' ,
108- ':tiger2:'
109- ]
117+ ':tiger2:' ,
118+ ] ;
110119
111120/**
112121 * Function to concatenate cat emojis
113122 * @param {number } numCats Number of cat emojis.
114123 * @return {string } The string message of cat emojis.
115124 */
116- function makeCatMessage ( numCats ) {
117- let catMessage = ''
125+ function makeCatMessage ( numCats ) {
126+ let catMessage = '' ;
118127 for ( let i = 0 ; i < numCats ; i ++ ) {
119128 // Append a random cat from the list
120- catMessage += catEmojis [ Math . floor ( Math . random ( ) * catEmojis . length ) ]
129+ catMessage += catEmojis [ Math . floor ( Math . random ( ) * catEmojis . length ) ] ;
121130 }
122- return catMessage
131+ return catMessage ;
123132}
124133
125134/**
126135 * Function to create the kitten conversation
127136 * @param {Object } controller The botkit controller.
128137 * @return {Object } The BotkitConversation object.
129138 */
130- function createKittenDialog ( controller ) {
131- const convo = new BotkitConversation ( 'kitten-delivery' , controller )
139+ function createKittenDialog ( controller ) {
140+ const convo = new BotkitConversation ( 'kitten-delivery' , controller ) ;
132141
133142 convo . ask ( 'Does someone need a kitten delivery?' , [
134143 {
135144 pattern : 'yes' ,
136145 handler : async ( response , convo , bot ) => {
137- await convo . gotoThread ( 'yes_kittens' )
138- }
146+ await convo . gotoThread ( 'yes_kittens' ) ;
147+ } ,
139148 } ,
140149 {
141150 pattern : 'no' ,
142151 handler : async ( response , convo , bot ) => {
143- await convo . gotoThread ( 'no_kittens' )
144- }
152+ await convo . gotoThread ( 'no_kittens' ) ;
153+ } ,
145154 } ,
146155 {
147156 default : true ,
148157 handler : async ( response , convo , bot ) => {
149- await convo . gotoThread ( 'default' )
150- }
151- }
152-
153- ] )
154-
155- convo . addQuestion ( 'How many would you like?' , [
156- {
157- pattern : '^[0-9]+?' ,
158- handler : async ( response , convo , bot , message ) => {
159- const numCats = parseInt ( response )
160- if ( numCats > maxCats ) {
161- await convo . gotoThread ( 'too_many' )
162- } else {
163- convo . setVar ( 'full_cat_message' , makeCatMessage ( numCats ) )
164- await convo . gotoThread ( 'cat_message' )
165- }
166- }
158+ await convo . gotoThread ( 'default' ) ;
159+ } ,
167160 } ,
168- {
169- default : true ,
170- handler : async ( response , convo , bot , message ) => {
171- if ( response ) {
172- await convo . gotoThread ( 'ask_again' )
173- } else {
174- // The response '0' is interpreted as null
175- await convo . gotoThread ( 'zero_kittens' )
176- }
177- }
178- }
179- ] , 'num_kittens' , 'yes_kittens' )
161+ ] ) ;
162+
163+ convo . addQuestion (
164+ 'How many would you like?' ,
165+ [
166+ {
167+ pattern : '^[0-9]+?' ,
168+ handler : async ( response , convo , bot , message ) => {
169+ const numCats = parseInt ( response ) ;
170+ if ( numCats > maxCats ) {
171+ await convo . gotoThread ( 'too_many' ) ;
172+ } else {
173+ convo . setVar ( 'full_cat_message' , makeCatMessage ( numCats ) ) ;
174+ await convo . gotoThread ( 'cat_message' ) ;
175+ }
176+ } ,
177+ } ,
178+ {
179+ default : true ,
180+ handler : async ( response , convo , bot , message ) => {
181+ if ( response ) {
182+ await convo . gotoThread ( 'ask_again' ) ;
183+ } else {
184+ // The response '0' is interpreted as null
185+ await convo . gotoThread ( 'zero_kittens' ) ;
186+ }
187+ } ,
188+ } ,
189+ ] ,
190+ 'num_kittens' ,
191+ 'yes_kittens'
192+ ) ;
180193
181194 // If numCats is too large, jump to start of the yes_kittens thread
182195 convo . addMessage (
183196 'Sorry, {{vars.num_kittens}} is too many cats. Pick a smaller number.' ,
184- 'too_many' )
185- convo . addAction ( 'yes_kittens' , 'too_many' )
197+ 'too_many'
198+ ) ;
199+ convo . addAction ( 'yes_kittens' , 'too_many' ) ;
186200
187201 // If response is not a number, jump to start of the yes_kittens thread
188- convo . addMessage ( ' Sorry I didn\ 't understand that' , 'ask_again' )
189- convo . addAction ( 'yes_kittens' , 'ask_again' )
202+ convo . addMessage ( " Sorry I didn't understand that" , 'ask_again' ) ;
203+ convo . addAction ( 'yes_kittens' , 'ask_again' ) ;
190204
191205 // If numCats is 0, send a dog instead
192206 convo . addMessage (
193207 {
194- text : 'Sorry to hear you want zero kittens. ' +
195- 'Here is a dog, instead. :dog:' ,
208+ text :
209+ 'Sorry to hear you want zero kittens. ' +
210+ 'Here is a dog, instead. :dog:' ,
196211 attachments : [
197212 {
198213 fallback : 'Chihuahua Bubbles - https://youtu.be/s84dBopsIe4' ,
199- text : '<https://youtu.be/s84dBopsIe4|' +
200- 'Chihuahua Bubbles>!'
201- }
202- ]
203- } , 'zero_kittens' )
214+ text : '<https://youtu.be/s84dBopsIe4|' + 'Chihuahua Bubbles>!' ,
215+ } ,
216+ ] ,
217+ } ,
218+ 'zero_kittens'
219+ ) ;
204220
205221 // Send cat message
206- convo . addMessage ( '{{vars.full_cat_message}}' , 'cat_message' )
222+ convo . addMessage ( '{{vars.full_cat_message}}' , 'cat_message' ) ;
207223
208- convo . addMessage ( 'Perhaps later.' , 'no_kittens' )
224+ convo . addMessage ( 'Perhaps later.' , 'no_kittens' ) ;
209225
210- return ( convo )
226+ return convo ;
211227}
212228// END: kitten-delivery convo
213229
214- kittenbotInit ( )
230+ kittenbotInit ( ) ;
0 commit comments