@@ -5,92 +5,115 @@ flags:gmi
55*/
66
77( function ( current ) {
8- var si = new Slacker ( ) ;
9- var thread_ts = current . getValue ( 'thread_ts' ) ;
10-
11- // Commented out code block
12- // if (!thread_ts) {
13- // message = 'This command is only usable in a thread.';
14- // si.send_chat(current, message, false);
15- // return null;
16- // }
17-
188 function findWordAfterDashDash ( str ) {
19- var match = str . match ( / - - \s * ( .+ ) / ) ;
20- return match ? " in the style of " + match [ 1 ] : "" ;
9+ let match = str . match ( / - - \s * ( .+ ) / ) ;
10+ return match ? ` in the style of ${ match [ 1 ] } ` : '' ;
2111 }
22- var style = findWordAfterDashDash ( current . text ) ;
2312
24- var chats = [ ] ;
25- var nda = false ;
26- var chatGr = new GlideRecord ( 'x_snc_pointsthing_chat' ) ;
13+ const si = new x_snc_slackerbot . Slacker ( ) ;
14+ const thread_ts = current . getValue ( 'thread_ts' ) ;
15+ const style = findWordAfterDashDash ( current . text ) ;
16+ const chats = [ ] ;
17+ const userPronouns = [ ] ;
18+ const users = [ ] ;
19+ let nda = false ;
20+ let encodedQuery = '' ;
2721 if ( thread_ts ) {
28- chatGr . addEncodedQuery ( 'thread_ts=' + thread_ts + ' ^ORts=' + thread_ts + ' ^textNOT LIKE!catchmeup^textNOT LIKE!summary^textNOT LIKE!catchup' ) ;
22+ encodedQuery = ` thread_ts= ${ thread_ts } ^ORts=${ thread_ts } ^textNOT LIKE!catchmeup^textNOT LIKE!summary^textNOT LIKE!catchup` ;
2923 } else {
30- chatGr . addEncodedQuery ( 'channel=' + current . getValue ( 'channel' ) + ' ^sys_created_onRELATIVEGT@minute@ago@60^thread_ts=NULL^textNOT LIKE!catchmeup^textNOT LIKE!summary^textNOT LIKE!catchup' ) ;
24+ encodedQuery = `channel= ${ current . getValue ( 'channel' ) } ^sys_created_onRELATIVEGT@minute@ago@60^thread_ts=NULL^textNOT LIKE!catchmeup^textNOT LIKE!summary^textNOT LIKE!catchup` ;
3125 }
26+
27+
28+ const chatGr = new GlideRecord ( 'x_snc_slackerbot_chat' ) ;
29+ chatGr . addEncodedQuery ( encodedQuery ) ;
3230 chatGr . orderBy ( 'sys_created_on' ) ;
3331 chatGr . query ( ) ;
32+
3433 while ( chatGr . next ( ) ) {
35- var chat = chatGr . getDisplayValue ( 'user' ) + ': ' + chatGr . getValue ( 'text' ) ;
34+ const chat = ` ${ chatGr . getDisplayValue ( 'user' ) } : ${ chatGr . getValue ( 'text' ) } ` ;
3635 if ( chat . indexOf ( '!nda' ) > - 1 || chat . indexOf ( '!confidential' ) > - 1 ) {
3736 nda = true ;
3837 break ;
3938 }
4039 chats . push ( chat ) ;
4140 }
42-
41+
4342 if ( nda ) {
4443 if ( thread_ts ) {
45- new x_snc_slackerbot . Slacker ( ) . send_chat ( current , " This thread has been marked confidential and cannot be summarized." , false ) ;
44+ si . send_chat ( current , ' This thread has been marked confidential and cannot be summarized.' , false ) ;
4645 } else {
47- new x_snc_slackerbot . Slacker ( ) . send_chat ( current , " This recent conversation has been marked confidential and cannot be summarized." , false ) ;
46+ si . send_chat ( current , ' This recent conversation has been marked confidential and cannot be summarized.' , false ) ;
4847 }
4948 return ;
5049 }
5150
52- var prompt = current . text . replace ( / ! c h a t g p t / gmi, "" ) . trim ( ) . substring ( 0 , 1000 ) ;
53- var chatReq = new sn_ws . RESTMessageV2 ( ) ;
51+ const userGa = new GlideAggregate ( 'x_snc_slackerbot_chat' ) ;
52+ userGa . addEncodedQuery ( encodedQuery ) ;
53+ userGa . addAggregate ( 'GROUP_CONCAT_DISTINCT' , 'user.user_id' ) ;
54+ userGa . query ( ) ;
55+
56+ while ( userGa . next ( ) ) {
57+ const userId = userGa . getAggregate ( 'GROUP_CONCAT_DISTINCT' , 'user.user_id' ) ;
58+
59+ const rm = new sn_ws . RESTMessageV2 ( ) ;
60+ rm . setHttpMethod ( 'GET' ) ;
61+ rm . setLogLevel ( 'all' ) ;
62+ rm . setRequestHeader ( 'Content-Type' , 'application/x-www-form-urlencoded' ) ;
63+ rm . setRequestHeader ( 'authorization' , `Bearer ${ gs . urlEncode ( gs . getProperty ( 'x_snc_slackerbot.SlackerBot.token' ) ) } ` ) ;
64+ rm . setEndpoint ( `https://slack.com/api/users.info?user=${ userId } ` ) ;
65+ const response = rm . execute ( ) ;
66+ const responseBody = JSON . parse ( response . getBody ( ) ) ;
67+ const pronouns = responseBody . user . profile . pronouns ?? 'they/them' ;
68+ const name = responseBody . user . real_name ?? 'Unknown' ;
69+ userPronouns . push ( `${ name } : ${ pronouns } ` ) ;
70+ users . push ( name ) ;
71+ }
72+
73+ const chatReq = new sn_ws . RESTMessageV2 ( ) ;
5474 chatReq . setEndpoint ( 'https://api.openai.com/v1/chat/completions' ) ;
55- chatReq . setHttpMethod ( " POST" ) ;
56- chatReq . setRequestHeader ( " Authorization" , " Bearer " + gs . getProperty ( " openai.key" ) ) ;
57- chatReq . setRequestHeader ( 'Content-Type' , " application/json" ) ;
58- chatReq . setRequestHeader ( 'User-Agent' , " ServiceNow" ) ;
59- chatReq . setRequestHeader ( " Accept" , " */*" ) ;
60- var body = {
61- " model" : " gpt-4o" ,
62- " messages" : [ {
63- " role" : " system" ,
64- " content" : `
75+ chatReq . setHttpMethod ( ' POST' ) ;
76+ chatReq . setRequestHeader ( ' Authorization' , ` Bearer ${ gs . getProperty ( ' openai.key' ) } ` ) ;
77+ chatReq . setRequestHeader ( 'Content-Type' , ' application/json' ) ;
78+ chatReq . setRequestHeader ( 'User-Agent' , ' ServiceNow' ) ;
79+ chatReq . setRequestHeader ( ' Accept' , ' */*' ) ;
80+ const body = {
81+ ' model' : ' gpt-4o' ,
82+ ' messages' : [ {
83+ ' role' : ' system' ,
84+ ' content' : `
6585 You are a Slack bot that enhances the formatting of messages to make them more engaging and visually appealing.
6686 Utilize Slack's markdown language mrkdwn for various formatting elements.
6787 Follow these instructions:
6888 1. Enclose important words or phrases with *asterisks* for bold emphasis.
6989 2. Enclose code and numbers and percentages using backticks, like \`this\`.
7090 3. Use emojis when necessary to add expressiveness.
71- 4. Organize text with numbered or bullet lists, using "-" for bullet points.
91+ 4. Organize text with numbered or bullet lists, using '-' for bullet points.
7292 5. Combine bold and lists as needed: *Bold text*: normal text.
7393 6. Italicize words like _this_.
74- 7. Use blockquotes with ">" for quotes.
94+ 7. Use blockquotes with '>' for quotes.
7595 8. For URLs, use <http://example.com|Clickable link>.
7696 9. Keep user (@user) and channel (#channel) tags unchanged.
77- Keep close to the original message tone and formatting.`
97+ Keep close to the original message tone and formatting.
98+
99+ Use the below mapping between user name and pronouns to ensure you use the correct pronoun:
100+ ${ userPronouns . join ( '\n' ) } `
78101 } ,
79102 {
80- " role" : " user" ,
81- " content" : " summarize the following conversation" + style + " . You cannot ask for follow-up responses. Ignore the user named Slackbot.\n\n" + chats . join ( "\n" )
103+ ' role' : ' user' ,
104+ ' content' : ` summarize the following conversation${ style } . You cannot ask for follow-up responses. Ignore the user named Slackbot.\n\n${ chats . join ( '\n' ) } `
82105 }
83106 ] ,
84- // " max_tokens" : 250
107+ // ' max_tokens' : 250
85108 } ;
86109 chatReq . setRequestBody ( JSON . stringify ( body ) ) ;
87- var chatResponse = chatReq . execute ( ) ;
110+ const chatResponse = chatReq . execute ( ) ;
88111 gs . info ( chatResponse . getBody ( ) ) ;
89- var chatResponseBody = JSON . parse ( chatResponse . getBody ( ) ) ;
112+ const chatResponseBody = JSON . parse ( chatResponse . getBody ( ) ) ;
90113
91- var show_tokens = false ;
92- var token_cost = show_tokens ? " > tokens: " + chatResponseBody . usage . total_tokens + " ($" + ( parseInt ( chatResponseBody . usage . total_tokens ) * 0.000002 ) . toFixed ( 6 ) + " )\n" : "" ;
114+ const show_tokens = false ;
115+ const token_cost = show_tokens ? ` > tokens: ${ chatResponseBody . usage . total_tokens } ($ ${ ( parseInt ( chatResponseBody . usage . total_tokens ) * 0.000002 ) . toFixed ( 6 ) } )\n` : '' ;
93116
94- var intro = thread_ts ? " > This thread so far:\n" : " > Last 60 minutes summarized:\n" ;
95- new x_snc_slackerbot . Slacker ( ) . send_chat ( current , intro + token_cost + "\n" + chatResponseBody . choices [ 0 ] . message . content , false ) ;
117+ const intro = thread_ts ? ' > This thread so far:\n' : ' > Last 60 minutes summarized:\n' ;
118+ si . send_chat ( current , ` ${ intro } \nInvolved users:\n ${ users . join ( '\n' ) } \n ${ token_cost } \n ${ chatResponseBody . choices [ 0 ] . message . content } ` , false ) ;
96119} ) ( current ) ;
0 commit comments