diff --git a/components/google_chat/package.json b/components/google_chat/package.json index 41abbd08f7579..6fd6411578f8b 100644 --- a/components/google_chat/package.json +++ b/components/google_chat/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/google_chat", - "version": "0.0.2", + "version": "0.1.0", "description": "Pipedream Google Chat Components", "main": "google_chat.app.mjs", "keywords": [ @@ -13,6 +13,6 @@ "access": "public" }, "dependencies": { - "@pipedream/platform": "^1.5.1" + "@pipedream/platform": "^3.1.0" } } diff --git a/components/google_chat/sources/common/base.mjs b/components/google_chat/sources/common/base.mjs new file mode 100644 index 0000000000000..ef825d372aed9 --- /dev/null +++ b/components/google_chat/sources/common/base.mjs @@ -0,0 +1,108 @@ +import googleChat from "../../google_chat.app.mjs"; +import { + DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, ConfigurationError, +} from "@pipedream/platform"; + +export default { + props: { + googleChat, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, + }, + }, + spaceId: { + propDefinition: [ + googleChat, + "spaceId", + ], + }, + }, + methods: { + _getLastTs() { + return this.db.get("lastTs"); + }, + _setLastTs(ts) { + this.db.set("lastTs", ts); + }, + async *paginateMessages(max) { + const lastTs = this._getLastTs(); + let next, count = 0; + const params = { + filter: lastTs + ? `createTime > "${lastTs}"` + : undefined, + orderBy: "createTime DESC", + }; + + do { + const { + messages, nextPageToken, + } = await this.googleChat.listMessages({ + spaceId: this.spaceId, + params, + }); + + if (!messages.length) { + return; + } + + for (const message of messages) { + yield message; + if (max && ++count >= max) { + return; + } + } + + next = nextPageToken; + params.pageToken = next; + } while (next); + }, + async getPaginatedMessages(max) { + const messages = []; + for await (const message of this.paginateMessages(max)) { + messages.push(message); + } + return messages; + }, + isRelevant() { + return true; + }, + generateMeta(message) { + return { + id: message.name, + summary: this.getSummary(message), + ts: Date.parse(message.createTime), + }; + }, + async processEvent(max) { + const messages = await this.getPaginatedMessages(max); + + const relevantMessages = messages.filter((message) => this.isRelevant(message)); + + if (!relevantMessages?.length) { + return; + } + + this._setLastTs(relevantMessages[0].createTime); + + relevantMessages.reverse().forEach((message) => { + const meta = this.generateMeta(message); + this.$emit(message, meta); + }); + }, + getSummary() { + throw new ConfigurationError("getSummary is not implemented"); + }, + }, + hooks: { + async deploy() { + await this.processEvent(25); + }, + }, + async run() { + await this.processEvent(); + }, +}; diff --git a/components/google_chat/sources/new-command-used/new-command-used.mjs b/components/google_chat/sources/new-command-used/new-command-used.mjs new file mode 100644 index 0000000000000..0a724ab3939c9 --- /dev/null +++ b/components/google_chat/sources/new-command-used/new-command-used.mjs @@ -0,0 +1,31 @@ +import common from "../common/base.mjs"; + +export default { + ...common, + key: "google_chat-new-command-used", + name: "New Command Used", + description: "Emit new event when a new command is used in a space. [See the documentation](https://developers.google.com/workspace/chat/api/reference/rest/v1/spaces.messages/list)", + version: "0.0.1", + type: "source", + dedupe: "unique", + props: { + ...common.props, + command: { + type: "string", + label: "Command", + description: "The command to emit events for.", + }, + }, + methods: { + ...common.methods, + isRelevant(message) { + const command = this.command.startsWith("/") + ? this.command + : `/${this.command}`; + return message.text.startsWith(command); + }, + getSummary(message) { + return `New Command Used in Message: ${message.text.slice(0, 50)}`; + }, + }, +}; diff --git a/components/google_chat/sources/new-mention-received/new-mention-received.mjs b/components/google_chat/sources/new-mention-received/new-mention-received.mjs new file mode 100644 index 0000000000000..a6a85d666938a --- /dev/null +++ b/components/google_chat/sources/new-mention-received/new-mention-received.mjs @@ -0,0 +1,32 @@ +import common from "../common/base.mjs"; + +export default { + ...common, + key: "google_chat-new-mention-received", + name: "New Mention Received", + description: "Emit new event when a new mention is received in a space. [See the documentation](https://developers.google.com/workspace/chat/api/reference/rest/v1/spaces.messages/list)", + version: "0.0.1", + type: "source", + dedupe: "unique", + props: { + ...common.props, + memberId: { + propDefinition: [ + common.props.googleChat, + "memberId", + (c) => ({ + spaceId: c.spaceId, + }), + ], + }, + }, + methods: { + ...common.methods, + isRelevant(message) { + return message.formattedText.includes(``); + }, + getSummary(message) { + return `New Mention in Message: ${message.formattedText.slice(0, 50)}`; + }, + }, +}; diff --git a/components/google_chat/sources/new-message-in-space/new-message-in-space.mjs b/components/google_chat/sources/new-message-in-space/new-message-in-space.mjs new file mode 100644 index 0000000000000..0683d2a3c9e12 --- /dev/null +++ b/components/google_chat/sources/new-message-in-space/new-message-in-space.mjs @@ -0,0 +1,17 @@ +import common from "../common/base.mjs"; + +export default { + ...common, + key: "google_chat-new-message-in-space", + name: "New Message in Space", + description: "Emit new event when a new message is posted in a space. [See the documentation](https://developers.google.com/workspace/chat/api/reference/rest/v1/spaces.messages/list)", + version: "0.0.1", + type: "source", + dedupe: "unique", + methods: { + ...common.methods, + getSummary(message) { + return `New Message: ${message.text.slice(0, 50)}`; + }, + }, +}; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fc97dde92332f..1d9c464501a9d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1575,8 +1575,7 @@ importers: specifier: ^3.0.3 version: 3.0.3 - components/bitget: - specifiers: {} + components/bitget: {} components/bitly: dependencies: @@ -5495,8 +5494,8 @@ importers: components/google_chat: dependencies: '@pipedream/platform': - specifier: ^1.5.1 - version: 1.6.6 + specifier: ^3.1.0 + version: 3.1.0 components/google_chat_developer_app: {} @@ -6905,8 +6904,7 @@ importers: specifier: ^1.5.1 version: 1.6.6 - components/joggai: - specifiers: {} + components/joggai: {} components/join: {}