Skip to content

Commit 0369293

Browse files
authored
New Components - botstar (#16757)
* new components * pnpm-lock.yaml * pnpm-lock.yaml * package.json * pnpm-lock.yaml
1 parent f9e5338 commit 0369293

File tree

11 files changed

+318
-15
lines changed

11 files changed

+318
-15
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import botstar from "../../botstar.app.mjs";
2+
3+
export default {
4+
key: "botstar-send-message",
5+
name: "Send Message",
6+
description: "Send a message to a user via BotStar. [See the docs](https://apis.botstar.com/docs/#/Messages/post_messages)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
botstar,
11+
recipientId: {
12+
type: "string",
13+
label: "Recipient ID",
14+
description: "The user ID to send the message to. To find a user ID, login to app.botstar.com > Report & Insight > Audience > click on a chat user The User ID is displayed under the information on the left of the screen",
15+
},
16+
message: {
17+
type: "string",
18+
label: "Message",
19+
description: "The text message to send",
20+
},
21+
},
22+
async run({ $ }) {
23+
const response = await this.botstar.sendMessage({
24+
$,
25+
data: {
26+
recipient: {
27+
id: this.recipientId,
28+
},
29+
message: {
30+
text: this.message,
31+
},
32+
},
33+
});
34+
$.export("$summary", `Sent message to user ${this.recipientId}`);
35+
return response;
36+
},
37+
};

components/botstar/botstar.app.mjs

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,107 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "botstar",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
botId: {
8+
type: "string",
9+
label: "Bot ID",
10+
description: "The ID of the bot to use for the request",
11+
async options() {
12+
const bots = await this.listBots();
13+
return bots?.map(({
14+
id: value, name: label,
15+
}) => ({
16+
label,
17+
value,
18+
})) || [];
19+
},
20+
},
21+
entityId: {
22+
type: "string",
23+
label: "CMS Entity ID",
24+
description: "The ID of the CMS entity to use for the request",
25+
async options({ botId }) {
26+
const cmsEntities = await this.listCmsEntities({
27+
botId,
28+
});
29+
return cmsEntities?.map(({
30+
id: value, name: label,
31+
}) => ({
32+
label,
33+
value,
34+
})) || [];
35+
},
36+
},
37+
},
538
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
39+
_baseUrl() {
40+
return "https://apis.botstar.com/v1";
41+
},
42+
_makeRequest({
43+
$ = this, path, ...opts
44+
}) {
45+
return axios($, {
46+
url: `${this._baseUrl()}${path}`,
47+
headers: {
48+
Authorization: `Bearer ${this.$auth.access_token}`,
49+
},
50+
...opts,
51+
});
52+
},
53+
listBots(opts = {}) {
54+
return this._makeRequest({
55+
path: "/bots",
56+
...opts,
57+
});
58+
},
59+
listCmsEntities({
60+
botId, ...opts
61+
}) {
62+
return this._makeRequest({
63+
path: `/bots/${botId}/cms_entities`,
64+
...opts,
65+
});
66+
},
67+
listCmsEntityItems({
68+
botId, entityId, ...opts
69+
}) {
70+
return this._makeRequest({
71+
path: `/bots/${botId}/cms_entities/${entityId}/items`,
72+
...opts,
73+
});
74+
},
75+
sendMessage(opts = {}) {
76+
return this._makeRequest({
77+
method: "POST",
78+
path: "/messages",
79+
...opts,
80+
});
81+
},
82+
async *paginate({
83+
fn, args, max,
84+
}) {
85+
args = {
86+
...args,
87+
params: {
88+
...args?.params,
89+
page: 1,
90+
limit: 100,
91+
},
92+
};
93+
let total, count = 1;
94+
do {
95+
const items = await fn(args);
96+
for (const item of items) {
97+
yield item;
98+
if (max && ++count >= max) {
99+
return;
100+
}
101+
}
102+
total = items?.length;
103+
args.params.page++;
104+
} while (total === args.params.limit);
9105
},
10106
},
11107
};

