13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
+ // [START handle_incident_with_application_credentials]
16
17
18
+ /**
19
+ * Handles an incident by creating a chat space, adding members, and posting a message.
20
+ * All the actions are done using application credentials.
21
+ *
22
+ * @param {Object } formData - The data submitted by the user. It should contain the fields:
23
+ * - title: The display name of the chat space.
24
+ * - description: The description of the incident.
25
+ * - users: A comma-separated string of user emails to be added to the space.
26
+ * @return {string } The resource name of the new space.
27
+ */
17
28
function handleIncidentWithAppCredentials ( formData ) {
18
29
const users = formData . users . trim ( ) . length > 0 ? formData . users . split ( ',' ) : [ ] ;
19
- const spaceName = createChatSpaceWithAppCredentials ( formData . title ) ;
20
- createHumanMembershipWithAppCredentials ( spaceName , getUserEmail ( ) ) ;
30
+ const service = _getService ( ) ;
31
+ if ( ! service . hasAccess ( ) ) {
32
+ console . error ( service . getLastError ( ) ) ;
33
+ return ;
34
+ }
35
+ const spaceName = _createChatSpaceWithAppCredentials ( formData . title , service ) ;
36
+ _createHumanMembershipWithAppCredentials ( spaceName , getUserEmail ( ) , service ) ;
21
37
for ( const user of users ) {
22
- createHumanMembershipWithAppCredentials ( spaceName , user ) ;
38
+ _createHumanMembershipWithAppCredentials ( spaceName , user , service ) ;
23
39
}
24
- createMessageWithAppCredentials ( spaceName , formData . description ) ;
40
+ _createMessageWithAppCredentials ( spaceName , formData . description , service ) ;
25
41
return spaceName ;
26
42
}
27
43
28
-
29
-
30
- function createChatSpaceWithAppCredentials ( spaceName ) {
44
+ /**
45
+ * Creates a chat space with application credentials.
46
+ *
47
+ * @param {string } displayName - The name of the chat space.
48
+ * @param {object } service - The credentials of the service account.
49
+ * @returns {string } The resource name of the new space.
50
+ */
51
+ function _createChatSpaceWithAppCredentials ( displayName , service ) {
31
52
try {
32
- const service = getService_ ( ) ;
33
- if ( ! service . hasAccess ( ) ) {
34
- console . error ( service . getLastError ( ) ) ;
35
- return ;
36
- }
37
53
// for private apps, the alias can be used
38
54
const my_customer_alias = "customers/my_customer"
39
55
// Specify the space to create.
40
56
const space = {
41
- displayName : spaceName ,
57
+ displayName : displayName ,
42
58
spaceType : 'SPACE' ,
43
59
customer : my_customer_alias
44
60
} ;
@@ -56,8 +72,15 @@ function createChatSpaceWithAppCredentials(spaceName) {
56
72
console . log ( 'Failed to create space with error %s' , err . message ) ;
57
73
}
58
74
}
59
-
60
- function createMessageWithAppCredentials ( spaceName , message ) {
75
+ /*
76
+ * Creates a chat message with application credentials.
77
+ *
78
+ * @param {string } spaceName - The resource name of the space.
79
+ * @param {string } message - The text to be posted.
80
+ * @param {object } service - The credentials of the service account.
81
+ * @return {string } the resource name of the new space.
82
+ */
83
+ function _createMessageWithAppCredentials ( spaceName , message , service ) {
61
84
try {
62
85
const service = getService_ ( ) ;
63
86
if ( ! service . hasAccess ( ) ) {
@@ -80,8 +103,14 @@ function createMessageWithAppCredentials(spaceName, message) {
80
103
console . log ( 'Failed to create message with error %s' , err . message ) ;
81
104
}
82
105
}
83
-
84
- function createHumanMembershipWithAppCredentials ( spaceName , email ) {
106
+ /**
107
+ * Creates a human membership in a chat space with application credentials.
108
+ *
109
+ * @param {string } spaceName - The resource name of the space.
110
+ * @param {string } email - The email of the user to be added.
111
+ * @param {object } service - The credentials of the service account.
112
+ */
113
+ function _createHumanMembershipWithAppCredentials ( spaceName , email , service ) {
85
114
try {
86
115
const service = getService_ ( ) ;
87
116
if ( ! service . hasAccess ( ) ) {
@@ -90,7 +119,6 @@ function createHumanMembershipWithAppCredentials(spaceName, email){
90
119
}
91
120
const membership = {
92
121
member : {
93
- // TODO(developer): Replace USER_NAME here
94
122
name : 'users/' + email ,
95
123
// User type for the membership
96
124
type : 'HUMAN'
@@ -109,13 +137,17 @@ function createHumanMembershipWithAppCredentials(spaceName, email){
109
137
110
138
}
111
139
112
-
113
- function getService_ ( ) {
140
+ /*
141
+ * Creates a service for the service account.
142
+ * @return {object } - The credentials of the service account.
143
+ */
144
+ function _getService ( ) {
114
145
return OAuth2 . createService ( APP_CREDENTIALS . client_email )
115
146
. setTokenUrl ( 'https://oauth2.googleapis.com/token' )
116
147
. setPrivateKey ( APP_CREDENTIALS . private_key )
117
148
. setIssuer ( APP_CREDENTIALS . client_email )
118
149
. setSubject ( APP_CREDENTIALS . client_email )
119
150
. setScope ( APP_CREDENTIALS_SCOPES )
120
151
. setPropertyStore ( PropertiesService . getScriptProperties ( ) ) ;
121
- }
152
+ }
153
+ // [END handle_incident_with_application_credentials]
0 commit comments