forked from PipedreamHQ/pipedream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathringcentral.app.js
214 lines (204 loc) · 5.8 KB
/
ringcentral.app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const axios = require("axios");
module.exports = {
type: "app",
app: "ringcentral",
propDefinitions: {
extensionId: {
type: "string",
label: "Extension",
description: "The extension (or user) that will trigger the event source",
async options(context) {
const { page } = context;
const { records: extensions } = await this._getExtensionList({
// Pages in the RingCentral API are 1-indexed
page: page + 1,
perPage: 10,
status: "Enabled",
});
const options = extensions.map((extension) => {
const {
id: value,
extensionNumber,
contact: {
firstName,
lastName,
},
} = extension;
const label = `${firstName} ${lastName} (ext: ${extensionNumber})`;
return {
label,
value,
};
});
return {
options,
context: {
nextPage: page + 1,
},
};
},
},
deviceId: {
type: "string",
label: "Device",
description: "The extension's device that will trigger the event source",
default: "",
async options(context) {
const {
page,
extensionId,
} = context;
const { records: devices } = await this._getDeviceList(extensionId, {
// Pages in the RingCentral API are 1-indexed
page: page + 1,
perPage: 10,
});
const options = devices.map((extension) => {
const {
id: value,
type,
name,
} = extension;
const label = `${name} (type: ${type})`;
return {
label,
value,
};
});
return {
options,
context: {
nextPage: page + 1,
extensionId,
},
};
},
},
},
methods: {
_authToken() {
return this.$auth.oauth_access_token;
},
_apiUrl() {
const {
base_url: baseUrl = "https://platform.ringcentral.com/restapi/v1.0",
} = this.$auth;
return baseUrl;
},
_accountUrl() {
const baseUrl = this._apiUrl();
return `${baseUrl}/account/~`;
},
_extensionUrl() {
const baseUrl = this._accountUrl();
return `${baseUrl}/extension`;
},
_deviceUrl(extensionId = "~") {
const baseUrl = this._extensionUrl();
return `${baseUrl}/${extensionId}/device`;
},
_callLogUrl(extensionId = "~") {
const baseUrl = this._extensionUrl();
return `${baseUrl}/${extensionId}/call-log`;
},
_subscriptionUrl(id) {
const baseUrl = this._apiUrl();
const basePath = "/subscription";
const path = id ? `${basePath}/${id}` : basePath;
return `${baseUrl}${path}`;
},
_makeRequestConfig() {
const authToken = this._authToken();
const headers = {
"Authorization": `Bearer ${authToken}`,
"User-Agent": "@PipedreamHQ/pipedream v0.1",
};
return {
headers,
};
},
async _getExtensionList(params) {
// `params` refers to the query params listed in the API docs:
// https://developers.ringcentral.com/api-reference/Extensions/listExtensions
const url = this._extensionUrl();
const requestConfig = {
...this._makeRequestConfig(),
params,
};
const { data } = await axios.get(url, requestConfig);
return data;
},
async _getDeviceList(extensionId, params) {
// `params` refers to the query params listed in the API docs:
// https://developers.ringcentral.com/api-reference/Devices/listExtensionDevices
const url = this._deviceUrl(extensionId);
const requestConfig = {
...this._makeRequestConfig(),
params,
};
const { data } = await axios.get(url, requestConfig);
return data;
},
async _getExtensionCallLog(extensionId, params, nextPage = {}) {
// `params` refers to the query params listed in the API docs:
// https://developers.ringcentral.com/api-reference/Call-Log/readUserCallLog
const {
uri: url = this._callLogUrl(extensionId),
} = nextPage;
const requestConfig = {
...this._makeRequestConfig(),
params,
};
const { data } = await axios.get(url, requestConfig);
return data;
},
async *getCallRecordings(extensionId, dateFrom, dateTo) {
const params = {
dateFrom,
dateTo,
withRecording: true,
};
let nextPage = {};
do {
const {
records,
navigation,
} = await this._getExtensionCallLog(extensionId, params, nextPage);
for (const record of records) {
yield record;
}
nextPage = navigation.nextPage;
} while (nextPage);
},
async createHook({
address,
eventFilters,
verificationToken,
}) {
const url = this._subscriptionUrl();
const requestConfig = this._makeRequestConfig();
// Details about the different webhook parameters can be found in the
// RingCentral API docs:
// https://developers.ringcentral.com/api-reference/Subscriptions/createSubscription
const requestData = {
eventFilters,
deliveryMode: {
transportType: "WebHook",
address,
verificationToken,
expiresIn: 630720000, // 20 years (max. allowed by the API)
},
};
const { data } = await axios.post(url, requestData, requestConfig);
return {
...data,
verificationToken,
};
},
deleteHook(hookId) {
const url = this._subscriptionUrl(hookId);
const requestConfig = this._makeRequestConfig();
return axios.delete(url, requestConfig);
},
},
};