UNPKG

@vulture916/piece-google-chat

Version:

Google Chat piece for ActivePieces - send messages to Google Chat spaces

105 lines 4.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sendChatMessage = void 0; const tslib_1 = require("tslib"); const pieces_framework_1 = require("@activepieces/pieces-framework"); const pieces_common_1 = require("@activepieces/pieces-common"); const __1 = require("../../"); exports.sendChatMessage = (0, pieces_framework_1.createAction)({ name: 'send_chat_message', displayName: 'Send Chat Message', description: 'Send a message to a Google Chat space', auth: __1.googleChatAuth, props: { space: pieces_framework_1.Property.Dropdown({ displayName: 'Space', description: 'Select the Google Chat space to send the message to', required: true, refreshers: ['auth'], options: (_a) => tslib_1.__awaiter(void 0, [_a], void 0, function* ({ auth }) { if (!auth) { return { disabled: true, placeholder: 'Please authenticate with Google Chat first', options: [], }; } try { const oauth2Auth = auth; const response = yield pieces_common_1.httpClient.sendRequest({ method: pieces_common_1.HttpMethod.GET, url: 'https://chat.googleapis.com/v1/spaces', headers: { Authorization: `Bearer ${oauth2Auth.access_token}`, 'Content-Type': 'application/json', }, }); const spaces = response.body.spaces || []; return { options: spaces.map((space) => ({ label: space.displayName || space.name || 'Unnamed Space', value: space.name, })), }; } catch (error) { console.error('Error fetching spaces:', error); return { disabled: false, placeholder: 'Error fetching spaces. Enter space ID manually.', options: [], }; } }), }), manualSpaceId: pieces_framework_1.Property.ShortText({ displayName: 'Manual Space ID', description: 'If dropdown fails, enter space ID manually (format: spaces/SPACE_ID)', required: false, }), message: pieces_framework_1.Property.LongText({ displayName: 'Message', description: 'The message to send', required: true, }), thread: pieces_framework_1.Property.ShortText({ displayName: 'Thread Name', description: 'Optional: The thread to reply to (format: spaces/SPACE_ID/threads/THREAD_ID)', required: false, }), }, run(context) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const { space, manualSpaceId, message, thread } = context.propsValue; const oauth2Auth = context.auth; const accessToken = oauth2Auth.access_token; // Use either selected space or manually entered space ID const spaceId = manualSpaceId || space; // Prepare the API URL const apiUrl = thread ? `https://chat.googleapis.com/v1/${thread}/messages` : `https://chat.googleapis.com/v1/${spaceId}/messages`; // Prepare the message payload const payload = { text: message, }; try { const response = yield pieces_common_1.httpClient.sendRequest({ method: pieces_common_1.HttpMethod.POST, url: apiUrl, body: payload, headers: { Authorization: `Bearer ${accessToken}`, 'Content-Type': 'application/json', }, }); return response.body; } catch (error) { console.error('Error sending message to Google Chat:', error); throw error; } }); }, }); //# sourceMappingURL=send-message.js.map