Skip to content

Commit 9a62303

Browse files
committed
Added HelpCenterQuery npm
1 parent e26eeb2 commit 9a62303

File tree

10 files changed

+2455
-0
lines changed

10 files changed

+2455
-0
lines changed

tiledesk-chatbot-util/test/test_parse_directives.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,20 @@ JUST WAIT A MOMENT`);
149149
assert(result.findDirective("Dir2NoParam").name === "Dir2NoParam");
150150
assert(result.findDirective("Dir3").name === "Dir3");
151151
});
152+
153+
it('Finds 1 directive', function() {
154+
const msg = "Andrea\n\\_tdWhenOfflineHours reply_with:offline_message";
155+
let result = TiledeskChatbotUtil.parseDirectives(msg);
156+
console.log("result:", JSON.stringify(result));
157+
console.log("directives:", JSON.stringify(result.directives));
158+
assert(result != null);
159+
assert(result.directives != null);
160+
assert.strictEqual(result.directives.length, 1);
161+
assert(result.directives[0]);
162+
163+
assert(result.directives[0].name === "WhenOfflineHours");
164+
assert(result.directives[0].parameter === "reply_with:offline_message");
165+
assert(result.text.trim() === "Andrea");
166+
assert(result.findDirective("WhenOfflineHours").name === "WhenOfflineHours");
167+
});
152168
});

tiledesk-helpcenter-client/.env

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
PASSWORD=testtest
3+
PROJECT_ID=630858150da8e800358687cf
4+
PROJECT_NAME=HelpCenter Test
5+
WORKSPACE_ID=6308582654a7290023c4a3cf
6+
API_ENDPOINT=https://tiledesk-cms-server-prod.herokuapp.com
7+
APIKEY=____TODO____
8+
LOG_STATUS=false
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Client library for Helpcenter query REST APIs
2+
3+
This library is on npm: https://www.npmjs.com/package/@tiledesk/helpcenter-query-client
4+
5+
6+
### 0.1.4
7+
- Added README.md
8+
9+
### 0.1.3
10+
- first public release

tiledesk-helpcenter-client/README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
Full [NodeJS API reference](https://tiledesk.github.io/tiledesk-nodejs-libs/TiledeskClient.html) is hosted on Github
2+
3+
# Introduction
4+
5+
Follow this guide to use the Tiledesk Helpcenter JavaScript SDK in your ``Node.js`` application.
6+
7+
Before you can add Tiledesk Helpcenter npm lib to your Node.js app, you need a [Tiledesk account](https://docs.tiledesk.com/knowledge-base/creating-a-tiledesk-account/) and a [Tiledesk project](https://docs.tiledesk.com/knowledge-base/creating-a-tiledesk-account/). Once you create your project you will have a _projectID_, a _user_ to play with project APIs.
8+
Next you must create your own Helpcenter workspace (in one Tiledesk project you can create more Workspaces)
9+
10+
# Add Helpcenter to your project
11+
12+
Install ``HelpCenterQuery`` library with *npm* command:
13+
14+
```
15+
npm install @tiledesk/helpcenter-query-client
16+
```
17+
18+
Alternatively use package.json to import the library in the "dependencies" property, as in the following example:
19+
20+
```json
21+
{
22+
"name": "Hello Tiledesk nodeJS",
23+
"version": "1.0.0",
24+
"description": "",
25+
"main": "index.js",
26+
"scripts": {
27+
"test": "echo \"Error: no test specified\" && exit 1",
28+
"start": "node index.js"
29+
},
30+
"keywords": [],
31+
"author": "",
32+
"license": "ISC",
33+
"dependencies": {
34+
"@tiledesk/helpcenter-query-client": "^0.1.3"
35+
}
36+
}
37+
```
38+
39+
Then run
40+
41+
```
42+
npm install
43+
```
44+
45+
Once installed you can import ```HelpCenterQuery()``` class in your Node.js file using the "require" command:
46+
47+
```
48+
const { HelpCenterQuery } = require('@tiledesk/helpcenter-query-client');
49+
```
50+
51+
# The HelpCenterQuery class
52+
53+
To interact with Tiledesk Helpcenter APIs and query your documents you need to create an instance of a HelpCenterQuery() class using his constructor. You MUST supply an *APIKEY*, an existing *Project ID* and a valid *Workspace ID*.
54+
55+
Please refer to the official [Tiledesk Help Center guide](https://gethelp.tiledesk.com/articles/getting-started-with-help-center/) to better understand how Help Center works and how to get your *Workspace ID*.
56+
57+
In the next example we first authenticate using our user credentials, then we create a new TiledeskClient instance using a ``PROJECT_ID`` and the ``token`` we got from authentication:
58+
59+
```javascript
60+
const helpcenter = new HelpCenterQuery({
61+
APIKEY: "__",
62+
projectId: "YOUR_PROJECT_ID",
63+
workspaceId: "YOUR_HELPCENTER_WORKSPACE_ID",
64+
log: false
65+
});
66+
```
67+
68+
# Query your Helpcenter Workspace
69+
70+
Once you created your Helpcenter instance you can query your workspace as in the following example:
71+
72+
```javascript
73+
const helpcenter = new HelpCenterQuery({
74+
APIKEY: APIKEY,
75+
projectId: PROJECT_ID,
76+
workspaceId: WORKSPACE_ID
77+
});
78+
text_to_search = "military";
79+
maxresults = 3;
80+
helpcenter.search(text_to_search, maxresults, (err, results) => {
81+
console.log("results:", results)
82+
assert(results[0].title)
83+
assert(results[0].score)
84+
assert(results[0].url)
85+
});
86+
```
87+
88+
### Specify the API endpoint in instance methods
89+
90+
If instead you are using instance methods working with an instance of TiledeskClient, you must specify the parameter in the constructor config object as config.APIRUL:
91+
92+
```javascript
93+
const helpcenter = new HelpCenterQuery({
94+
APIKEY: APIKEY,
95+
projectId: PROJECT_ID,
96+
workspaceId: WORKSPACE_ID,
97+
APIURL: API_ENDPOINT,
98+
log: true
99+
});
100+
```
101+
102+
103+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
jsdoc index.js -d ../docs

tiledesk-helpcenter-client/index.js

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
let axios = require('axios');
2+
let https = require("https");
3+
4+
class HelpCenterQuery {
5+
6+
static DEFAULT_API_ENDPOINT = "https://tiledesk-cms-server-prod.herokuapp.com";
7+
8+
/**
9+
* Constructor for HelpCenterQuery Client object
10+
*
11+
* @example
12+
* const { HelpCenterQuery } = require('@tiledesk/helpcenter-query-client');
13+
* const helpcenter = new HelpCenterQuery({APIKEY: 'THE_API_KEY', projectId: 'YOUR_PROJECT_ID', workspaceId: 'HELPCENTER_WORKSPACE'});
14+
* const helpcenter = new HelpCenterQuery({APIKEY: 'THE_API_KEY', projectId: 'YOUR_PROJECT_ID', workspaceId: 'HELPCENTER_WORKSPACE', APIURL: 'OPTIONAL_SELF_HOSTED_INSTANCE_ENDPOINT', log: "OPTIONAL BOOLEAN true/false"});
15+
* @param {Object} options JSON configuration.
16+
* @param {string} options.APIKEY Mandatory. Tiledesk APIKEY
17+
* @param {string} options.projectId Mandatory. Tiledesk projectId. Will be used in each call on project's APIs.
18+
* @param {string} options.APIURL Optional. Tiledesk server API endpoint.
19+
* @param {boolean} options.log Optional. If true HTTP requests are logged.
20+
*/
21+
constructor(options) {
22+
if (!options) {
23+
throw new Error('options.APIKEY, options.projectId and options.token are mandatory.');
24+
}
25+
26+
if (!options.APIKEY) {
27+
throw new Error('options.APIKEY can NOT be null.');
28+
}
29+
else {
30+
this.APIKEY = options.APIKEY;
31+
}
32+
33+
if (options && options.APIURL) {
34+
this.APIURL = options.APIURL
35+
}
36+
else {
37+
this.APIURL = HelpCenterQuery.DEFAULT_API_ENDPOINT;
38+
}
39+
40+
if (!options.projectId) {
41+
throw new Error('options.projectId can NOT be null.');
42+
}
43+
else {
44+
this.projectId = options.projectId;
45+
}
46+
47+
if (!options.workspaceId) {
48+
throw new Error('options.workspaceId can NOT be null.');
49+
}
50+
else {
51+
this.workspaceId = options.workspaceId;
52+
}
53+
54+
this.log = false;
55+
if (options.log) {
56+
this.log = options.log;
57+
}
58+
}
59+
60+
search(text, maxResults, callback) {
61+
62+
const escaped_text = encodeURI(text);
63+
var url = this.APIURL + `/${this.projectId}/${this.workspaceId}/contents/search?text=${escaped_text}&maxresults=${maxResults}`
64+
65+
const HTTPREQUEST = {
66+
url: url,
67+
headers: {
68+
},
69+
method: 'GET'
70+
};
71+
this.myrequest(
72+
HTTPREQUEST,
73+
function(err, resbody) {
74+
if (err) {
75+
if (callback) {
76+
callback(err);
77+
}
78+
}
79+
else {
80+
if (callback) {
81+
callback(null, resbody);
82+
}
83+
}
84+
}, this.log
85+
);
86+
}
87+
88+
myrequest(options, callback, log) {
89+
//console.log("API URL:", options.url);
90+
//console.log("** Options:", options);
91+
const httpsAgent = new https.Agent({
92+
rejectUnauthorized: false,
93+
});
94+
axios(
95+
{
96+
url: options.url,
97+
method: options.method,
98+
data: options.json,
99+
httpsAgent: httpsAgent,
100+
headers: options.headers
101+
})
102+
.then(function (res) {
103+
if (log) {
104+
console.log("Response for url:", options.url);
105+
console.log("Response headers:\n", res.headers);
106+
console.log("******** Response for url:", res);
107+
console.log("Response body:\n", res.data);
108+
}
109+
if (res && res.status == 200 && res.data) {
110+
if (callback) {
111+
callback(null, res.data);
112+
}
113+
}
114+
else {
115+
if (callback) {
116+
callback(TiledeskClient.getErr({message: "Response status not 200"}, options, res), null, null);
117+
}
118+
}
119+
})
120+
.catch(function (error) {
121+
console.error("An error occurred:", error);
122+
if (callback) {
123+
callback(error, null, null);
124+
}
125+
});
126+
}
127+
}
128+
129+
module.exports = { HelpCenterQuery };

0 commit comments

Comments
 (0)