diff --git a/components/insertchat/actions/create-lead/create-lead.mjs b/components/insertchat/actions/create-lead/create-lead.mjs new file mode 100644 index 0000000000000..1d1fce45231fd --- /dev/null +++ b/components/insertchat/actions/create-lead/create-lead.mjs @@ -0,0 +1,77 @@ +import insertchat from "../../insertchat.app.mjs"; + +export default { + key: "insertchat-create-lead", + name: "Create Lead", + description: "Creates a new lead within Insertchat. [See the documentation](https://www.postman.com/gold-star-239225/insertchat/request/uiugp1c/create-a-lead)", + version: "0.0.1", + type: "action", + props: { + insertchat, + chatbotId: { + propDefinition: [ + insertchat, + "chatbotId", + ], + }, + firstName: { + type: "string", + label: "First Name", + description: "First name of the lead", + optional: true, + }, + lastName: { + type: "string", + label: "Last Name", + description: "Last name of the lead", + optional: true, + }, + email: { + type: "string", + label: "Email", + description: "Email address of the lead", + optional: true, + }, + phone: { + type: "string", + label: "Phone", + description: "Phone number of the lead", + optional: true, + }, + address: { + type: "string", + label: "Address", + description: "Address of the lead", + optional: true, + }, + website: { + type: "string", + label: "Website", + description: "Website of the lead", + optional: true, + }, + company: { + type: "string", + label: "Company", + description: "Company of the lead", + optional: true, + }, + }, + async run({ $ }) { + const response = await this.insertchat.createLead({ + $, + data: { + widget_uid: this.chatbotId, + first_name: this.firstName, + last_name: this.lastName, + email: this.email, + phone: this.phone, + address: this.address, + website: this.website, + company: this.company, + }, + }); + $.export("$summary", `Created lead with ID: ${response.uid}`); + return response; + }, +}; diff --git a/components/insertchat/actions/delete-lead/delete-lead.mjs b/components/insertchat/actions/delete-lead/delete-lead.mjs new file mode 100644 index 0000000000000..e908d3437073a --- /dev/null +++ b/components/insertchat/actions/delete-lead/delete-lead.mjs @@ -0,0 +1,26 @@ +import insertchat from "../../insertchat.app.mjs"; + +export default { + key: "insertchat-delete-lead", + name: "Delete Lead", + description: "Deletes an existing lead from InsertChat. [See the documentation](https://www.postman.com/gold-star-239225/insertchat/request/2vgc20j/delete-a-lead)", + version: "0.0.1", + type: "action", + props: { + insertchat, + leadId: { + propDefinition: [ + insertchat, + "leadId", + ], + }, + }, + async run({ $ }) { + const response = await this.insertchat.deleteLead({ + $, + leadId: this.leadId, + }); + $.export("$summary", `Successfully deleted lead with ID: ${this.leadId}`); + return response; + }, +}; diff --git a/components/insertchat/actions/push-message-existing-chat/push-message-existing-chat.mjs b/components/insertchat/actions/push-message-existing-chat/push-message-existing-chat.mjs new file mode 100644 index 0000000000000..996bc0a1fe873 --- /dev/null +++ b/components/insertchat/actions/push-message-existing-chat/push-message-existing-chat.mjs @@ -0,0 +1,55 @@ +import insertchat from "../../insertchat.app.mjs"; + +export default { + key: "insertchat-push-message-existing-chat", + name: "Push Message to Existing Chat", + description: "Pushes a new message into an existing chat session in InsertChat. [See the documentation](https://www.postman.com/gold-star-239225/insertchat/request/me7mcwa/push-a-message-into-a-chat-session)", + version: "0.0.1", + type: "action", + props: { + insertchat, + chatbotId: { + propDefinition: [ + insertchat, + "chatbotId", + ], + }, + chatSessionId: { + propDefinition: [ + insertchat, + "chatSessionId", + (c) => ({ + chatbotId: c.chatbotId, + }), + ], + }, + role: { + type: "string", + label: "Role", + description: "Role to send message as", + options: [ + "user", + "assistant", + ], + }, + message: { + type: "string", + label: "Message Content", + description: "The content of the message to be pushed into the chat session", + }, + }, + run({ $ }) { + // method works, but times out if we await the response + this.insertchat.pushMessage({ + $, + data: new URLSearchParams({ + widget_uid: this.chatbotId, + chat_uid: this.chatSessionId, + role: this.role, + input: this.message, + }), + }); + $.export("$summary", `Successfully pushed message to chat session ${this.chatSessionId}`); + // nothing to return + }, +}; diff --git a/components/insertchat/insertchat.app.mjs b/components/insertchat/insertchat.app.mjs index c5bb665b4c97b..45b944864f5c7 100644 --- a/components/insertchat/insertchat.app.mjs +++ b/components/insertchat/insertchat.app.mjs @@ -1,11 +1,162 @@ +import { axios } from "@pipedream/platform"; + export default { type: "app", app: "insertchat", - propDefinitions: {}, + propDefinitions: { + chatbotId: { + type: "string", + label: "Chatbot ID", + description: "The unique identifier for the chatbot", + async options({ page }) { + const { data } = await this.listChatbots({ + params: { + page: page + 1, + }, + }); + return data?.map(({ + uid: value, label, + }) => ({ + value, + label, + })) || []; + }, + }, + leadId: { + type: "string", + label: "Lead ID", + description: "The unique identifier for the lead", + async options({ page }) { + const { data } = await this.listLeads({ + params: { + page: page + 1, + }, + }); + return data?.map(({ + uid: value, first_name: firstName, last_name: lastName, + }) => ({ + value, + label: firstName || lastName + ? (`${firstName} ${lastName}`).trim() + : value, + })) || []; + }, + }, + chatSessionId: { + type: "string", + label: "Chat Session ID", + description: "The unique identifier for the chat session", + async options({ + chatbotId, page, + }) { + const { data } = await this.listChatSessions({ + chatbotId, + page: page + 1, + }); + return data?.map(({ + uid: value, label, + }) => ({ + value, + label, + })) || []; + }, + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _appId() { + return this.$auth.app_uid; + }, + _baseUrl() { + return "https://api.insertchat.com/v1"; + }, + _makeRequest(opts = {}) { + const { + $ = this, + path, + headers, + ...otherOpts + } = opts; + return axios($, { + ...otherOpts, + url: `${this._baseUrl()}${path}`, + headers: { + ...headers, + Authorization: `Bearer ${this.$auth.oauth_access_token}`, + }, + }); + }, + listChatbots(opts = {}) { + return this._makeRequest({ + path: `/${this._appId()}/widgets`, + ...opts, + }); + }, + listLeads(opts = {}) { + return this._makeRequest({ + path: `/${this._appId()}/contacts`, + ...opts, + }); + }, + listChatSessions({ + chatbotId, ...opts + }) { + return this._makeRequest({ + path: `/${this._appId()}/chats/history/${chatbotId}?expand[0]=messages`, + ...opts, + }); + }, + createLead(opts = {}) { + return this._makeRequest({ + method: "POST", + path: `/${this._appId()}/contacts`, + ...opts, + }); + }, + deleteLead({ + leadId, ...opts + }) { + return this._makeRequest({ + method: "DELETE", + path: `/${this._appId()}/contacts/${leadId}`, + ...opts, + }); + }, + pushMessage(opts = {}) { + return this._makeRequest({ + method: "POST", + path: "/embeds/messages", + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + ...opts, + }); + }, + async *paginate({ + fn, + args, + max, + }) { + args = { + ...args, + params: { + ...args?.params, + page: 1, + }, + }; + let done, count = 0; + do { + const { + data, meta, + } = await fn(args); + for (const item of data) { + yield item; + if (max && ++count >= max) { + return; + } + done = args.params.page === meta.last_page; + args.params.page++; + } + } while (!done); }, }, -}; \ No newline at end of file +}; diff --git a/components/insertchat/package.json b/components/insertchat/package.json index 1190d619ba195..7b39139ab935f 100644 --- a/components/insertchat/package.json +++ b/components/insertchat/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/insertchat", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream InsertChat Components", "main": "insertchat.app.mjs", "keywords": [ @@ -11,5 +11,8 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^3.0.3" } -} \ No newline at end of file +} diff --git a/components/insertchat/sources/common/base.mjs b/components/insertchat/sources/common/base.mjs new file mode 100644 index 0000000000000..30c3e68d735a4 --- /dev/null +++ b/components/insertchat/sources/common/base.mjs @@ -0,0 +1,74 @@ +import insertchat from "../../insertchat.app.mjs"; +import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; + +export default { + props: { + insertchat, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, + }, + }, + }, + hooks: { + async deploy() { + await this.processEvent(25); + }, + }, + methods: { + _getLastTs() { + return this.db.get("lastTs") || 0; + }, + _setLastTs(lastTs) { + this.db.set("lastTs", lastTs); + }, + getResourceFn() { + throw new Error("getResourceFn is not implemented"); + }, + getSummary() { + throw new Error("getSummary is not implemented"); + }, + getArgs() { + return {}; + }, + getTsField() { + return "created_at"; + }, + generateMeta(item) { + return { + id: item.uid, + summary: this.getSummary(item), + ts: Date.parse(item[this.getTsField()]), + }; + }, + async processEvent(max) { + const lastTs = this._getLastTs(); + let maxTs = lastTs; + const resourceFn = this.getResourceFn(); + const args = this.getArgs(); + const tsField = this.getTsField(); + + const items = this.insertchat.paginate({ + fn: resourceFn, + args, + max, + }); + + for await (const item of items) { + const ts = Date.parse(item[tsField]); + if (ts > lastTs) { + const meta = this.generateMeta(item); + this.$emit(item, meta); + maxTs = Math.max(ts, maxTs); + } + } + + this._setLastTs(maxTs); + }, + }, + async run() { + await this.processEvent(); + }, +}; diff --git a/components/insertchat/sources/new-ai-chatbot/new-ai-chatbot.mjs b/components/insertchat/sources/new-ai-chatbot/new-ai-chatbot.mjs new file mode 100644 index 0000000000000..3bd9c821f759d --- /dev/null +++ b/components/insertchat/sources/new-ai-chatbot/new-ai-chatbot.mjs @@ -0,0 +1,20 @@ +import common from "../common/base.mjs"; + +export default { + ...common, + key: "insertchat-new-ai-chatbot", + name: "New AI Chatbot", + description: "Emit new event when a new AI chatbot is created.", + version: "0.0.1", + type: "source", + dedupe: "unique", + methods: { + ...common.methods, + getResourceFn() { + return this.insertchat.listChatbots; + }, + getSummary(item) { + return `New Chatbot ID: ${item.uid}`; + }, + }, +}; diff --git a/components/insertchat/sources/new-chat-session/new-chat-session.mjs b/components/insertchat/sources/new-chat-session/new-chat-session.mjs new file mode 100644 index 0000000000000..49d433e88e9c9 --- /dev/null +++ b/components/insertchat/sources/new-chat-session/new-chat-session.mjs @@ -0,0 +1,34 @@ +import common from "../common/base.mjs"; + +export default { + ...common, + key: "insertchat-new-chat-session", + name: "New Chat Session", + description: "Emit new event when a new chat session is initiated.", + version: "0.0.1", + type: "source", + dedupe: "unique", + props: { + ...common.props, + chatbotId: { + propDefinition: [ + common.props.insertchat, + "chatbotId", + ], + }, + }, + methods: { + ...common.methods, + getResourceFn() { + return this.insertchat.listChatSessions; + }, + getArgs() { + return { + chatbotId: this.chatbotId, + }; + }, + getSummary(item) { + return `New Chat Session ID: ${item.uid}`; + }, + }, +}; diff --git a/components/insertchat/sources/new-lead/new-lead.mjs b/components/insertchat/sources/new-lead/new-lead.mjs new file mode 100644 index 0000000000000..17ab6f9ceeffe --- /dev/null +++ b/components/insertchat/sources/new-lead/new-lead.mjs @@ -0,0 +1,20 @@ +import common from "../common/base.mjs"; + +export default { + ...common, + key: "insertchat-new-lead", + name: "New Lead Created", + description: "Emit new event when a new lead is created.", + version: "0.0.1", + type: "source", + dedupe: "unique", + methods: { + ...common.methods, + getResourceFn() { + return this.insertchat.listLeads; + }, + getSummary(item) { + return `New Lead ID: ${item.uid}`; + }, + }, +}; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3240d3e4ac457..716fe6d8cc171 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5036,7 +5036,10 @@ importers: '@pipedream/platform': 1.5.1 components/insertchat: - specifiers: {} + specifiers: + '@pipedream/platform': ^3.0.3 + dependencies: + '@pipedream/platform': 3.0.3 components/insightly: specifiers: @@ -13362,55 +13365,6 @@ packages: - aws-crt dev: false - /@aws-sdk/client-sso-oidc/3.600.0_tdq3komn4zwyd65w7klbptsu34: - resolution: {integrity: sha512-7+I8RWURGfzvChyNQSyj5/tKrqRbzRl7H+BnTOf/4Vsw1nFOi5ROhlhD4X/Y0QCTacxnaoNcIrqnY7uGGvVRzw==} - engines: {node: '>=16.0.0'} - dependencies: - '@aws-crypto/sha256-browser': 5.2.0 - '@aws-crypto/sha256-js': 5.2.0 - '@aws-sdk/client-sts': 3.600.0 - '@aws-sdk/core': 3.598.0 - '@aws-sdk/credential-provider-node': 3.600.0_f7n47caigsrjd2lr2szmwfuee4 - '@aws-sdk/middleware-host-header': 3.598.0 - '@aws-sdk/middleware-logger': 3.598.0 - '@aws-sdk/middleware-recursion-detection': 3.598.0 - '@aws-sdk/middleware-user-agent': 3.598.0 - '@aws-sdk/region-config-resolver': 3.598.0 - '@aws-sdk/types': 3.598.0 - '@aws-sdk/util-endpoints': 3.598.0 - '@aws-sdk/util-user-agent-browser': 3.598.0 - '@aws-sdk/util-user-agent-node': 3.598.0 - '@smithy/config-resolver': 3.0.3 - '@smithy/core': 2.2.3 - '@smithy/fetch-http-handler': 3.2.1 - '@smithy/hash-node': 3.0.2 - '@smithy/invalid-dependency': 3.0.2 - '@smithy/middleware-content-length': 3.0.2 - '@smithy/middleware-endpoint': 3.0.4 - '@smithy/middleware-retry': 3.0.6 - '@smithy/middleware-serde': 3.0.3 - '@smithy/middleware-stack': 3.0.3 - '@smithy/node-config-provider': 3.1.3 - '@smithy/node-http-handler': 3.1.2 - '@smithy/protocol-http': 4.0.3 - '@smithy/smithy-client': 3.1.6 - '@smithy/types': 3.3.0 - '@smithy/url-parser': 3.0.3 - '@smithy/util-base64': 3.0.0 - '@smithy/util-body-length-browser': 3.0.0 - '@smithy/util-body-length-node': 3.0.0 - '@smithy/util-defaults-mode-browser': 3.0.6 - '@smithy/util-defaults-mode-node': 3.0.6 - '@smithy/util-endpoints': 2.0.3 - '@smithy/util-middleware': 3.0.3 - '@smithy/util-retry': 3.0.2 - '@smithy/util-utf8': 3.0.0 - tslib: 2.6.3 - transitivePeerDependencies: - - '@aws-sdk/client-sts' - - aws-crt - dev: false - /@aws-sdk/client-sso/3.423.0: resolution: {integrity: sha512-znIufHkwhCIePgaYciIs3x/+BpzR57CZzbCKHR9+oOvGyufEPPpUT5bFLvbwTgfiVkTjuk6sG/ES3U5Bc+xtrA==} engines: {node: '>=14.0.0'} @@ -13646,7 +13600,7 @@ packages: dependencies: '@aws-crypto/sha256-browser': 5.2.0 '@aws-crypto/sha256-js': 5.2.0 - '@aws-sdk/client-sso-oidc': 3.600.0_tdq3komn4zwyd65w7klbptsu34 + '@aws-sdk/client-sso-oidc': 3.600.0 '@aws-sdk/core': 3.598.0 '@aws-sdk/credential-provider-node': 3.600.0_f7n47caigsrjd2lr2szmwfuee4 '@aws-sdk/middleware-host-header': 3.598.0 @@ -13688,6 +13642,55 @@ packages: - aws-crt dev: false + /@aws-sdk/client-sts/3.600.0_dseaa2p5u2yk67qiepewcq3hkq: + resolution: {integrity: sha512-KQG97B7LvTtTiGmjlrG1LRAY8wUvCQzrmZVV5bjrJ/1oXAU7DITYwVbSJeX9NWg6hDuSk0VE3MFwIXS2SvfLIA==} + engines: {node: '>=16.0.0'} + dependencies: + '@aws-crypto/sha256-browser': 5.2.0 + '@aws-crypto/sha256-js': 5.2.0 + '@aws-sdk/client-sso-oidc': 3.600.0 + '@aws-sdk/core': 3.598.0 + '@aws-sdk/credential-provider-node': 3.600.0_f7n47caigsrjd2lr2szmwfuee4 + '@aws-sdk/middleware-host-header': 3.598.0 + '@aws-sdk/middleware-logger': 3.598.0 + '@aws-sdk/middleware-recursion-detection': 3.598.0 + '@aws-sdk/middleware-user-agent': 3.598.0 + '@aws-sdk/region-config-resolver': 3.598.0 + '@aws-sdk/types': 3.598.0 + '@aws-sdk/util-endpoints': 3.598.0 + '@aws-sdk/util-user-agent-browser': 3.598.0 + '@aws-sdk/util-user-agent-node': 3.598.0 + '@smithy/config-resolver': 3.0.3 + '@smithy/core': 2.2.3 + '@smithy/fetch-http-handler': 3.2.1 + '@smithy/hash-node': 3.0.2 + '@smithy/invalid-dependency': 3.0.2 + '@smithy/middleware-content-length': 3.0.2 + '@smithy/middleware-endpoint': 3.0.4 + '@smithy/middleware-retry': 3.0.6 + '@smithy/middleware-serde': 3.0.3 + '@smithy/middleware-stack': 3.0.3 + '@smithy/node-config-provider': 3.1.3 + '@smithy/node-http-handler': 3.1.2 + '@smithy/protocol-http': 4.0.3 + '@smithy/smithy-client': 3.1.6 + '@smithy/types': 3.3.0 + '@smithy/url-parser': 3.0.3 + '@smithy/util-base64': 3.0.0 + '@smithy/util-body-length-browser': 3.0.0 + '@smithy/util-body-length-node': 3.0.0 + '@smithy/util-defaults-mode-browser': 3.0.6 + '@smithy/util-defaults-mode-node': 3.0.6 + '@smithy/util-endpoints': 2.0.3 + '@smithy/util-middleware': 3.0.3 + '@smithy/util-retry': 3.0.2 + '@smithy/util-utf8': 3.0.0 + tslib: 2.6.3 + transitivePeerDependencies: + - '@aws-sdk/client-sso-oidc' + - aws-crt + dev: false + /@aws-sdk/core/3.556.0: resolution: {integrity: sha512-vJaSaHw2kPQlo11j/Rzuz0gk1tEaKdz+2ser0f0qZ5vwFlANjt08m/frU17ctnVKC1s58bxpctO/1P894fHLrA==} engines: {node: '>=14.0.0'} @@ -18031,7 +18034,7 @@ packages: '@aws-sdk/client-sns': 3.423.0 '@aws-sdk/client-sqs': 3.423.0 '@aws-sdk/client-ssm': 3.423.0 - '@aws-sdk/client-sts': 3.600.0 + '@aws-sdk/client-sts': 3.600.0_dseaa2p5u2yk67qiepewcq3hkq '@aws-sdk/s3-request-presigner': 3.609.0 '@pipedream/helper_functions': 0.3.12 '@pipedream/platform': 1.6.6