Skip to content

Commit e4c0d03

Browse files
committed
Adding 'List Articles' action
1 parent 9d0204c commit e4c0d03

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import utils from "../../common/utils.mjs";
2+
import app from "../../trengo.app.mjs";
3+
4+
export default {
5+
type: "action",
6+
key: "trengo-list-articles",
7+
version: "0.0.1",
8+
name: "List Articles",
9+
description: "List articles from a help center according to the specified criteria. [See the docs](https://developers.trengo.com/reference/list-all-articles)",
10+
props: {
11+
app,
12+
helpCenterId: {
13+
propDefinition: [
14+
app,
15+
"helpCenterId",
16+
],
17+
},
18+
localeCode: {
19+
type: "string",
20+
label: "Locale Code",
21+
description: "The article's locale code",
22+
optional: true,
23+
default: "en",
24+
},
25+
filter: {
26+
type: "string",
27+
label: "Filter",
28+
description: "The article's filter. You can choose one of the available options, specify `untranslated_<language_code>` for other language codes, or leave empty for all articles",
29+
optional: true,
30+
options: [
31+
"draft",
32+
"published",
33+
"untranslated_en",
34+
],
35+
},
36+
term: {
37+
type: "string",
38+
label: "Search Term",
39+
description: "The article's search term (if not specified, all articles will be returned)",
40+
optional: true,
41+
},
42+
},
43+
async run({ $ }) {
44+
const articles = [];
45+
const resourcesStream = utils.getResourcesStream({
46+
resourceFn: this.app.getArticles,
47+
resourceFnArgs: {
48+
helpCenterId: this.helpCenterId,
49+
params: {
50+
localeCode: this.localeCode,
51+
filter: this.filter,
52+
term: this.term,
53+
},
54+
},
55+
});
56+
for await (const item of resourcesStream) {
57+
articles.push(item);
58+
}
59+
const length = articles.length;
60+
$.export("$summary", `Successfully retrieved ${length} article${length === 1
61+
? ""
62+
: "s"}`);
63+
return articles;
64+
},
65+
};

components/trengo/trengo.app.mjs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,22 @@ export default {
146146
description: "Search term to find a contact. If not given, all contacts will be returned.",
147147
optional: true,
148148
},
149+
helpCenterId: {
150+
type: "integer",
151+
label: "Help Center ID",
152+
description: "Select a help center or provide an ID",
153+
async options({ page = 0 }) {
154+
const response = await this.getHelpCenters({
155+
params: {
156+
page: page + 1,
157+
},
158+
});
159+
return response.data.map((helpCenter) => ({
160+
label: helpCenter.name || helpCenter.slug,
161+
value: helpCenter.id,
162+
}));
163+
},
164+
},
149165
},
150166
methods: {
151167
_getUrl(path) {
@@ -241,5 +257,19 @@ export default {
241257
...args,
242258
});
243259
},
260+
async getHelpCenters(args = {}) {
261+
return this._makeRequest({
262+
path: "/help_center",
263+
...args,
264+
});
265+
},
266+
async getArticles({
267+
helpCenterId, ...args
268+
} = {}) {
269+
return this._makeRequest({
270+
path: `/help_center/${helpCenterId}/articles`,
271+
...args,
272+
});
273+
},
244274
},
245275
};

0 commit comments

Comments
 (0)