Skip to content

Commit 89f40af

Browse files
committed
Adding polling source
1 parent 6af21c2 commit 89f40af

File tree

3 files changed

+136
-7
lines changed

3 files changed

+136
-7
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
2+
import yayCom from "../../yay_com.app.mjs";
3+
4+
export default {
5+
props: {
6+
yayCom,
7+
db: "$.service.db",
8+
timer: {
9+
type: "$.interface.timer",
10+
default: {
11+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
12+
},
13+
},
14+
},
15+
methods: {
16+
_getSavedIds() {
17+
return this.db.get("savedIds") || [];
18+
},
19+
_setSavedIds(ids) {
20+
this.db.set("savedIds", ids);
21+
},
22+
async startEvent() {
23+
const savedIds = this._getSavedIds();
24+
const items = await this.getItems(savedIds);
25+
26+
const newIds = [];
27+
for (const item of items) {
28+
const id = this.getItemId(item);
29+
if (!savedIds.includes(id)) {
30+
const meta = this.generateMeta(item);
31+
this.$emit(item, meta);
32+
newIds.push(id);
33+
}
34+
}
35+
36+
if (newIds.length > 0) {
37+
// Keep only the most recent IDs to prevent the array from growing indefinitely
38+
const ids = [
39+
...savedIds,
40+
...newIds,
41+
].slice(-100);
42+
this._setSavedIds(ids);
43+
}
44+
},
45+
},
46+
async run() {
47+
await this.startEvent();
48+
},
49+
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import common from "../common/polling.mjs";
2+
3+
export default {
4+
...common,
5+
key: "yay_com-new-contact-added",
6+
name: "New Contact Added",
7+
description: "Emit new event when a new contact is added to a phone book. [See the documentation](https://www.yay.com/voip/api-docs/phone-books/phone-book-contact/)",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
props: {
12+
...common.props,
13+
phoneBookId: {
14+
propDefinition: [
15+
common.props.yayCom,
16+
"phoneBookId",
17+
],
18+
},
19+
},
20+
methods: {
21+
...common.methods,
22+
generateMeta(contact) {
23+
const name = this.getItemId(contact);
24+
return {
25+
id: name,
26+
summary: `New Contact: ${name}`,
27+
ts: Date.now(),
28+
};
29+
},
30+
getItemId(contact) {
31+
let name = `${contact.first_name} ${contact.last_name}`;
32+
if (!name.trim()) {
33+
name = contact.company_name;
34+
}
35+
return name;
36+
},
37+
async getItems() {
38+
const { phoneBookId } = this;
39+
const contacts = await this.yayCom.listPhoneBookContacts({
40+
phoneBookId,
41+
params: {
42+
sort: "id",
43+
limit: 100,
44+
uuid: phoneBookId,
45+
},
46+
});
47+
return contacts || [];
48+
},
49+
},
50+
};

components/yay_com/yay_com.app.mjs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export default {
99
label: "SIP User",
1010
description: "The SIP user to make the outbound call for",
1111
async options() {
12-
const { result } = await this.listSipUsers();
13-
return result?.map(({
12+
const users = await this.listSipUsers();
13+
return users?.map(({
1414
uuid: value, display_name, user_name,
1515
}) => ({
1616
label: display_name || user_name,
@@ -24,8 +24,8 @@ export default {
2424
description: "List of SIP users who will receive the outbound call request",
2525
optional: true,
2626
async options() {
27-
const { result } = await this.listSipUsers();
28-
return result?.map(({
27+
const users = await this.listSipUsers();
28+
return users?.map(({
2929
uuid: value, display_name, user_name,
3030
}) => ({
3131
label: display_name || user_name,
@@ -39,8 +39,22 @@ export default {
3939
description: "List of hunt groups who will receive the outbound call request",
4040
optional: true,
4141
async options() {
42-
const { result } = await this.listHuntGroups();
43-
return result?.map(({
42+
const groups = await this.listHuntGroups();
43+
return groups?.map(({
44+
uuid: value, name: label,
45+
}) => ({
46+
label,
47+
value,
48+
})) || [];
49+
},
50+
},
51+
phoneBookId: {
52+
type: "string",
53+
label: "Phone Book",
54+
description: "The phone book to monitor for new contacts",
55+
async options() {
56+
const books = await this.listPhoneBooks();
57+
return books?.map(({
4458
uuid: value, name: label,
4559
}) => ({
4660
label,
@@ -101,7 +115,23 @@ export default {
101115
},
102116
async listPhoneBooks(args) {
103117
return this._makeRequest({
104-
path: "/phone-book",
118+
path: "/voip/phone-book",
119+
...args,
120+
});
121+
},
122+
async listPhoneBookContacts({
123+
phoneBookId, ...args
124+
}) {
125+
return this._makeRequest({
126+
path: `/voip/phone-book/${phoneBookId}`,
127+
...args,
128+
});
129+
},
130+
async listMailboxMessages({
131+
mailboxId, ...args
132+
}) {
133+
return this._makeRequest({
134+
path: `/voip/mailbox/${mailboxId}/messages`,
105135
...args,
106136
});
107137
},

0 commit comments

Comments
 (0)