Skip to content

Commit ff349c4

Browse files
committed
[Components] poper #12738
Sources - New Lead
1 parent 1197319 commit ff349c4

File tree

4 files changed

+93
-81
lines changed

4 files changed

+93
-81
lines changed

components/poper/package.json

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

components/poper/poper.app.mjs

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,67 +9,48 @@ export default {
99
label: "Poper ID",
1010
description: "The ID of the Poper popup",
1111
async options() {
12-
const popups = await this.listPopups();
13-
return popups.map((popup) => ({
14-
label: popup.name,
15-
value: popup.id,
12+
const { popups } = await this.listPopups();
13+
return popups.map(({
14+
id: value, name: label,
15+
}) => ({
16+
label,
17+
value,
1618
}));
1719
},
1820
},
19-
apiKey: {
20-
type: "string",
21-
label: "API Key",
22-
description: "Your Poper API Key",
23-
secret: true,
24-
},
2521
},
2622
methods: {
27-
// this.$auth contains connected account data
28-
authKeys() {
29-
console.log(Object.keys(this.$auth));
30-
},
3123
_baseUrl() {
3224
return "https://api.poper.ai/general/v1";
3325
},
34-
async _makeRequest(opts = {}) {
35-
const {
36-
$ = this,
37-
method = "POST",
38-
path = "/",
39-
headers,
40-
...otherOpts
41-
} = opts;
26+
_data(data = {}) {
27+
return {
28+
...data,
29+
api_key: `${this.$auth.api_key}`,
30+
};
31+
},
32+
_makeRequest({
33+
$ = this, path, data, ...opts
34+
}) {
4235
return axios($, {
43-
...otherOpts,
44-
method,
36+
method: "POST",
4537
url: this._baseUrl() + path,
4638
headers: {
47-
...headers,
4839
"Content-Type": "application/x-www-form-urlencoded",
4940
},
41+
data: this._data(data),
42+
...opts,
5043
});
5144
},
52-
async listPopups() {
45+
listPopups() {
5346
return this._makeRequest({
5447
path: "/popup/list",
55-
data: `api_key=${this.$auth.api_key}`,
5648
});
5749
},
58-
async getPopupResponses({ poperId }) {
50+
listPoperResponses(opts = {}) {
5951
return this._makeRequest({
6052
path: "/popup/responses",
61-
data: `api_key=${this.$auth.api_key}&popup_id=${poperId}`,
62-
});
63-
},
64-
async emitNewLeadEvent({ poperId }) {
65-
const responses = await this.getPopupResponses({
66-
poperId,
67-
});
68-
responses.responses.forEach((response) => {
69-
this.$emit(response, {
70-
summary: `New lead from Poper ID: ${poperId}`,
71-
id: response.id,
72-
});
53+
...opts,
7354
});
7455
},
7556
},
Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
import { axios } from "@pipedream/platform";
1+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
22
import poper from "../../poper.app.mjs";
3+
import sampleEmit from "./test-event.mjs";
34

45
export default {
56
key: "poper-new-lead",
67
name: "New Lead from Poper Popup",
7-
description: "Emit new event when a new lead is obtained from Poper popups. [See the documentation](https://help.poper.ai/portal/en/kb/articles/view-popup-responses)",
8-
version: "0.0.{{ts}}",
8+
description: "Emit new event when a new lead is obtained from Poper popups.",
9+
version: "0.0.1",
910
type: "source",
10-
dedupe: "unique",
1111
props: {
1212
poper,
1313
db: "$.service.db",
1414
timer: {
15+
label: "Polling interval",
16+
description: "Pipedream will poll the Poper API on this schedule",
1517
type: "$.interface.timer",
1618
default: {
17-
intervalSeconds: 60,
19+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
1820
},
1921
},
2022
poperId: {
@@ -25,48 +27,49 @@ export default {
2527
},
2628
},
2729
methods: {
28-
_getLastLeadId() {
29-
return this.db.get("lastLeadId");
30+
_getLastId() {
31+
return this.db.get("lastId") || 0;
3032
},
31-
_setLastLeadId(id) {
32-
this.db.set("lastLeadId", id);
33+
_setLastId(lastId) {
34+
this.db.set("lastId", lastId);
35+
},
36+
getParams() {
37+
return {};
38+
},
39+
async startEvent(maxResults = 0) {
40+
const lastId = this._getLastId();
41+
const { responses } = await this.poper.listPoperResponses({
42+
maxResults,
43+
data: {
44+
popup_id: this.poperId,
45+
},
46+
});
47+
48+
const filteredResponse = responses.filter((item) => item.id > lastId);
49+
50+
if (filteredResponse.length) {
51+
if (maxResults && filteredResponse.length > maxResults) {
52+
filteredResponse.length = maxResults;
53+
}
54+
this._setLastId(filteredResponse[0].id);
55+
}
56+
57+
for (const item of filteredResponse.reverse()) {
58+
this.$emit( item, {
59+
id: item.id,
60+
summary: `New lead with Id: ${item.id}`,
61+
ts: Date.parse(item.time_stamp),
62+
});
63+
}
3364
},
3465
},
3566
hooks: {
3667
async deploy() {
37-
await this.emitNewLeads();
38-
},
39-
async activate() {
40-
await this.emitNewLeads();
41-
},
42-
async deactivate() {
43-
// No specific deactivation logic required
68+
await this.startEvent(25);
4469
},
4570
},
4671
async run() {
47-
await this.emitNewLeads();
48-
},
49-
async emitNewLeads() {
50-
const poperId = this.poperId;
51-
const responses = await this.poper.getPopupResponses({
52-
poperId,
53-
});
54-
const lastLeadId = this._getLastLeadId();
55-
let newLastLeadId = lastLeadId;
56-
57-
for (const response of responses.responses) {
58-
if (!lastLeadId || response.id > lastLeadId) {
59-
this.$emit(response, {
60-
summary: `New lead from Poper ID: ${poperId}`,
61-
id: response.id,
62-
ts: Date.now(),
63-
});
64-
newLastLeadId = response.id;
65-
}
66-
}
67-
68-
if (newLastLeadId !== lastLeadId) {
69-
this._setLastLeadId(newLastLeadId);
70-
}
72+
await this.startEvent();
7173
},
74+
sampleEmit,
7275
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export default {
2+
"id": 6625,
3+
"popup_id": 1233,
4+
"time_stamp": "2024-07-05T16:22:23.830Z",
5+
"response": {
6+
"email": "[email protected]"
7+
},
8+
"system": {
9+
"page": "/",
10+
"referrer": "https://site.com.br/wp-admin/admin.php?page=poper-settings",
11+
"language": "pt-BR",
12+
"os": "Windows",
13+
"browser": "Chrome",
14+
"ip": "123.456.78.90",
15+
"city": "São Paulo",
16+
"country": "BR",
17+
"continent": "SA",
18+
"latitude": "-22.123421",
19+
"longitude": "-49.657476",
20+
"postal_code": "05040001",
21+
"region": "São Paulo",
22+
"region_code": "SP"
23+
}
24+
}

0 commit comments

Comments
 (0)