1
+ /**
2
+ * Copyright 2025 Google LLC
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ function handleIncidentWithAppCredentials ( formData ) {
18
+ const users = formData . users . trim ( ) . length > 0 ? formData . users . split ( ',' ) : [ ] ;
19
+ const spaceName = createChatSpaceWithAppCredentials ( formData . title ) ;
20
+ createHumanMembershipWithAppCredentials ( spaceName , getUserEmail ( ) ) ;
21
+ for ( const user of users ) {
22
+ createHumanMembershipWithAppCredentials ( spaceName , user ) ;
23
+ }
24
+ createMessageWithAppCredentials ( spaceName , formData . description ) ;
25
+ return spaceName ;
26
+ }
27
+
28
+
29
+
30
+ function createChatSpaceWithAppCredentials ( spaceName ) {
31
+ try {
32
+ const service = getService_ ( ) ;
33
+ if ( ! service . hasAccess ( ) ) {
34
+ console . error ( service . getLastError ( ) ) ;
35
+ return ;
36
+ }
37
+ // for private apps, the alias can be used
38
+ const my_customer_alias = "customers/my_customer"
39
+ // Specify the space to create.
40
+ const space = {
41
+ displayName : spaceName ,
42
+ spaceType : 'SPACE' ,
43
+ customer : my_customer_alias
44
+ } ;
45
+ // Call Chat API with a service account to create a message.
46
+ const createdSpace = Chat . Spaces . create (
47
+ space ,
48
+ { } ,
49
+ // Authenticate with the service account token.
50
+ { 'Authorization' : 'Bearer ' + service . getAccessToken ( ) } ) ;
51
+ // Log details about the created message.
52
+ console . log ( createdSpace ) ;
53
+ return createdSpace . name ;
54
+ } catch ( err ) {
55
+ // TODO (developer) - Handle exception.
56
+ console . log ( 'Failed to create space with error %s' , err . message ) ;
57
+ }
58
+ }
59
+
60
+ function createMessageWithAppCredentials ( spaceName , message ) {
61
+ try {
62
+ const service = getService_ ( ) ;
63
+ if ( ! service . hasAccess ( ) ) {
64
+ console . error ( service . getLastError ( ) ) ;
65
+ return ;
66
+ }
67
+
68
+ // Call Chat API with a service account to create a message.
69
+ const result = Chat . Spaces . Messages . create (
70
+ { 'text' : message } ,
71
+ spaceName ,
72
+ { } ,
73
+ // Authenticate with the service account token.
74
+ { 'Authorization' : 'Bearer ' + service . getAccessToken ( ) } ) ;
75
+
76
+ // Log details about the created message.
77
+ console . log ( result ) ;
78
+ } catch ( err ) {
79
+ // TODO (developer) - Handle exception.
80
+ console . log ( 'Failed to create message with error %s' , err . message ) ;
81
+ }
82
+ }
83
+
84
+ function createHumanMembershipWithAppCredentials ( spaceName , email ) {
85
+ try {
86
+ const service = getService_ ( ) ;
87
+ if ( ! service . hasAccess ( ) ) {
88
+ console . error ( service . getLastError ( ) ) ;
89
+ return ;
90
+ }
91
+ const membership = {
92
+ member : {
93
+ // TODO(developer): Replace USER_NAME here
94
+ name : 'users/' + email ,
95
+ // User type for the membership
96
+ type : 'HUMAN'
97
+ }
98
+ } ;
99
+ const result = Chat . Spaces . Members . create (
100
+ membership ,
101
+ spaceName ,
102
+ { } ,
103
+ { 'Authorization' : 'Bearer ' + service . getAccessToken ( ) }
104
+ ) ;
105
+ console . log ( result )
106
+ } catch ( err ) {
107
+ console . log ( 'Failed to create membership with error %s' , err . message )
108
+ }
109
+
110
+ }
111
+
112
+
113
+ function getService_ ( ) {
114
+ return OAuth2 . createService ( APP_CREDENTIALS . client_email )
115
+ . setTokenUrl ( 'https://oauth2.googleapis.com/token' )
116
+ . setPrivateKey ( APP_CREDENTIALS . private_key )
117
+ . setIssuer ( APP_CREDENTIALS . client_email )
118
+ . setSubject ( APP_CREDENTIALS . client_email )
119
+ . setScope ( APP_CREDENTIALS_SCOPES )
120
+ . setPropertyStore ( PropertiesService . getScriptProperties ( ) ) ;
121
+ }
0 commit comments