@@ -5,55 +5,74 @@ flags:gmi
55*/
66
77if ( current . channel == "GD51HTR46" || current . channel == "G9LAJG7G8" || current . channel == "G7M4AP6U8" ) { //admin channels on sndevs
8- var rm = new sn_ws . RESTMessageV2 ( ) ;
9- rm . setHttpMethod ( 'GET' ) ;
10- rm . setLogLevel ( 'all' ) ;
11- rm . setRequestHeader ( 'Content-Type' , 'application/x-www-form-urlencoded' ) ;
12- rm . setRequestHeader ( 'authorization' , 'Bearer ' + gs . urlEncode ( gs . getProperty ( 'x_snc_slackerbot.SlackerBot.token' ) ) ) ;
13- var bodyString = '' ;
14- // bodyString += 'token=' + gs.urlEncode(gs.getProperty('x_snc_slackerbot.SlackerBot.token'));
15- var user_id = current . text ;
16- user_id = user_id . replace ( '!whois <@' , '' ) ;
17- user_id = user_id . replace ( '>' , '' ) . trim ( ) ;
18- bodyString += 'user=' + gs . urlEncode ( user_id ) ;
19- rm . setEndpoint ( 'https://slack.com/api/users.info' + '?' + bodyString ) ;
20- var response = rm . execute ( ) ;
21- var response_body = JSON . parse ( response . getBody ( ) ) ;
8+ // Update desired keys if you want to change returned fields from payload
9+ const desiredKeys = [ 'user.real_name' , 'user.profile.pronouns' , 'user.profile.email' , 'user.tz_label' , 'user.updated' ] ;
10+ const slacker = new x_snc_slackerbot . Slacker ( ) ;
11+ let userId = current . text ;
12+ userId = userId . replace ( '!whois <@' , '' ) ;
13+ userId = gs . urlEncode ( userId . replace ( '>' , '' ) . trim ( ) ) ;
2214
23- var message_body = '' ;
24- for ( var key in response_body . user ) {
25- if ( key == 'profile' ) {
26- message_body += 'profile: \n' ;
27- for ( var prof_key in response_body . user . profile ) {
28- if ( prof_key . indexOf ( 'image_' ) != - 1 ) continue ;
29- if ( prof_key == 'status_emoji_display_info' ) {
30- message_body += ' status_emoji_display_info: \n' ;
31- for ( var stat_key in response_body . user . profile . status_emoji_display_info ) {
32- message_body += ' ' + stat_key + ': ' + response_body . user . profile . status_emoji_display_info [ stat_key ] + '\n' ;
33- }
34- } else message_body += ' ' + prof_key + ': ' + response_body . user . profile [ prof_key ] + '\n' ;
35- }
36- } else message_body += key + ': ' + response_body . user [ key ] + '\n' ;
37- }
15+ const rm = new sn_ws . RESTMessageV2 ( ) ;
16+ rm . setHttpMethod ( 'GET' ) ;
17+ rm . setLogLevel ( 'all' ) ;
18+ rm . setRequestHeader ( 'Content-Type' , 'application/x-www-form-urlencoded' ) ;
19+ rm . setRequestHeader ( 'authorization' , `Bearer ${ gs . urlEncode ( gs . getProperty ( 'x_snc_slackerbot.SlackerBot.token' ) ) } ` ) ;
20+ rm . setEndpoint ( `https://slack.com/api/users.info?user=${ userId } ` ) ;
21+ const response = rm . execute ( ) ;
22+ const responseBody = JSON . parse ( response . getBody ( ) ) ;
3823
39- message_body = message_body . match ( / ^ ( r e a l _ n a m e | t z _ l a b e l | { 2 } e m a i l ) : .* $ / gm) . join ( '\n' ) ;
40- message_body = message_body . replace ( / ^ \s { 2 } ( e m a i l ) / gm, '$1' ) ;
24+ if ( ! responseBody . ok ) {
25+ slacker . send_chat ( current , 'i dunno' , false ) ;
26+ } else {
27+ const messageArray = [ ] ;
28+ let messageBody = '' ;
4129
42- // Add record details
43- var grUser = new GlideRecord ( 'x_snc_slackerbot_user' ) ;
44- if ( grUser . get ( 'user_id' , user_id ) && Object . keys ( grUser ) . indexOf ( 'verified' ) != - 1 ) { // Backwards support until instance updates with new schema
45- message_body += '\nIdentity verified: ' + ( grUser . getValue ( 'verified' ) == 1 ? 'Yes' : 'No' ) + '\n' ;
46- if ( ! gs . nil ( grUser . getValue ( 'admin_info' ) ) ) {
47- message_body += 'Admin information: ' + grUser . getValue ( 'admin_info' ) + '\n' ;
48- }
49- if ( ! gs . nil ( grUser . getValue ( 'user_info' ) ) ) {
50- message_body += 'User-visible information: ' + grUser . getValue ( 'user_info' ) + '\n' ;
51- }
52- }
30+ for ( let key of desiredKeys ) {
31+ let path = key . split ( '.' ) ;
32+ let obj = { } ;
33+ switch ( path . length ) {
34+ case 1 :
35+ obj [ path [ 0 ] ] = responseBody [ path [ 0 ] ] ?? '{Not set}' ;
36+ break ;
37+ case 2 :
38+ obj [ path [ 1 ] ] = responseBody [ path [ 0 ] ] [ path [ 1 ] ] ?? '{Not set}' ;
39+ break ;
40+ case 3 :
41+ obj [ path [ 2 ] ] = responseBody [ path [ 0 ] ] [ path [ 1 ] ] [ path [ 2 ] ] ?? '{Not set}' ;
42+ break ;
43+ default :
44+ gs . warn ( `Whois parser encountered unexpected path length for a key. Key is ${ key } ` ) ;
45+ }
5346
54- if ( response_body . user . name ) {
55- var send_chat = new x_snc_slackerbot . Slacker ( ) . send_chat ( current , '```' + message_body + '```' , false ) ;
56- } else {
57- var send_chat2 = new x_snc_slackerbot . Slacker ( ) . send_chat ( current , 'i dunno' , false ) ;
58- }
47+ // Handle conversions
48+ if ( path [ 1 ] == 'updated' ) {
49+ let time = new GlideDateTime ( ) ;
50+ time . setValue ( responseBody [ path [ 0 ] ] [ path [ 1 ] ] * 1000 ) ;
51+ obj [ path [ 1 ] ] = `${ time . getDisplayValue ( ) } (${ gs . getSession ( ) . getTimeZoneName ( ) } )` ;
52+ }
53+ messageArray . push ( obj ) ;
54+ }
55+
56+ // Get verify data
57+ const grUser = new GlideRecord ( 'x_snc_slackerbot_user' ) ;
58+ if ( grUser . get ( 'user_id' , userId ) ) {
59+ messageArray . push ( {
60+ 'Identity verified' : grUser . getValue ( 'verified' ) == 1 ? 'Yes' : 'No'
61+ } ) ;
62+ if ( ! gs . nil ( grUser . getValue ( 'admin_info' ) ) ) {
63+ messageArray . push ( {
64+ 'Admin information' : grUser . getValue ( 'admin_info' )
65+ } ) ;
66+ }
67+ if ( ! gs . nil ( grUser . getValue ( 'user_info' ) ) ) {
68+ messageArray . push ( {
69+ 'User-visible information' : grUser . getValue ( 'user_info' )
70+ } ) ;
71+ }
72+ }
73+
74+ messageBody = messageArray . map ( obj => `${ Object . keys ( obj ) [ 0 ] } : ${ Object . values ( obj ) [ 0 ] } ` ) . join ( '\n' ) ;
75+
76+ slacker . send_chat ( current , '```' + messageBody + '```' , false ) ;
77+ }
5978}
0 commit comments