Skip to content

Commit bea9956

Browse files
authored
New Components - google_chat (#17368)
* new sources * pnpm-lock.yaml * pnpm-lock.yaml * pnpm-lock.yaml * fix pagination * update * update
1 parent c889659 commit bea9956

File tree

6 files changed

+192
-4
lines changed

6 files changed

+192
-4
lines changed

components/google_chat/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/google_chat",
3-
"version": "0.0.2",
3+
"version": "0.1.0",
44
"description": "Pipedream Google Chat Components",
55
"main": "google_chat.app.mjs",
66
"keywords": [
@@ -13,6 +13,6 @@
1313
"access": "public"
1414
},
1515
"dependencies": {
16-
"@pipedream/platform": "^1.5.1"
16+
"@pipedream/platform": "^3.1.0"
1717
}
1818
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import googleChat from "../../google_chat.app.mjs";
2+
import {
3+
DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, ConfigurationError,
4+
} from "@pipedream/platform";
5+
6+
export default {
7+
props: {
8+
googleChat,
9+
db: "$.service.db",
10+
timer: {
11+
type: "$.interface.timer",
12+
default: {
13+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
14+
},
15+
},
16+
spaceId: {
17+
propDefinition: [
18+
googleChat,
19+
"spaceId",
20+
],
21+
},
22+
},
23+
methods: {
24+
_getLastTs() {
25+
return this.db.get("lastTs");
26+
},
27+
_setLastTs(ts) {
28+
this.db.set("lastTs", ts);
29+
},
30+
async *paginateMessages(max) {
31+
const lastTs = this._getLastTs();
32+
let next, count = 0;
33+
const params = {
34+
filter: lastTs
35+
? `createTime > "${lastTs}"`
36+
: undefined,
37+
orderBy: "createTime DESC",
38+
};
39+
40+
do {
41+
const {
42+
messages, nextPageToken,
43+
} = await this.googleChat.listMessages({
44+
spaceId: this.spaceId,
45+
params,
46+
});
47+
48+
if (!messages.length) {
49+
return;
50+
}
51+
52+
for (const message of messages) {
53+
yield message;
54+
if (max && ++count >= max) {
55+
return;
56+
}
57+
}
58+
59+
next = nextPageToken;
60+
params.pageToken = next;
61+
} while (next);
62+
},
63+
async getPaginatedMessages(max) {
64+
const messages = [];
65+
for await (const message of this.paginateMessages(max)) {
66+
messages.push(message);
67+
}
68+
return messages;
69+
},
70+
isRelevant() {
71+
return true;
72+
},
73+
generateMeta(message) {
74+
return {
75+
id: message.name,
76+
summary: this.getSummary(message),
77+
ts: Date.parse(message.createTime),
78+
};
79+
},
80+
async processEvent(max) {
81+
const messages = await this.getPaginatedMessages(max);
82+
83+
const relevantMessages = messages.filter((message) => this.isRelevant(message));
84+
85+
if (!relevantMessages?.length) {
86+
return;
87+
}
88+
89+
this._setLastTs(relevantMessages[0].createTime);
90+
91+
relevantMessages.reverse().forEach((message) => {
92+
const meta = this.generateMeta(message);
93+
this.$emit(message, meta);
94+
});
95+
},
96+
getSummary() {
97+
throw new ConfigurationError("getSummary is not implemented");
98+
},
99+
},
100+
hooks: {
101+
async deploy() {
102+
await this.processEvent(25);
103+
},
104+
},
105+
async run() {
106+
await this.processEvent();
107+
},
108+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import common from "../common/base.mjs";
2+
3+
export default {
4+
...common,
5+
key: "google_chat-new-command-used",
6+
name: "New Command Used",
7+
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)",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
props: {
12+
...common.props,
13+
command: {
14+
type: "string",
15+
label: "Command",
16+
description: "The command to emit events for.",
17+
},
18+
},
19+
methods: {
20+
...common.methods,
21+
isRelevant(message) {
22+
const command = this.command.startsWith("/")
23+
? this.command
24+
: `/${this.command}`;
25+
return message.text.startsWith(command);
26+
},
27+
getSummary(message) {
28+
return `New Command Used in Message: ${message.text.slice(0, 50)}`;
29+
},
30+
},
31+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import common from "../common/base.mjs";
2+
3+
export default {
4+
...common,
5+
key: "google_chat-new-mention-received",
6+
name: "New Mention Received",
7+
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)",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
props: {
12+
...common.props,
13+
memberId: {
14+
propDefinition: [
15+
common.props.googleChat,
16+
"memberId",
17+
(c) => ({
18+
spaceId: c.spaceId,
19+
}),
20+
],
21+
},
22+
},
23+
methods: {
24+
...common.methods,
25+
isRelevant(message) {
26+
return message.formattedText.includes(`<users/${this.memberId}>`);
27+
},
28+
getSummary(message) {
29+
return `New Mention in Message: ${message.formattedText.slice(0, 50)}`;
30+
},
31+
},
32+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import common from "../common/base.mjs";
2+
3+
export default {
4+
...common,
5+
key: "google_chat-new-message-in-space",
6+
name: "New Message in Space",
7+
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)",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
methods: {
12+
...common.methods,
13+
getSummary(message) {
14+
return `New Message: ${message.text.slice(0, 50)}`;
15+
},
16+
},
17+
};

pnpm-lock.yaml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)