-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.js
More file actions
59 lines (52 loc) · 1.44 KB
/
api.js
File metadata and controls
59 lines (52 loc) · 1.44 KB
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
const axios = require('axios');
const crypto = require('crypto');
const uuidv4 = require('uuid/v4');
class Api {
constructor(key, secret, user) {
this.CSML_API_CLIENT_KEY = key;
this.CSML_API_SECRET_KEY = secret;
this.CSML_USER_ID = user;
}
buildKeys() {
const { CSML_API_CLIENT_KEY, CSML_API_SECRET_KEY } = this;
const UNIX_TIMESTAMP = Math.floor(Date.now() / 1000);
const XApiKey = `${CSML_API_CLIENT_KEY}|${UNIX_TIMESTAMP}`;
const signature = crypto.createHmac('sha256', CSML_API_SECRET_KEY)
.update(XApiKey, 'utf-8')
.digest('hex');
const XApiSignature = `sha256=${signature}`;
return { XApiKey, XApiSignature }
}
async getConv(input) {
const { XApiKey, XApiSignature } = this.buildKeys();
return axios({
method: 'post',
url: 'https://clients.csml.dev/prod/api/chat',
headers: {
'X-Api-Key': XApiKey,
'X-Api-Signature': XApiSignature,
},
data: {
client: {
user_id: this.CSML_USER_ID
},
metadata: {},
request_id: uuidv4(),
payload: {
content: {
text: input
},
content_type: "text"
}
}
})
.then(response => {
return response.data
})
.catch(error => {
console.log("IMPOSSIBLE TO CONNECT TO CSML STUDIO");
throw console.error(error);
});
}
};
module.exports.Api = Api;