diff --git a/components/botstar/actions/send-message/send-message.mjs b/components/botstar/actions/send-message/send-message.mjs new file mode 100644 index 0000000000000..c0ca0465918dc --- /dev/null +++ b/components/botstar/actions/send-message/send-message.mjs @@ -0,0 +1,37 @@ +import botstar from "../../botstar.app.mjs"; + +export default { + key: "botstar-send-message", + name: "Send Message", + description: "Send a message to a user via BotStar. [See the docs](https://apis.botstar.com/docs/#/Messages/post_messages)", + version: "0.0.1", + type: "action", + props: { + botstar, + recipientId: { + type: "string", + label: "Recipient ID", + 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", + }, + message: { + type: "string", + label: "Message", + description: "The text message to send", + }, + }, + async run({ $ }) { + const response = await this.botstar.sendMessage({ + $, + data: { + recipient: { + id: this.recipientId, + }, + message: { + text: this.message, + }, + }, + }); + $.export("$summary", `Sent message to user ${this.recipientId}`); + return response; + }, +}; diff --git a/components/botstar/botstar.app.mjs b/components/botstar/botstar.app.mjs index f3d0b9dc6b6ac..446139b9c1bec 100644 --- a/components/botstar/botstar.app.mjs +++ b/components/botstar/botstar.app.mjs @@ -1,11 +1,107 @@ +import { axios } from "@pipedream/platform"; + export default { type: "app", app: "botstar", - propDefinitions: {}, + propDefinitions: { + botId: { + type: "string", + label: "Bot ID", + description: "The ID of the bot to use for the request", + async options() { + const bots = await this.listBots(); + return bots?.map(({ + id: value, name: label, + }) => ({ + label, + value, + })) || []; + }, + }, + entityId: { + type: "string", + label: "CMS Entity ID", + description: "The ID of the CMS entity to use for the request", + async options({ botId }) { + const cmsEntities = await this.listCmsEntities({ + botId, + }); + return cmsEntities?.map(({ + id: value, name: label, + }) => ({ + label, + value, + })) || []; + }, + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _baseUrl() { + return "https://apis.botstar.com/v1"; + }, + _makeRequest({ + $ = this, path, ...opts + }) { + return axios($, { + url: `${this._baseUrl()}${path}`, + headers: { + Authorization: `Bearer ${this.$auth.access_token}`, + }, + ...opts, + }); + }, + listBots(opts = {}) { + return this._makeRequest({ + path: "/bots", + ...opts, + }); + }, + listCmsEntities({ + botId, ...opts + }) { + return this._makeRequest({ + path: `/bots/${botId}/cms_entities`, + ...opts, + }); + }, + listCmsEntityItems({ + botId, entityId, ...opts + }) { + return this._makeRequest({ + path: `/bots/${botId}/cms_entities/${entityId}/items`, + ...opts, + }); + }, + sendMessage(opts = {}) { + return this._makeRequest({ + method: "POST", + path: "/messages", + ...opts, + }); + }, + async *paginate({ + fn, args, max, + }) { + args = { + ...args, + params: { + ...args?.params, + page: 1, + limit: 100, + }, + }; + let total, count = 1; + do { + const items = await fn(args); + for (const item of items) { + yield item; + if (max && ++count >= max) { + return; + } + } + total = items?.length; + args.params.page++; + } while (total === args.params.limit); }, }, }; diff --git a/components/botstar/package.json b/components/botstar/package.json index afa47077c6bfb..fe13421a535c5 100644 --- a/components/botstar/package.json +++ b/components/botstar/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/botstar", - "version": "0.6.0", + "version": "0.7.0", "description": "Pipedream botstar Components", "main": "botstar.app.mjs", "keywords": [ @@ -13,6 +13,6 @@ "access": "public" }, "dependencies": { - "@pipedream/platform": "^3.0.0" + "@pipedream/platform": "^3.0.3" } } diff --git a/components/botstar/sources/common/base.mjs b/components/botstar/sources/common/base.mjs new file mode 100644 index 0000000000000..5bfa34637c802 --- /dev/null +++ b/components/botstar/sources/common/base.mjs @@ -0,0 +1,14 @@ +import botstar from "../../botstar.app.mjs"; +import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; + +export default { + props: { + botstar, + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, + }, + }, + }, +}; diff --git a/components/botstar/sources/new-bot-created/new-bot-created.mjs b/components/botstar/sources/new-bot-created/new-bot-created.mjs new file mode 100644 index 0000000000000..0c8202dc5c204 --- /dev/null +++ b/components/botstar/sources/new-bot-created/new-bot-created.mjs @@ -0,0 +1,29 @@ +import common from "../common/base.mjs"; +import sampleEmit from "./test-event.mjs"; + +export default { + ...common, + key: "botstar-new-bot-created", + name: "New Bot Created", + description: "Emit new event when a new bot is created in BotStar. [See the documentation](https://apis.botstar.com/docs/#/Bots/get_bots_)", + version: "0.0.1", + type: "source", + dedupe: "unique", + methods: { + generateMeta(bot) { + return { + id: bot.id, + summary: `New Bot: ${bot.name}`, + ts: Date.now(), + }; + }, + }, + async run() { + const bots = await this.botstar.listBots(); + for (const bot of bots) { + const meta = this.generateMeta(bot); + this.$emit(bot, meta); + } + }, + sampleEmit, +}; diff --git a/components/botstar/sources/new-bot-created/test-event.mjs b/components/botstar/sources/new-bot-created/test-event.mjs new file mode 100644 index 0000000000000..c9f0640703c80 --- /dev/null +++ b/components/botstar/sources/new-bot-created/test-event.mjs @@ -0,0 +1,5 @@ +export default { + "id": "s13d855e0-366f-11f0-8b00-d5c347dbdc20", + "name": "Sample Tutorial Bot", + "team_name": "Team" + } \ No newline at end of file diff --git a/components/botstar/sources/new-cms-entity-created/new-cms-entity-created.mjs b/components/botstar/sources/new-cms-entity-created/new-cms-entity-created.mjs new file mode 100644 index 0000000000000..9aa257a739ae9 --- /dev/null +++ b/components/botstar/sources/new-cms-entity-created/new-cms-entity-created.mjs @@ -0,0 +1,43 @@ +import common from "../common/base.mjs"; +import sampleEmit from "./test-event.mjs"; + +export default { + ...common, + key: "botstar-new-cms-entity-created", + name: "New CMS Entity Created", + 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)", + version: "0.0.1", + type: "source", + dedupe: "unique", + props: { + ...common.props, + botId: { + propDefinition: [ + common.props.botstar, + "botId", + ], + }, + }, + methods: { + generateMeta(entity) { + return { + id: entity.id, + summary: `New CMS Entity: ${entity.name}`, + ts: Date.now(), + }; + }, + }, + async run() { + const entities = await this.botstar.listCmsEntities({ + botId: this.botId, + }); + if (!entities?.length) { + return; + } + for (const entity of entities.reverse()) { + const meta = this.generateMeta(entity); + this.$emit(entity, meta); + } + }, + sampleEmit, +}; diff --git a/components/botstar/sources/new-cms-entity-created/test-event.mjs b/components/botstar/sources/new-cms-entity-created/test-event.mjs new file mode 100644 index 0000000000000..5ca41e10a8064 --- /dev/null +++ b/components/botstar/sources/new-cms-entity-created/test-event.mjs @@ -0,0 +1,26 @@ +export default { + "id": "s13d85634-366f-11f0-8b00-d5c347dbdc20", + "name": "Coffee", + "fields": [ + { + "unique_name": "name", + "name": "Name", + "data_type": "text" + }, + { + "unique_name": "status", + "name": "Status", + "data_type": "single_option" + }, + { + "unique_name": "short_description", + "name": "Short Description", + "data_type": "free_text" + }, + { + "unique_name": "photo", + "name": "Photo", + "data_type": "image" + } + ] + } \ No newline at end of file diff --git a/components/botstar/sources/new-cms-entity-item-created/new-cms-entity-item-created.mjs b/components/botstar/sources/new-cms-entity-item-created/new-cms-entity-item-created.mjs new file mode 100644 index 0000000000000..139258ae3f8af --- /dev/null +++ b/components/botstar/sources/new-cms-entity-item-created/new-cms-entity-item-created.mjs @@ -0,0 +1,53 @@ +import common from "../common/base.mjs"; +import sampleEmit from "./test-event.mjs"; + +export default { + ...common, + key: "botstar-new-cms-entity-item-created", + name: "New CMS Entity Item Created", + 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)", + version: "0.0.1", + type: "source", + dedupe: "unique", + props: { + ...common.props, + botId: { + propDefinition: [ + common.props.botstar, + "botId", + ], + }, + entityId: { + propDefinition: [ + common.props.botstar, + "entityId", + (c) => ({ + botId: c.botId, + }), + ], + }, + }, + methods: { + generateMeta(item) { + return { + id: item._id, + summary: `New CMS Entity Item: ${item.name || item.id}`, + ts: Date.now(), + }; + }, + }, + async run() { + const results = this.botstar.paginate({ + fn: this.botstar.listCmsEntityItems, + args: { + botId: this.botId, + entityId: this.entityId, + }, + }); + for await (const item of results) { + const meta = this.generateMeta(item); + this.$emit(item, meta); + } + }, + sampleEmit, +}; diff --git a/components/botstar/sources/new-cms-entity-item-created/test-event.mjs b/components/botstar/sources/new-cms-entity-item-created/test-event.mjs new file mode 100644 index 0000000000000..5c85c6a3d5649 --- /dev/null +++ b/components/botstar/sources/new-cms-entity-item-created/test-event.mjs @@ -0,0 +1,8 @@ +export default { + "name": "Cappuccino", + "status": "enabled", + "entity_id": "s13d85634-366f-11f0-8b00-d5c347dbdc20", + "_id": "s13d85632-366f-11f0-8b00-d5c347dbdc20", + "short_description": "Cappuccino Coffee", + "photo": "https://app-upload-assets.cdn.botstar.com/d72e0570-bfdb-11e7-affb-8b31a0f64612/image/1517097658980/cappuccino.jpg" + } \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index caa2527f3b43c..88d9cf07540cc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1708,7 +1708,7 @@ importers: components/botstar: dependencies: '@pipedream/platform': - specifier: ^3.0.0 + specifier: ^3.0.3 version: 3.0.3 components/botx: {} @@ -15448,14 +15448,6 @@ importers: specifier: ^6.0.0 version: 6.2.0 - modelcontextprotocol/node_modules2/@modelcontextprotocol/sdk/dist/cjs: {} - - modelcontextprotocol/node_modules2/@modelcontextprotocol/sdk/dist/esm: {} - - modelcontextprotocol/node_modules2/zod-to-json-schema/dist/cjs: {} - - modelcontextprotocol/node_modules2/zod-to-json-schema/dist/esm: {} - packages/ai: dependencies: '@pipedream/sdk':