-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Main.gs
719 lines (643 loc) · 21.8 KB
/
Main.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// This script provides each code sample in a separate function.
// It may require modifications to work in your environment.
// For more information on user authentication, see
// https://developers.google.com/workspace/chat/authenticate-authorize-chat-user
// For more information on app authentication, see
// https://developers.google.com/workspace/chat/authenticate-authorize-chat-app
// [START chat_create_membership_user_cred]
/**
* This sample shows how to create membership with user credential for a human user
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.memberships'
* referenced in the manifest file (appsscript.json).
*/
function createMembershipUserCred() {
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME here.
const parent = 'spaces/SPACE_NAME';
const membership = {
member: {
// TODO(developer): Replace USER_NAME here
name: 'users/USER_NAME',
// User type for the membership
type: 'HUMAN'
}
};
// Make the request
const response = Chat.Spaces.Members.create(membership, parent);
// Handle the response
console.log(response);
}
// [END chat_create_membership_user_cred]
// [START chat_create_membership_user_cred_for_app]
/**
* This sample shows how to create membership with app credential for an app
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.memberships.app'
* referenced in the manifest file (appsscript.json).
*/
function createMembershipUserCredForApp() {
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME here.
const parent = 'spaces/SPACE_NAME';
const membership = {
member: {
// Member name for app membership, do not change this
name: 'users/app',
// User type for the membership
type: 'BOT'
}
};
// Make the request
const response = Chat.Spaces.Members.create(membership, parent);
// Handle the response
console.log(response);
}
// [END chat_create_membership_user_cred_for_app]
// [START chat_create_message_app_cred]
/**
* This sample shows how to create message with app credential
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.bot'
* used by service accounts.
*/
function createMessageAppCred() {
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME here.
const parent = 'spaces/SPACE_NAME';
const message = {
text: 'Hello with app credential!'
};
const parameters = {};
// Make the request
const response = Chat.Spaces.Messages.create(
message, parent, parameters, getHeaderWithAppCredentials()
);
// Handle the response
console.log(response);
}
// [END chat_create_message_app_cred]
// [START chat_create_message_app_cred_with_cards]
/**
* This sample shows how to create message with a card attached with app credential
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.bot'
* used by service accounts.
*/
function createMessageAppCredWithCards() {
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME here.
const parent = 'spaces/SPACE_NAME';
const message = {
text: 'Hello with app credential!',
cardsV2: [{
cardId: 'card-id',
card: {
header: {
title: 'And with a card!',
}
}
}]
};
const parameters = {};
// Make the request
const response = Chat.Spaces.Messages.create(
message, parent, parameters, getHeaderWithAppCredentials()
);
// Handle the response
console.log(response);
}
// [END chat_create_message_app_cred_with_cards]
// [START chat_create_message_user_cred]
/**
* This sample shows how to create message with user credential
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.messages.create'
* referenced in the manifest file (appsscript.json).
*/
function createMessageUserCred() {
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME here.
const parent = 'spaces/SPACE_NAME';
const message = {
text: 'Hello with user credential!'
};
// Make the request
const response = Chat.Spaces.Messages.create(message, parent);
// Handle the response
console.log(response);
}
// [END chat_create_message_user_cred]
// [START chat_create_message_user_cred_at_mention]
/**
* This sample shows how to create message with user credential with a user mention
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.messages.create'
* referenced in the manifest file (appsscript.json).
*/
function createMessageUserCredAtMention() {
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME here.
const parent = 'spaces/SPACE_NAME';
const message = {
// The user with USER_NAME will be mentioned if they are in the space
// TODO(developer): Replace USER_NAME here
text: 'Hello <users/USER_NAME>!'
};
// Make the request
const response = Chat.Spaces.Messages.create(message, parent);
// Handle the response
console.log(response);
}
// [END chat_create_message_user_cred_at_mention]
// [START chat_create_message_user_cred_message_id]
/**
* This sample shows how to create message with user credential with message id
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.messages.create'
* referenced in the manifest file (appsscript.json).
*/
function createMessageUserCredMessageId() {
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME here.
const parent = 'spaces/SPACE_NAME';
// Message id lets chat apps get, update or delete a message without needing
// to store the system assigned ID in the message's resource name
const messageId = 'client-MESSAGE-ID';
const message = { text: 'Hello with user credential!' };
// Make the request
const response = Chat.Spaces.Messages.create(message, parent, {
messageId: messageId
});
// Handle the response
console.log(response);
}
// [END chat_create_message_user_cred_message_id]
// [START chat_create_message_user_cred_request_id]
/**
* This sample shows how to create message with user credential with request id
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.messages.create'
* referenced in the manifest file (appsscript.json).
*/
function createMessageUserCredRequestId() {
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME here.
const parent = 'spaces/SPACE_NAME';
// Specifying an existing request ID returns the message created with
// that ID instead of creating a new message
const requestId = 'REQUEST_ID';
const message = { text: 'Hello with user credential!' };
// Make the request
const response = Chat.Spaces.Messages.create(message, parent, {
requestId: requestId
});
// Handle the response
console.log(response);
}
// [END chat_create_message_user_cred_request_id]
// [START chat_create_message_user_cred_thread_key]
/**
* This sample shows how to create message with user credential with thread key
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.messages.create'
* referenced in the manifest file (appsscript.json).
*/
function createMessageUserCredThreadKey() {
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME here.
const parent = 'spaces/SPACE_NAME';
// Creates the message as a reply to the thread specified by thread_key
// If it fails, the message starts a new thread instead
const messageReplyOption = 'REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD';
const message = {
text: 'Hello with user credential!',
thread: {
// Thread key specifies a thread and is unique to the chat app
// that sets it
threadKey: 'THREAD_KEY'
}
};
// Make the request
const response = Chat.Spaces.Messages.create(message, parent, {
messageReplyOption: messageReplyOption
});
// Handle the response
console.log(response);
}
// [END chat_create_message_user_cred_thread_key]
// [START chat_create_message_user_cred_thread_name]
/**
* This sample shows how to create message with user credential with thread name
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.messages.create'
* referenced in the manifest file (appsscript.json).
*/
function createMessageUserCredThreadName() {
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME here.
const parent = 'spaces/SPACE_NAME';
// Creates the message as a reply to the thread specified by thread.name
// If it fails, the message starts a new thread instead
const messageReplyOption = 'REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD';
const message = {
text: 'Hello with user credential!',
thread: {
// Resource name of a thread that uniquely identify a thread
// TODO(developer): Replace SPACE_NAME and THREAD_NAME here
name: 'spaces/SPACE_NAME/threads/THREAD_NAME'
}
};
// Make the request
const response = Chat.Spaces.Messages.create(message, parent, {
messageReplyOption: messageReplyOption
});
// Handle the response
console.log(response);
}
// [END chat_create_message_user_cred_thread_name]
// [START chat_delete_message_app_cred]
/**
* This sample shows how to delete a message with app credential
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.bot'
* used by service accounts.
*/
function deleteMessageAppCred() {
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME and MESSAGE_NAME here
const name = 'spaces/SPACE_NAME/messages/MESSAGE_NAME';
const parameters = {};
// Make the request
const response = Chat.Spaces.Messages.remove(name, parameters, getHeaderWithAppCredentials());
// Handle the response
console.log(response);
}
// [END chat_delete_message_app_cred]
// [START chat_delete_message_user_cred]
/**
* This sample shows how to delete a message with user credential
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.messages'
* referenced in the manifest file (appsscript.json).
*/
function deleteMessageUserCred() {
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME and MESSAGE_NAME here
const name = 'spaces/SPACE_NAME/messages/MESSAGE_NAME';
// Make the request
const response = Chat.Spaces.Messages.remove(name);
// Handle the response
console.log(response);
}
// [END chat_delete_message_user_cred]
// [START chat_get_membership_app_cred]
/**
* This sample shows how to get membership with app credential
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.bot'
* used by service accounts.
*/
function getMembershipAppCred() {
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME and MEMBER_NAME here
const name = 'spaces/SPACE_NAME/members/MEMBER_NAME';
const parameters = {};
// Make the request
const response = Chat.Spaces.Members.get(name, parameters, getHeaderWithAppCredentials());
// Handle the response
console.log(response);
}
// [END chat_get_membership_app_cred]
// [START chat_get_membership_user_cred]
/**
* This sample shows how to get membership with user credential
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.memberships.readonly'
* referenced in the manifest file (appsscript.json).
*/
function getMembershipUserCred() {
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME and MEMBER_NAME here
const name = 'spaces/SPACE_NAME/members/MEMBER_NAME';
// Make the request
const response = Chat.Spaces.Members.get(name);
// Handle the response
console.log(response);
}
// [END chat_get_membership_user_cred]
// [START chat_get_message_app_cred]
/**
* This sample shows how to get message with app credential
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.bot'
* used by service accounts.
*/
function getMessageAppCred() {
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME and MESSAGE_NAME here
const name = 'spaces/SPACE_NAME/messages/MESSAGE_NAME';
const parameters = {};
// Make the request
const response = Chat.Spaces.Messages.get(name, parameters, getHeaderWithAppCredentials());
// Handle the response
console.log(response);
}
// [END chat_get_message_app_cred]
// [START chat_get_message_user_cred]
/**
* This sample shows how to get message with user credential
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.messages.readonly'
* referenced in the manifest file (appsscript.json).
*/
function getMessageUserCred() {
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME and MESSAGE_NAME here
const name = 'spaces/SPACE_NAME/messages/MESSAGE_NAME';
// Make the request
const response = Chat.Spaces.Messages.get(name);
// Handle the response
console.log(response);
}
// [END chat_get_message_user_cred]
// [START chat_get_space_app_cred]
/**
* This sample shows how to get space with app credential
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.bot'
* used by service accounts.
*/
function getSpaceAppCred() {
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME here
const name = 'spaces/SPACE_NAME';
const parameters = {};
// Make the request
const response = Chat.Spaces.get(name, parameters, getHeaderWithAppCredentials());
// Handle the response
console.log(response);
}
// [END chat_get_space_app_cred]
// [START chat_get_space_user_cred]
/**
* This sample shows how to get space with user credential
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.spaces.readonly'
* referenced in the manifest file (appsscript.json).
*/
function getSpaceUserCred() {
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME here
const name = 'spaces/SPACE_NAME';
// Make the request
const response = Chat.Spaces.get(name);
// Handle the response
console.log(response);
}
// [END chat_get_space_user_cred]
// [START chat_list_memberships_app_cred]
/**
* This sample shows how to list memberships with app credential
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.bot'
* used by service accounts.
*/
function listMembershipsAppCred() {
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME here
const parent = 'spaces/SPACE_NAME';
// Filter membership by type (HUMAN or BOT) or role (ROLE_MEMBER or
// ROLE_MANAGER)
const filter = 'member.type = "HUMAN"';
// Iterate through the response pages using page tokens
let responsePage;
let pageToken = null;
do {
// Request response pages
responsePage = Chat.Spaces.Members.list(parent, {
filter: filter,
pageSize: 10,
pageToken: pageToken
}, getHeaderWithAppCredentials());
// Handle response pages
if (responsePage.memberships) {
responsePage.memberships.forEach((membership) => console.log(membership));
}
// Update the page token to the next one
pageToken = responsePage.nextPageToken;
} while (pageToken);
}
// [END chat_list_memberships_app_cred]
// [START chat_list_memberships_user_cred]
/**
* This sample shows how to list memberships with user credential
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.memberships.readonly'
* referenced in the manifest file (appsscript.json).
*/
function listMembershipsUserCred() {
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME here
const parent = 'spaces/SPACE_NAME';
// Filter membership by type (HUMAN or BOT) or role (ROLE_MEMBER or
// ROLE_MANAGER)
const filter = 'member.type = "HUMAN"';
// Iterate through the response pages using page tokens
let responsePage;
let pageToken = null;
do {
// Request response pages
responsePage = Chat.Spaces.Members.list(parent, {
filter: filter,
pageSize: 10,
pageToken: pageToken
});
// Handle response pages
if (responsePage.memberships) {
responsePage.memberships.forEach((membership) => console.log(membership));
}
// Update the page token to the next one
pageToken = responsePage.nextPageToken;
} while (pageToken);
}
// [END chat_list_memberships_user_cred]
// [START chat_list_messages_user_cred]
/**
* This sample shows how to list messages with user credential
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.messages.readonly'
* referenced in the manifest file (appsscript.json).
*/
function listMessagesUserCred() {
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME here
const parent = 'spaces/SPACE_NAME';
// Iterate through the response pages using page tokens
let responsePage;
let pageToken = null;
do {
// Request response pages
responsePage = Chat.Spaces.Messages.list(parent, {
pageSize: 10,
pageToken: pageToken
});
// Handle response pages
if (responsePage.messages) {
responsePage.messages.forEach((message) => console.log(message));
}
// Update the page token to the next one
pageToken = responsePage.nextPageToken;
} while (pageToken);
}
// [END chat_list_messages_user_cred]
// [START chat_list_spaces_app_cred]
/**
* This sample shows how to list spaces with app credential
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.bot'
* used by service accounts.
*/
function listSpacesAppCred() {
// Initialize request argument(s)
// Filter spaces by space type (SPACE or GROUP_CHAT or DIRECT_MESSAGE)
const filter = 'space_type = "SPACE"';
// Iterate through the response pages using page tokens
let responsePage;
let pageToken = null;
do {
// Request response pages
responsePage = Chat.Spaces.list({
filter: filter,
pageSize: 10,
pageToken: pageToken
}, getHeaderWithAppCredentials());
// Handle response pages
if (responsePage.spaces) {
responsePage.spaces.forEach((space) => console.log(space));
}
// Update the page token to the next one
pageToken = responsePage.nextPageToken;
} while (pageToken);
}
// [END chat_list_spaces_app_cred]
// [START chat_list_spaces_user_cred]
/**
* This sample shows how to list spaces with user credential
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.spaces.readonly'
* referenced in the manifest file (appsscript.json).
*/
function listSpacesUserCred() {
// Initialize request argument(s)
// Filter spaces by space type (SPACE or GROUP_CHAT or DIRECT_MESSAGE)
const filter = 'space_type = "SPACE"';
// Iterate through the response pages using page tokens
let responsePage;
let pageToken = null;
do {
// Request response pages
responsePage = Chat.Spaces.list({
filter: filter,
pageSize: 10,
pageToken: pageToken
});
// Handle response pages
if (responsePage.spaces) {
responsePage.spaces.forEach((space) => console.log(space));
}
// Update the page token to the next one
pageToken = responsePage.nextPageToken;
} while (pageToken);
}
// [END chat_list_spaces_user_cred]
// [START chat_update_message_app_cred]
/**
* This sample shows how to update a message with app credential
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.bot'
* used by service accounts.
*/
function updateMessageAppCred() {
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME and MESSAGE_NAME here
const name = 'spaces/SPACE_NAME/messages/MESSAGE_NAME';
const message = {
text: 'Updated with app credential!'
};
// The field paths to update. Separate multiple values with commas or use
// `*` to update all field paths.
const updateMask = 'text';
// Make the request
const response = Chat.Spaces.Messages.patch(message, name, {
updateMask: updateMask
}, getHeaderWithAppCredentials());
// Handle the response
console.log(response);
}
// [END chat_update_message_app_cred]
// [START chat_update_message_user_cred]
/**
* This sample shows how to update a message with user credential
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.messages'
* referenced in the manifest file (appsscript.json).
*/
function updateMessageUserCred() {
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME and MESSAGE_NAME here
const name = 'spaces/SPACE_NAME/messages/MESSAGE_NAME';
const message = {
text: 'Updated with user credential!'
};
// The field paths to update. Separate multiple values with commas or use
// `*` to update all field paths.
const updateMask = 'text';
// Make the request
const response = Chat.Spaces.Messages.patch(message, name, {
updateMask: updateMask
});
// Handle the response
console.log(response);
}
// [END chat_update_message_user_cred]
// [START chat_update_space_user_cred]
/**
* This sample shows how to update a space with user credential
*
* It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.spaces'
* referenced in the manifest file (appsscript.json).
*/
function updateSpaceUserCred() {
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME here
const name = 'spaces/SPACE_NAME';
const space = {
displayName: 'New space display name'
};
// The field paths to update. Separate multiple values with commas or use
// `*` to update all field paths.
const updateMask = 'displayName';
// Make the request
const response = Chat.Spaces.patch(space, name, {
updateMask: updateMask
});
// Handle the response
console.log(response);
}
// [END chat_update_space_user_cred]