Skip to content

Commit 3f93f80

Browse files
authored
Merging pull request #15508
* Added actions * Done requests changes * Done requests changes
1 parent 6ecab27 commit 3f93f80

File tree

7 files changed

+211
-20
lines changed

7 files changed

+211
-20
lines changed

components/zulip/.gitignore

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import app from "../../zulip.app.mjs";
2+
import { ConfigurationError } from "@pipedream/platform";
3+
4+
export default {
5+
key: "zulip-send-message",
6+
name: "Send Message",
7+
description: "Send a direct or channel message. [See the documentation](https://zulip.com/api/send-message)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
app,
12+
type: {
13+
propDefinition: [
14+
app,
15+
"type",
16+
],
17+
},
18+
userId: {
19+
propDefinition: [
20+
app,
21+
"userId",
22+
],
23+
},
24+
channelId: {
25+
propDefinition: [
26+
app,
27+
"channelId",
28+
],
29+
},
30+
topic: {
31+
propDefinition: [
32+
app,
33+
"topic",
34+
(c) => ({
35+
channelId: c.channelId,
36+
}),
37+
],
38+
},
39+
content: {
40+
propDefinition: [
41+
app,
42+
"content",
43+
],
44+
},
45+
},
46+
47+
async run({ $ }) {
48+
const isDirect = this.type === "direct" || this.type === "private";
49+
const isChannel = this.type === "channel" || this.type === "stream";
50+
51+
if (isDirect && (!this.userId || this.userId.length === 0)) {
52+
throw new ConfigurationError("You must provide at least one User ID when the type is 'direct' or 'private'.");
53+
}
54+
55+
if (isChannel && !this.channelId) {
56+
throw new ConfigurationError("You must provide a Channel ID when the type is 'channel' or 'stream'.");
57+
}
58+
59+
if ((isDirect && this.channelId) || (isChannel && this.userId?.length > 0)) {
60+
throw new ConfigurationError(`Invalid input: '${this.type}' messages require only ${isDirect
61+
? "User ID(s)"
62+
: "a Channel ID"}.`);
63+
}
64+
const response = await this.app.sendMessage({
65+
$,
66+
params: {
67+
type: this.type,
68+
to: this.channelId || `[${this.userId.join(",")}]`,
69+
topic: this.topic,
70+
content: this.content,
71+
},
72+
});
73+
$.export("$summary", `Sucessfully sent message with ID ${response.id}`);
74+
return response;
75+
},
76+
};

components/zulip/app/zulip.app.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default {
2+
TYPE_OPTIONS: [
3+
"direct",
4+
"channel",
5+
"stream",
6+
"private",
7+
],
8+
};

components/zulip/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
{
22
"name": "@pipedream/zulip",
3-
"version": "0.0.2",
3+
"version": "0.1.0",
44
"description": "Pipedream Zulip Components",
5-
"main": "dist/app/zulip.app.mjs",
5+
"main": "zulip.app.mjs",
66
"keywords": [
77
"pipedream",
88
"zulip"
99
],
10-
"files": ["dist"],
1110
"homepage": "https://pipedream.com/apps/zulip",
1211
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1312
"publishConfig": {
1413
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3"
1517
}
1618
}

components/zulip/zulip.app.mjs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import { axios } from "@pipedream/platform";
2+
import constants from "./common/constants.mjs";
3+
4+
export default {
5+
type: "app",
6+
app: "zulip",
7+
propDefinitions: {
8+
type: {
9+
type: "string",
10+
label: "Type",
11+
description: "The type of message to be sent",
12+
options: constants.TYPE_OPTIONS,
13+
},
14+
userId: {
15+
type: "integer[]",
16+
label: "User ID",
17+
description: "ID of the Users recipients of the message. Required when type is `direct` or `private`",
18+
optional: true,
19+
async options() {
20+
const response = await this.getUsers();
21+
const usersIds = response.members;
22+
return usersIds.map(({
23+
user_id, full_name,
24+
}) => ({
25+
label: full_name,
26+
value: user_id,
27+
}));
28+
},
29+
},
30+
channelId: {
31+
type: "integer",
32+
label: "Channel ID",
33+
description: "ID of the Channel recipient of the message. Required when type is `channel` or `stream`",
34+
optional: true,
35+
async options() {
36+
const response = await this.getChannels();
37+
const channelsIds = response.streams;
38+
return channelsIds.map(({
39+
stream_id, name,
40+
}) => ({
41+
label: name,
42+
value: stream_id,
43+
}));
44+
},
45+
},
46+
topic: {
47+
type: "string",
48+
label: "Topic",
49+
description: "The topic of the message. Required when type is `channel` or `stream`",
50+
optional: true,
51+
async options({ channelId }) {
52+
const response = await this.getTopics({
53+
channelId,
54+
});
55+
const topics = response.topics;
56+
return topics.map(({ name }) => ({
57+
label: name,
58+
value: name,
59+
}));
60+
},
61+
},
62+
content: {
63+
type: "string",
64+
label: "Content",
65+
description: "The content of the message",
66+
},
67+
},
68+
methods: {
69+
_baseUrl() {
70+
return `https://${this.$auth.domain}.zulipchat.com/api/v1`;
71+
},
72+
async _makeRequest(opts = {}) {
73+
const {
74+
$ = this,
75+
path,
76+
auth,
77+
...otherOpts
78+
} = opts;
79+
return axios($, {
80+
...otherOpts,
81+
url: this._baseUrl() + path,
82+
auth: {
83+
username: `${this.$auth.email}`,
84+
password: `${this.$auth.api_key}`,
85+
...auth,
86+
},
87+
});
88+
},
89+
async getUsers(args = {}) {
90+
return this._makeRequest({
91+
path: "/users",
92+
...args,
93+
});
94+
},
95+
async getChannels(args = {}) {
96+
return this._makeRequest({
97+
path: "/streams",
98+
...args,
99+
});
100+
},
101+
async getTopics({
102+
channelId, ...args
103+
}) {
104+
return this._makeRequest({
105+
path: `/users/me/${channelId}/topics`,
106+
...args,
107+
});
108+
},
109+
async sendMessage(args = {}) {
110+
return this._makeRequest({
111+
path: "/messages",
112+
method: "post",
113+
...args,
114+
});
115+
},
116+
},
117+
};

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)