components/botstar/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/botstar",
3-
"version": "0.6.0",
3+
"version": "0.7.0",
44
"description": "Pipedream botstar Components",
55
"main": "botstar.app.mjs",
66
"keywords": [
@@ -13,6 +13,6 @@
1313
"access": "public"
1414
},
1515
"dependencies": {
16-
"@pipedream/platform": "^3.0.0"
16+
"@pipedream/platform": "^3.0.3"
1717
}
1818
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import botstar from "../../botstar.app.mjs";
2+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
3+
4+
export default {
5+
props: {
6+
botstar,
7+
timer: {
8+
type: "$.interface.timer",
9+
default: {
10+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
11+
},
12+
},
13+
},
14+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import common from "../common/base.mjs";
2+
import sampleEmit from "./test-event.mjs";
3+
4+
export default {
5+
...common,
6+
key: "botstar-new-bot-created",
7+
name: "New Bot Created",
8+
description: "Emit new event when a new bot is created in BotStar. [See the documentation](https://apis.botstar.com/docs/#/Bots/get_bots_)",
9+
version: "0.0.1",
10+
type: "source",
11+
dedupe: "unique",
12+
methods: {
13+
generateMeta(bot) {
14+
return {
15+
id: bot.id,
16+
summary: `New Bot: ${bot.name}`,
17+
ts: Date.now(),
18+
};
19+
},
20+
},
21+
async run() {
22+
const bots = await this.botstar.listBots();
23+
for (const bot of bots) {
24+
const meta = this.generateMeta(bot);
25+
this.$emit(bot, meta);
26+
}
27+
},
28+
sampleEmit,
29+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
"id": "s13d855e0-366f-11f0-8b00-d5c347dbdc20",
3+
"name": "Sample Tutorial Bot",
4+
"team_name": "Team"
5+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import common from "../common/base.mjs";
2+
import sampleEmit from "./test-event.mjs";
3+
4+
export default {
5+
...common,
6+
key: "botstar-new-cms-entity-created",
7+
name: "New CMS Entity Created",
8+
description: "Emit new event when a new CMS entity is created in a BotStar bot. [See the documentation](https://apis.botstar.com/docs/#/CMS%20Entities/get_bots__botId__cms_entities)",
9+
version: "0.0.1",
10+
type: "source",
11+
dedupe: "unique",
12+
props: {
13+
...common.props,
14+
botId: {
15+
propDefinition: [
16+
common.props.botstar,
17+
"botId",
18+
],
19+
},
20+
},
21+
methods: {
22+
generateMeta(entity) {
23+
return {
24+
id: entity.id,
25+
summary: `New CMS Entity: ${entity.name}`,
26+
ts: Date.now(),
27+
};
28+
},
29+
},
30+
async run() {
31+
const entities = await this.botstar.listCmsEntities({
32+
botId: this.botId,
33+
});
34+
if (!entities?.length) {
35+
return;
36+
}
37+
for (const entity of entities.reverse()) {
38+
const meta = this.generateMeta(entity);
39+
this.$emit(entity, meta);
40+
}
41+
},
42+
sampleEmit,
43+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export default {
2+
"id": "s13d85634-366f-11f0-8b00-d5c347dbdc20",
3+
"name": "Coffee",
4+
"fields": [
5+
{
6+
"unique_name": "name",
7+
"name": "Name",
8+
"data_type": "text"
9+
},
10+
{
11+
"unique_name": "status",
12+
"name": "Status",
13+
"data_type": "single_option"
14+
},
15+
{
16+
"unique_name": "short_description",
17+
"name": "Short Description",
18+
"data_type": "free_text"
19+
},
20+
{
21+
"unique_name": "photo",
22+
"name": "Photo",
23+
"data_type": "image"
24+
}
25+
]
26+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import common from "../common/base.mjs";
2+
import sampleEmit from "./test-event.mjs";
3+
4+
export default {
5+
...common,
6+
key: "botstar-new-cms-entity-item-created",
7+
name: "New CMS Entity Item Created",
8+
description: "Emit new event when a new item is created in a CMS entity in BotStar. [See the documentation](https://apis.botstar.com/docs/#/CMS%20Entity%20Items/get_bots__botId__cms_entities__entityId__items)",
9+
version: "0.0.1",
10+
type: "source",
11+
dedupe: "unique",
12+
props: {
13+
...common.props,
14+
botId: {
15+
propDefinition: [
16+
common.props.botstar,
17+
"botId",
18+
],
19+
},
20+
entityId: {
21+
propDefinition: [
22+
common.props.botstar,
23+
"entityId",
24+
(c) => ({
25+
botId: c.botId,
26+
}),
27+
],
28+
},
29+
},
30+
methods: {
31+
generateMeta(item) {
32+
return {
33+
id: item._id,
34+
summary: `New CMS Entity Item: ${item.name || item.id}`,
35+
ts: Date.now(),
36+
};
37+
},
38+
},
39+
async run() {
40+
const results = this.botstar.paginate({
41+
fn: this.botstar.listCmsEntityItems,
42+
args: {
43+
botId: this.botId,
44+
entityId: this.entityId,
45+
},
46+
});
47+
for await (const item of results) {
48+
const meta = this.generateMeta(item);
49+
this.$emit(item, meta);
50+
}
51+
},
52+
sampleEmit,
53+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default {
2+
"name": "Cappuccino",
3+
"status": "enabled",
4+
"entity_id": "s13d85634-366f-11f0-8b00-d5c347dbdc20",
5+
"_id": "s13d85632-366f-11f0-8b00-d5c347dbdc20",
6+
"short_description": "Cappuccino Coffee",
7+
"photo": "https://app-upload-assets.cdn.botstar.com/d72e0570-bfdb-11e7-affb-8b31a0f64612/image/1517097658980/cappuccino.jpg"
8+
}

0 commit comments

Comments
 (0)