|
| 1 | +/** |
| 2 | + * Copyright 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 | + * https://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 | +// [START chat_post_message_with_user_credentials] |
| 18 | +/** |
| 19 | + * Posts a new message to the specified space on behalf of the user. |
| 20 | + * @param {string} spaceName The resource name of the space. |
| 21 | + */ |
| 22 | +function postMessageWithUserCredentials(spaceName) { |
| 23 | + try { |
| 24 | + const message = {'text': 'Hello world!'}; |
| 25 | + Chat.Spaces.Messages.create(message, spaceName); |
| 26 | + } catch (err) { |
| 27 | + // TODO (developer) - Handle exception |
| 28 | + console.log('Failed to create message with error %s', err.message); |
| 29 | + } |
| 30 | +} |
| 31 | +// [END chat_post_message_with_user_credentials] |
| 32 | + |
| 33 | +// [START chat_post_message_with_app_credentials] |
| 34 | +/** |
| 35 | + * Posts a new message to the specified space on behalf of the app. |
| 36 | + * @param {string} spaceName The resource name of the space. |
| 37 | + */ |
| 38 | +function postMessageWithAppCredentials(spaceName) { |
| 39 | + try { |
| 40 | + // See https://developers.google.com/chat/api/guides/auth/service-accounts |
| 41 | + // for details on how to obtain a service account OAuth token. |
| 42 | + const appToken = getToken_(); |
| 43 | + const message = {'text': 'Hello world!'}; |
| 44 | + Chat.Spaces.Messages.create( |
| 45 | + message, |
| 46 | + spaceName, |
| 47 | + {}, |
| 48 | + // Authenticate with the service account token. |
| 49 | + {'Authorization': 'Bearer ' + appToken}); |
| 50 | + } catch (err) { |
| 51 | + // TODO (developer) - Handle exception |
| 52 | + console.log('Failed to create message with error %s', err.message); |
| 53 | + } |
| 54 | +} |
| 55 | +// [END chat_post_message_with_app_credentials] |
| 56 | + |
| 57 | +// [START chat_get_space] |
| 58 | +/** |
| 59 | + * Gets information about a Chat space. |
| 60 | + * @param {string} spaceName The resource name of the space. |
| 61 | + */ |
| 62 | +function getSpace(spaceName) { |
| 63 | + try { |
| 64 | + const space = Chat.Spaces.get(spaceName); |
| 65 | + console.log('Space display name: %s', space.displayName); |
| 66 | + console.log('Space type: %s', space.spaceType); |
| 67 | + } catch (err) { |
| 68 | + // TODO (developer) - Handle exception |
| 69 | + console.log('Failed to get space with error %s', err.message); |
| 70 | + } |
| 71 | +} |
| 72 | +// [END chat_get_space] |
| 73 | + |
| 74 | +// [START chat_create_space] |
| 75 | +/** |
| 76 | + * Creates a new Chat space. |
| 77 | + */ |
| 78 | +function createSpace() { |
| 79 | + try { |
| 80 | + const space = {'displayName': 'New Space', 'spaceType': 'SPACE'}; |
| 81 | + Chat.Spaces.create(space); |
| 82 | + } catch (err) { |
| 83 | + // TODO (developer) - Handle exception |
| 84 | + console.log('Failed to create space with error %s', err.message); |
| 85 | + } |
| 86 | +} |
| 87 | +// [END chat_create_space] |
| 88 | + |
| 89 | +// [START chat_list_memberships] |
| 90 | +/** |
| 91 | + * Lists all the members of a Chat space. |
| 92 | + * @param {string} spaceName The resource name of the space. |
| 93 | + */ |
| 94 | +function listMemberships(spaceName) { |
| 95 | + let response; |
| 96 | + let pageToken = null; |
| 97 | + try { |
| 98 | + do { |
| 99 | + response = Chat.Spaces.Members.list(spaceName, { |
| 100 | + pageSize: 10, |
| 101 | + pageToken: pageToken |
| 102 | + }); |
| 103 | + if (!response.memberships || response.memberships.length === 0) { |
| 104 | + pageToken = response.nextPageToken; |
| 105 | + continue; |
| 106 | + } |
| 107 | + response.memberships.forEach((membership) => console.log( |
| 108 | + 'Member resource name: %s (type: %s)', |
| 109 | + membership.name, |
| 110 | + membership.member.type)); |
| 111 | + pageToken = response.nextPageToken; |
| 112 | + } while (pageToken); |
| 113 | + } catch (err) { |
| 114 | + // TODO (developer) - Handle exception |
| 115 | + console.log('Failed with error %s', err.message); |
| 116 | + } |
| 117 | +} |
| 118 | +// [END chat_list_memberships] |
0 commit comments