Skip to content

Commit 884bac7

Browse files
michelle0927lcaresia
authored andcommitted
New Components - insertchat (#14623)
* init * insertchat * pnpm-lock.yaml
1 parent 079a077 commit 884bac7

File tree

10 files changed

+522
-59
lines changed

10 files changed

+522
-59
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import insertchat from "../../insertchat.app.mjs";
2+
3+
export default {
4+
key: "insertchat-create-lead",
5+
name: "Create Lead",
6+
description: "Creates a new lead within Insertchat. [See the documentation](https://www.postman.com/gold-star-239225/insertchat/request/uiugp1c/create-a-lead)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
insertchat,
11+
chatbotId: {
12+
propDefinition: [
13+
insertchat,
14+
"chatbotId",
15+
],
16+
},
17+
firstName: {
18+
type: "string",
19+
label: "First Name",
20+
description: "First name of the lead",
21+
optional: true,
22+
},
23+
lastName: {
24+
type: "string",
25+
label: "Last Name",
26+
description: "Last name of the lead",
27+
optional: true,
28+
},
29+
email: {
30+
type: "string",
31+
label: "Email",
32+
description: "Email address of the lead",
33+
optional: true,
34+
},
35+
phone: {
36+
type: "string",
37+
label: "Phone",
38+
description: "Phone number of the lead",
39+
optional: true,
40+
},
41+
address: {
42+
type: "string",
43+
label: "Address",
44+
description: "Address of the lead",
45+
optional: true,
46+
},
47+
website: {
48+
type: "string",
49+
label: "Website",
50+
description: "Website of the lead",
51+
optional: true,
52+
},
53+
company: {
54+
type: "string",
55+
label: "Company",
56+
description: "Company of the lead",
57+
optional: true,
58+
},
59+
},
60+
async run({ $ }) {
61+
const response = await this.insertchat.createLead({
62+
$,
63+
data: {
64+
widget_uid: this.chatbotId,
65+
first_name: this.firstName,
66+
last_name: this.lastName,
67+
email: this.email,
68+
phone: this.phone,
69+
address: this.address,
70+
website: this.website,
71+
company: this.company,
72+
},
73+
});
74+
$.export("$summary", `Created lead with ID: ${response.uid}`);
75+
return response;
76+
},
77+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import insertchat from "../../insertchat.app.mjs";
2+
3+
export default {
4+
key: "insertchat-delete-lead",
5+
name: "Delete Lead",
6+
description: "Deletes an existing lead from InsertChat. [See the documentation](https://www.postman.com/gold-star-239225/insertchat/request/2vgc20j/delete-a-lead)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
insertchat,
11+
leadId: {
12+
propDefinition: [
13+
insertchat,
14+
"leadId",
15+
],
16+
},
17+
},
18+
async run({ $ }) {
19+
const response = await this.insertchat.deleteLead({
20+
$,
21+
leadId: this.leadId,
22+
});
23+
$.export("$summary", `Successfully deleted lead with ID: ${this.leadId}`);
24+
return response;
25+
},
26+
};
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import insertchat from "../../insertchat.app.mjs";
2+
3+
export default {
4+
key: "insertchat-push-message-existing-chat",
5+
name: "Push Message to Existing Chat",
6+
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)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
insertchat,
11+
chatbotId: {
12+
propDefinition: [
13+
insertchat,
14+
"chatbotId",
15+
],
16+
},
17+
chatSessionId: {
18+
propDefinition: [
19+
insertchat,
20+
"chatSessionId",
21+
(c) => ({
22+
chatbotId: c.chatbotId,
23+
}),
24+
],
25+
},
26+
role: {
27+
type: "string",
28+
label: "Role",
29+
description: "Role to send message as",
30+
options: [
31+
"user",
32+
"assistant",
33+
],
34+
},
35+
message: {
36+
type: "string",
37+
label: "Message Content",
38+
description: "The content of the message to be pushed into the chat session",
39+
},
40+
},
41+
run({ $ }) {
42+
// method works, but times out if we await the response
43+
this.insertchat.pushMessage({
44+
$,
45+
data: new URLSearchParams({
46+
widget_uid: this.chatbotId,
47+
chat_uid: this.chatSessionId,
48+
role: this.role,
49+
input: this.message,
50+
}),
51+
});
52+
$.export("$summary", `Successfully pushed message to chat session ${this.chatSessionId}`);
53+
// nothing to return
54+
},
55+
};
Lines changed: 156 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,162 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "insertchat",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
chatbotId: {
8+
type: "string",
9+
label: "Chatbot ID",
10+
description: "The unique identifier for the chatbot",
11+
async options({ page }) {
12+
const { data } = await this.listChatbots({
13+
params: {
14+
page: page + 1,
15+
},
16+
});
17+
return data?.map(({
18+
uid: value, label,
19+
}) => ({
20+
value,
21+
label,
22+
})) || [];
23+
},
24+
},
25+
leadId: {
26+
type: "string",
27+
label: "Lead ID",
28+
description: "The unique identifier for the lead",
29+
async options({ page }) {
30+
const { data } = await this.listLeads({
31+
params: {
32+
page: page + 1,
33+
},
34+
});
35+
return data?.map(({
36+
uid: value, first_name: firstName, last_name: lastName,
37+
}) => ({
38+
value,
39+
label: firstName || lastName
40+
? (`${firstName} ${lastName}`).trim()
41+
: value,
42+
})) || [];
43+
},
44+
},
45+
chatSessionId: {
46+
type: "string",
47+
label: "Chat Session ID",
48+
description: "The unique identifier for the chat session",
49+
async options({
50+
chatbotId, page,
51+
}) {
52+
const { data } = await this.listChatSessions({
53+
chatbotId,
54+
page: page + 1,
55+
});
56+
return data?.map(({
57+
uid: value, label,
58+
}) => ({
59+
value,
60+
label,
61+
})) || [];
62+
},
63+
},
64+
},
565
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
66+
_appId() {
67+
return this.$auth.app_uid;
68+
},
69+
_baseUrl() {
70+
return "https://api.insertchat.com/v1";
71+
},
72+
_makeRequest(opts = {}) {
73+
const {
74+
$ = this,
75+
path,
76+
headers,
77+
...otherOpts
78+
} = opts;
79+
return axios($, {
80+
...otherOpts,
81+
url: `${this._baseUrl()}${path}`,
82+
headers: {
83+
...headers,
84+
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
85+
},
86+
});
87+
},
88+
listChatbots(opts = {}) {
89+
return this._makeRequest({
90+
path: `/${this._appId()}/widgets`,
91+
...opts,
92+
});
93+
},
94+
listLeads(opts = {}) {
95+
return this._makeRequest({
96+
path: `/${this._appId()}/contacts`,
97+
...opts,
98+
});
99+
},
100+
listChatSessions({
101+
chatbotId, ...opts
102+
}) {
103+
return this._makeRequest({
104+
path: `/${this._appId()}/chats/history/${chatbotId}?expand[0]=messages`,
105+
...opts,
106+
});
107+
},
108+
createLead(opts = {}) {
109+
return this._makeRequest({
110+
method: "POST",
111+
path: `/${this._appId()}/contacts`,
112+
...opts,
113+
});
114+
},
115+
deleteLead({
116+
leadId, ...opts
117+
}) {
118+
return this._makeRequest({
119+
method: "DELETE",
120+
path: `/${this._appId()}/contacts/${leadId}`,
121+
...opts,
122+
});
123+
},
124+
pushMessage(opts = {}) {
125+
return this._makeRequest({
126+
method: "POST",
127+
path: "/embeds/messages",
128+
headers: {
129+
"Content-Type": "application/x-www-form-urlencoded",
130+
},
131+
...opts,
132+
});
133+
},
134+
async *paginate({
135+
fn,
136+
args,
137+
max,
138+
}) {
139+
args = {
140+
...args,
141+
params: {
142+
...args?.params,
143+
page: 1,
144+
},
145+
};
146+
let done, count = 0;
147+
do {
148+
const {
149+
data, meta,
150+
} = await fn(args);
151+
for (const item of data) {
152+
yield item;
153+
if (max && ++count >= max) {
154+
return;
155+
}
156+
done = args.params.page === meta.last_page;
157+
args.params.page++;
158+
}
159+
} while (!done);
9160
},
10161
},
11-
};
162+
};

components/insertchat/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/insertchat",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream InsertChat Components",
55
"main": "insertchat.app.mjs",
66
"keywords": [
@@ -11,5 +11,8 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3"
1417
}
15-
}
18+
}

0 commit comments

Comments
 (0)