|
| 1 | +import { axios } from "@pipedream/platform"; |
| 2 | + |
1 | 3 | export default { |
2 | 4 | type: "app", |
3 | 5 | 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 | + }, |
5 | 38 | 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); |
9 | 105 | }, |
10 | 106 | }, |
11 | 107 | }; |
0 commit comments