From fb4189fe1812566e166a349993d6350df2111280 Mon Sep 17 00:00:00 2001 From: michelle0927 Date: Fri, 8 Nov 2024 15:11:19 -0500 Subject: [PATCH 1/3] init --- .../actions/create-lead/create-lead.mjs | 30 +++++ .../actions/delete-lead/delete-lead.mjs | 23 ++++ .../push-message-existing-chat.mjs | 40 ++++++ components/insertchat/insertchat.app.mjs | 121 +++++++++++++++++- .../sources/new-ai-chatbot/new-ai-chatbot.mjs | 37 ++++++ .../new-chat-session/new-chat-session.mjs | 55 ++++++++ .../insertchat/sources/new-lead/new-lead.mjs | 57 +++++++++ 7 files changed, 360 insertions(+), 3 deletions(-) create mode 100644 components/insertchat/actions/create-lead/create-lead.mjs create mode 100644 components/insertchat/actions/delete-lead/delete-lead.mjs create mode 100644 components/insertchat/actions/push-message-existing-chat/push-message-existing-chat.mjs create mode 100644 components/insertchat/sources/new-ai-chatbot/new-ai-chatbot.mjs create mode 100644 components/insertchat/sources/new-chat-session/new-chat-session.mjs create mode 100644 components/insertchat/sources/new-lead/new-lead.mjs 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..acad4c560be93 --- /dev/null +++ b/components/insertchat/actions/create-lead/create-lead.mjs @@ -0,0 +1,30 @@ +import insertchat from "../../insertchat.app.mjs"; + +export default { + key: "insertchat-create-lead", + name: "Create Lead", + description: "Creates a new lead within Insertchat.", + version: "0.0.{{ts}}", + type: "action", + props: { + insertchat, + leadContactInfo: { + propDefinition: [ + insertchat, + "leadContactInfo", + ], + }, + leadStatus: { + propDefinition: [ + insertchat, + "leadStatus", + ], + optional: true, + }, + }, + async run({ $ }) { + const response = await this.insertchat.createNewLead(this.leadContactInfo, this.leadStatus); + $.export("$summary", `Created lead with ID: ${response.id}`); + 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..1a3773b86915f --- /dev/null +++ b/components/insertchat/actions/delete-lead/delete-lead.mjs @@ -0,0 +1,23 @@ +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://docs.insertchat.com/)", + version: "0.0.1", + type: "action", + props: { + insertchat, + leadId: { + propDefinition: [ + insertchat, + "leadId", + ], + }, + }, + async run({ $ }) { + const response = await this.insertchat.deleteLead(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..618d3c2ea1d35 --- /dev/null +++ b/components/insertchat/actions/push-message-existing-chat/push-message-existing-chat.mjs @@ -0,0 +1,40 @@ +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", + version: "0.0.{{ts}}", + type: "action", + props: { + insertchat, + chatSessionId: { + propDefinition: [ + insertchat, + "chatSessionId", + ], + }, + messageContent: { + propDefinition: [ + insertchat, + "messageContent", + ], + }, + messageSender: { + propDefinition: [ + insertchat, + "messageSender", + ], + optional: true, + }, + }, + async run({ $ }) { + const response = await this.insertchat.pushMessage( + this.chatSessionId, + this.messageContent, + this.messageSender, + ); + $.export("$summary", `Successfully pushed message to chat session ${this.chatSessionId}`); + return response; + }, +}; diff --git a/components/insertchat/insertchat.app.mjs b/components/insertchat/insertchat.app.mjs index c5bb665b4c97b..b3528e8fc692e 100644 --- a/components/insertchat/insertchat.app.mjs +++ b/components/insertchat/insertchat.app.mjs @@ -1,11 +1,126 @@ +import { axios } from "@pipedream/platform"; + export default { type: "app", app: "insertchat", - propDefinitions: {}, + propDefinitions: { + leadContactInfo: { + type: "object", + label: "Lead Contact Information", + description: "The contact information for the lead (name, email, phone, etc.)", + required: true, + }, + leadStatus: { + type: "string", + label: "Lead Status", + description: "The status of the lead (new, in-progress, converted)", + optional: true, + }, + leadId: { + type: "string", + label: "Lead ID", + description: "The unique identifier for the lead", + required: true, + }, + chatSessionId: { + type: "string", + label: "Chat Session ID", + description: "The unique identifier for the chat session", + required: true, + }, + messageContent: { + type: "string", + label: "Message Content", + description: "The content of the message to be pushed into the chat session", + required: true, + }, + messageSender: { + type: "string", + label: "Message Sender", + description: "The sender of the message (user or ai)", + optional: true, + }, + userInfo: { + type: "object", + label: "User Info", + description: "The user's information", + required: true, + }, + chatTranscript: { + type: "string", + label: "Chat Transcript", + description: "The transcript of the chat session", + required: true, + }, + }, methods: { - // this.$auth contains connected account data authKeys() { console.log(Object.keys(this.$auth)); }, + _baseUrl() { + return "https://api.insertchat.com"; + }, + async _makeRequest(opts = {}) { + const { + $ = this, method = "GET", path = "/", headers, ...otherOpts + } = opts; + return axios($, { + ...otherOpts, + method, + url: this._baseUrl() + path, + headers: { + ...headers, + Authorization: `Bearer ${this.$auth.api_token}`, + }, + }); + }, + async emitNewChatbot() { + return this._makeRequest({ + path: "/chatbots/new", + }); + }, + async emitNewLead(contactInfo) { + return this._makeRequest({ + method: "POST", + path: "/leads/new", + data: contactInfo, + }); + }, + async emitNewChatSession(userInfo, chatTranscript) { + return this._makeRequest({ + method: "POST", + path: "/chats/new", + data: { + userInfo, + chatTranscript, + }, + }); + }, + async createNewLead(contactInfo, leadStatus) { + return this._makeRequest({ + method: "POST", + path: "/leads", + data: { + contactInfo, + status: leadStatus, + }, + }); + }, + async deleteLead(leadId) { + return this._makeRequest({ + method: "DELETE", + path: `/leads/${leadId}`, + }); + }, + async pushMessage(sessionId, messageContent, sender) { + return this._makeRequest({ + method: "POST", + path: `/chats/${sessionId}/messages`, + data: { + content: messageContent, + sender, + }, + }); + }, }, -}; \ No newline at end of file +}; 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..9c438de8f8f76 --- /dev/null +++ b/components/insertchat/sources/new-ai-chatbot/new-ai-chatbot.mjs @@ -0,0 +1,37 @@ +import insertchat from "../../insertchat.app.mjs"; + +export default { + 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", + props: { + insertchat, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: 60 * 15, // by default, run every 15 minutes + }, + }, + }, + methods: { + generateMeta(data) { + const { + id, created_at, + } = data; + return { + id, + summary: `New AI Chatbot: ${id}`, + ts: Date.parse(created_at), + }; + }, + }, + async run() { + const chatbot = await this.insertchat.emitNewChatbot(); + const meta = this.generateMeta(chatbot); + this.$emit(chatbot, meta); + }, +}; 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..3ea8638641fb6 --- /dev/null +++ b/components/insertchat/sources/new-chat-session/new-chat-session.mjs @@ -0,0 +1,55 @@ +import insertchat from "../../insertchat.app.mjs"; + +export default { + key: "insertchat-new-chat-session", + name: "New Chat Session", + description: "Emit new event when a new chat session is initiated. [See the documentation](https://docs.insertchat.com)", + version: "0.0.{{ts}}", + type: "source", + dedupe: "unique", + props: { + insertchat, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: 60, + }, + }, + userInfo: { + propDefinition: [ + insertchat, + "userInfo", + ], + }, + chatTranscript: { + propDefinition: [ + insertchat, + "chatTranscript", + ], + }, + }, + methods: { + generateMeta(data) { + const { + id, created_at, + } = data; + return { + id, + summary: `New chat session: ${id}`, + ts: Date.parse(created_at), + }; + }, + }, + async run() { + const lastRun = this.db.get("lastRun") || this.timer.timestamp; + const results = await this.insertchat.emitNewChatSession(this.userInfo, this.chatTranscript); + for (const result of results) { + const timestamp = Date.parse(result.created_at); + if (timestamp > lastRun) { + this.$emit(result, this.generateMeta(result)); + } + } + this.db.set("lastRun", this.timer.timestamp); + }, +}; 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..57760dd2f3872 --- /dev/null +++ b/components/insertchat/sources/new-lead/new-lead.mjs @@ -0,0 +1,57 @@ +import insertchat from "../../insertchat.app.mjs"; + +export default { + key: "insertchat-new-lead", + name: "New Lead Created", + description: "Emits a new event when a new lead is created. Best for real-time CRM integration.", + version: "0.0.{{ts}}", + type: "source", + dedupe: "unique", + props: { + insertchat, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: 60, + }, + }, + leadContactInfo: { + propDefinition: [ + insertchat, + "leadContactInfo", + ], + required: true, + }, + }, + methods: { + _getLeadId() { + return this.db.get("leadId") || null; + }, + _setLeadId(leadId) { + this.db.set("leadId", leadId); + }, + }, + hooks: { + async deploy() { + const lead = await this.insertchat.emitNewLead(this.leadContactInfo); + this._setLeadId(lead.id); + this.$emit(lead, { + id: lead.id, + summary: `New Lead: ${lead.name}`, + ts: Date.now(), + }); + }, + }, + async run() { + const lead = await this.insertchat.emitNewLead(this.leadContactInfo); + if (lead.id !== this._getLeadId()) { + this._setLeadId(lead.id); + this.$emit(lead, { + id: lead.id, + summary: `New Lead: ${lead.name}`, + ts: Date.now(), + }); + } + }, +}; From 9391348f231bb4cc0fad144d95af1664e2295812 Mon Sep 17 00:00:00 2001 From: michelle0927 Date: Fri, 8 Nov 2024 17:41:58 -0500 Subject: [PATCH 2/3] insertchat --- .../actions/create-lead/create-lead.mjs | 69 +++++-- .../actions/delete-lead/delete-lead.mjs | 7 +- .../push-message-existing-chat.mjs | 51 +++-- components/insertchat/insertchat.app.mjs | 176 +++++++++++------- components/insertchat/package.json | 7 +- components/insertchat/sources/common/base.mjs | 74 ++++++++ .../sources/new-ai-chatbot/new-ai-chatbot.mjs | 33 +--- .../new-chat-session/new-chat-session.mjs | 55 ++---- .../insertchat/sources/new-lead/new-lead.mjs | 55 +----- 9 files changed, 315 insertions(+), 212 deletions(-) create mode 100644 components/insertchat/sources/common/base.mjs diff --git a/components/insertchat/actions/create-lead/create-lead.mjs b/components/insertchat/actions/create-lead/create-lead.mjs index acad4c560be93..1d1fce45231fd 100644 --- a/components/insertchat/actions/create-lead/create-lead.mjs +++ b/components/insertchat/actions/create-lead/create-lead.mjs @@ -3,28 +3,75 @@ import insertchat from "../../insertchat.app.mjs"; export default { key: "insertchat-create-lead", name: "Create Lead", - description: "Creates a new lead within Insertchat.", - version: "0.0.{{ts}}", + 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, - leadContactInfo: { + chatbotId: { propDefinition: [ insertchat, - "leadContactInfo", + "chatbotId", ], }, - leadStatus: { - propDefinition: [ - insertchat, - "leadStatus", - ], + 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.createNewLead(this.leadContactInfo, this.leadStatus); - $.export("$summary", `Created lead with ID: ${response.id}`); + 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 index 1a3773b86915f..e908d3437073a 100644 --- a/components/insertchat/actions/delete-lead/delete-lead.mjs +++ b/components/insertchat/actions/delete-lead/delete-lead.mjs @@ -3,7 +3,7 @@ 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://docs.insertchat.com/)", + 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: { @@ -16,7 +16,10 @@ export default { }, }, async run({ $ }) { - const response = await this.insertchat.deleteLead(this.leadId); + 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 index 618d3c2ea1d35..996bc0a1fe873 100644 --- 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 @@ -3,38 +3,53 @@ 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", - version: "0.0.{{ts}}", + 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, - chatSessionId: { + chatbotId: { propDefinition: [ insertchat, - "chatSessionId", + "chatbotId", ], }, - messageContent: { + chatSessionId: { propDefinition: [ insertchat, - "messageContent", + "chatSessionId", + (c) => ({ + chatbotId: c.chatbotId, + }), ], }, - messageSender: { - propDefinition: [ - insertchat, - "messageSender", + role: { + type: "string", + label: "Role", + description: "Role to send message as", + options: [ + "user", + "assistant", ], - optional: true, + }, + message: { + type: "string", + label: "Message Content", + description: "The content of the message to be pushed into the chat session", }, }, - async run({ $ }) { - const response = await this.insertchat.pushMessage( - this.chatSessionId, - this.messageContent, - this.messageSender, - ); + 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}`); - return response; + // nothing to return }, }; diff --git a/components/insertchat/insertchat.app.mjs b/components/insertchat/insertchat.app.mjs index b3528e8fc692e..45b944864f5c7 100644 --- a/components/insertchat/insertchat.app.mjs +++ b/components/insertchat/insertchat.app.mjs @@ -4,123 +4,159 @@ export default { type: "app", app: "insertchat", propDefinitions: { - leadContactInfo: { - type: "object", - label: "Lead Contact Information", - description: "The contact information for the lead (name, email, phone, etc.)", - required: true, - }, - leadStatus: { + chatbotId: { type: "string", - label: "Lead Status", - description: "The status of the lead (new, in-progress, converted)", - optional: true, + 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", - required: true, + 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", - required: true, - }, - messageContent: { - type: "string", - label: "Message Content", - description: "The content of the message to be pushed into the chat session", - required: true, - }, - messageSender: { - type: "string", - label: "Message Sender", - description: "The sender of the message (user or ai)", - optional: true, - }, - userInfo: { - type: "object", - label: "User Info", - description: "The user's information", - required: true, - }, - chatTranscript: { - type: "string", - label: "Chat Transcript", - description: "The transcript of the chat session", - required: true, + async options({ + chatbotId, page, + }) { + const { data } = await this.listChatSessions({ + chatbotId, + page: page + 1, + }); + return data?.map(({ + uid: value, label, + }) => ({ + value, + label, + })) || []; + }, }, }, methods: { - authKeys() { - console.log(Object.keys(this.$auth)); + _appId() { + return this.$auth.app_uid; }, _baseUrl() { - return "https://api.insertchat.com"; + return "https://api.insertchat.com/v1"; }, - async _makeRequest(opts = {}) { + _makeRequest(opts = {}) { const { - $ = this, method = "GET", path = "/", headers, ...otherOpts + $ = this, + path, + headers, + ...otherOpts } = opts; return axios($, { ...otherOpts, - method, - url: this._baseUrl() + path, + url: `${this._baseUrl()}${path}`, headers: { ...headers, - Authorization: `Bearer ${this.$auth.api_token}`, + Authorization: `Bearer ${this.$auth.oauth_access_token}`, }, }); }, - async emitNewChatbot() { + listChatbots(opts = {}) { return this._makeRequest({ - path: "/chatbots/new", + path: `/${this._appId()}/widgets`, + ...opts, }); }, - async emitNewLead(contactInfo) { + listLeads(opts = {}) { return this._makeRequest({ - method: "POST", - path: "/leads/new", - data: contactInfo, + path: `/${this._appId()}/contacts`, + ...opts, }); }, - async emitNewChatSession(userInfo, chatTranscript) { + listChatSessions({ + chatbotId, ...opts + }) { return this._makeRequest({ - method: "POST", - path: "/chats/new", - data: { - userInfo, - chatTranscript, - }, + path: `/${this._appId()}/chats/history/${chatbotId}?expand[0]=messages`, + ...opts, }); }, - async createNewLead(contactInfo, leadStatus) { + createLead(opts = {}) { return this._makeRequest({ method: "POST", - path: "/leads", - data: { - contactInfo, - status: leadStatus, - }, + path: `/${this._appId()}/contacts`, + ...opts, }); }, - async deleteLead(leadId) { + deleteLead({ + leadId, ...opts + }) { return this._makeRequest({ method: "DELETE", - path: `/leads/${leadId}`, + path: `/${this._appId()}/contacts/${leadId}`, + ...opts, }); }, - async pushMessage(sessionId, messageContent, sender) { + pushMessage(opts = {}) { return this._makeRequest({ method: "POST", - path: `/chats/${sessionId}/messages`, - data: { - content: messageContent, - sender, + 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); + }, }, }; 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 index 9c438de8f8f76..3bd9c821f759d 100644 --- a/components/insertchat/sources/new-ai-chatbot/new-ai-chatbot.mjs +++ b/components/insertchat/sources/new-ai-chatbot/new-ai-chatbot.mjs @@ -1,37 +1,20 @@ -import insertchat from "../../insertchat.app.mjs"; +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", - props: { - insertchat, - db: "$.service.db", - timer: { - type: "$.interface.timer", - default: { - intervalSeconds: 60 * 15, // by default, run every 15 minutes - }, - }, - }, methods: { - generateMeta(data) { - const { - id, created_at, - } = data; - return { - id, - summary: `New AI Chatbot: ${id}`, - ts: Date.parse(created_at), - }; + ...common.methods, + getResourceFn() { + return this.insertchat.listChatbots; + }, + getSummary(item) { + return `New Chatbot ID: ${item.uid}`; }, - }, - async run() { - const chatbot = await this.insertchat.emitNewChatbot(); - const meta = this.generateMeta(chatbot); - this.$emit(chatbot, meta); }, }; diff --git a/components/insertchat/sources/new-chat-session/new-chat-session.mjs b/components/insertchat/sources/new-chat-session/new-chat-session.mjs index 3ea8638641fb6..49d433e88e9c9 100644 --- a/components/insertchat/sources/new-chat-session/new-chat-session.mjs +++ b/components/insertchat/sources/new-chat-session/new-chat-session.mjs @@ -1,55 +1,34 @@ -import insertchat from "../../insertchat.app.mjs"; +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. [See the documentation](https://docs.insertchat.com)", - version: "0.0.{{ts}}", + description: "Emit new event when a new chat session is initiated.", + version: "0.0.1", type: "source", dedupe: "unique", props: { - insertchat, - db: "$.service.db", - timer: { - type: "$.interface.timer", - default: { - intervalSeconds: 60, - }, - }, - userInfo: { - propDefinition: [ - insertchat, - "userInfo", - ], - }, - chatTranscript: { + ...common.props, + chatbotId: { propDefinition: [ - insertchat, - "chatTranscript", + common.props.insertchat, + "chatbotId", ], }, }, methods: { - generateMeta(data) { - const { - id, created_at, - } = data; + ...common.methods, + getResourceFn() { + return this.insertchat.listChatSessions; + }, + getArgs() { return { - id, - summary: `New chat session: ${id}`, - ts: Date.parse(created_at), + chatbotId: this.chatbotId, }; }, - }, - async run() { - const lastRun = this.db.get("lastRun") || this.timer.timestamp; - const results = await this.insertchat.emitNewChatSession(this.userInfo, this.chatTranscript); - for (const result of results) { - const timestamp = Date.parse(result.created_at); - if (timestamp > lastRun) { - this.$emit(result, this.generateMeta(result)); - } - } - this.db.set("lastRun", this.timer.timestamp); + 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 index 57760dd2f3872..17ab6f9ceeffe 100644 --- a/components/insertchat/sources/new-lead/new-lead.mjs +++ b/components/insertchat/sources/new-lead/new-lead.mjs @@ -1,57 +1,20 @@ -import insertchat from "../../insertchat.app.mjs"; +import common from "../common/base.mjs"; export default { + ...common, key: "insertchat-new-lead", name: "New Lead Created", - description: "Emits a new event when a new lead is created. Best for real-time CRM integration.", - version: "0.0.{{ts}}", + description: "Emit new event when a new lead is created.", + version: "0.0.1", type: "source", dedupe: "unique", - props: { - insertchat, - db: "$.service.db", - timer: { - type: "$.interface.timer", - default: { - intervalSeconds: 60, - }, - }, - leadContactInfo: { - propDefinition: [ - insertchat, - "leadContactInfo", - ], - required: true, - }, - }, methods: { - _getLeadId() { - return this.db.get("leadId") || null; + ...common.methods, + getResourceFn() { + return this.insertchat.listLeads; }, - _setLeadId(leadId) { - this.db.set("leadId", leadId); + getSummary(item) { + return `New Lead ID: ${item.uid}`; }, }, - hooks: { - async deploy() { - const lead = await this.insertchat.emitNewLead(this.leadContactInfo); - this._setLeadId(lead.id); - this.$emit(lead, { - id: lead.id, - summary: `New Lead: ${lead.name}`, - ts: Date.now(), - }); - }, - }, - async run() { - const lead = await this.insertchat.emitNewLead(this.leadContactInfo); - if (lead.id !== this._getLeadId()) { - this._setLeadId(lead.id); - this.$emit(lead, { - id: lead.id, - summary: `New Lead: ${lead.name}`, - ts: Date.now(), - }); - } - }, }; From 0e737e1072224828054754d0509fcac64e4260c6 Mon Sep 17 00:00:00 2001 From: michelle0927 Date: Fri, 8 Nov 2024 17:42:53 -0500 Subject: [PATCH 3/3] pnpm-lock.yaml --- pnpm-lock.yaml | 107 +++++++++++++++++++++++++------------------------ 1 file changed, 55 insertions(+), 52 deletions(-) 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