Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions components/google_chat/package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand All @@ -13,6 +13,6 @@
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^1.5.1"
"@pipedream/platform": "^3.1.0"
}
}
108 changes: 108 additions & 0 deletions components/google_chat/sources/common/base.mjs
Original file line number Diff line number Diff line change
@@ -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();
},
};
Original file line number Diff line number Diff line change
@@ -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)}`;
},
},
};
Original file line number Diff line number Diff line change
@@ -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(`<users/${this.memberId}>`);
},
getSummary(message) {
return `New Mention in Message: ${message.formattedText.slice(0, 50)}`;
},
},
};
Original file line number Diff line number Diff line change
@@ -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)}`;
},
},
};
10 changes: 4 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading