@tiledesk/tiledesk-voice-twilio-connector 0.1.14 → 0.1.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,82 @@
1
+
2
+ var winston = require('../../winston');
3
+ const axios = require("axios").default;
4
+ const FormData = require('form-data');
5
+
6
+ /*UTILS*/
7
+ const fileUtils = require('../fileUtils.js')
8
+
9
+ class IntegrationService {
10
+
11
+ constructor(config) {
12
+
13
+ if (!config) {
14
+ throw new Error("[IntegrationService] config is mandatory");
15
+ }
16
+ if (!config.API_URL) {
17
+ throw new Error("[IntegrationService] config.API_URL is mandatory");
18
+ }
19
+
20
+ this.API_URL = config.API_URL;
21
+
22
+ }
23
+
24
+
25
+ async getKeyFromIntegrations(id_project, integration_name, token){
26
+
27
+ winston.debug('[IntegrationService] getKeyFromIntegrations id_project:', id_project)
28
+
29
+ return await axios({
30
+ url: this.API_URL + "/"+ id_project + "/integration/name/" + integration_name,
31
+ headers: {
32
+ 'Content-Type': 'application/json',
33
+ 'Authorization': token
34
+ },
35
+ data: {},
36
+ method: "GET",
37
+ }).then( async (response) => {
38
+ if (!response.data || response.data?.value) {
39
+ return null;
40
+ }
41
+
42
+ return response.data?.value?.apikey
43
+ })
44
+ .catch((err) => {
45
+ winston.error("[IntegrationService] getKeyFromIntegrations error: ", err.response?.data);
46
+ return null;
47
+ });
48
+
49
+
50
+ }
51
+
52
+ async getKeyFromKbSettings(id_project, token) {
53
+
54
+ winston.debug('[IntegrationService] getKeyFromIntegrations id_project:', id_project)
55
+
56
+ return await axios({
57
+ url: this.API_URL + "/"+ id_project + "/kbsettings",
58
+ headers: {
59
+ 'Content-Type': 'application/json',
60
+ 'Authorization': token
61
+ },
62
+ data: {},
63
+ method: "GET",
64
+ }).then( async (response) => {
65
+ if (!response.data || response.data?.gptkey) {
66
+ return null;
67
+ }
68
+
69
+ return response.data?.gptkey
70
+ })
71
+ .catch((err) => {
72
+ winston.error("[IntegrationService] getKeyFromKbSettings error: ", err.response?.data);
73
+ return null;
74
+ });
75
+
76
+ }
77
+
78
+
79
+
80
+ }
81
+
82
+ module.exports = { IntegrationService };
@@ -0,0 +1,79 @@
1
+ var winston = require('../../winston');
2
+ const axios = require("axios").default;
3
+ const FormData = require('form-data');
4
+ const fs = require('fs');
5
+ const path = require('path');
6
+
7
+ /*UTILS*/
8
+ const fileUtils = require('../fileUtils.js')
9
+
10
+ class UploadService {
11
+
12
+ constructor(config) {
13
+
14
+ if (!config) {
15
+ throw new Error("[UploadService] config is mandatory");
16
+ }
17
+ if (!config.API_URL) {
18
+ throw new Error("[UploadService] config.API_URL is mandatory");
19
+ }
20
+
21
+ if (config.user) {
22
+ this.user = config.user
23
+ }
24
+
25
+ this.API_URL = config.API_URL;
26
+
27
+ }
28
+
29
+ async upload(id, file, user) {
30
+
31
+ winston.debug(`[UploadService] upload for id ${id} and user ${user}`);
32
+
33
+
34
+ // Step 2: Salva il file MP3 localmente (temporaneamente)
35
+ const tempFilePath = path.join(__dirname, `speech_${user._id}_${id}.wav`);
36
+ fs.writeFileSync(tempFilePath, file);
37
+
38
+ // Step 3: Carica il file al tuo server
39
+ const formData = new FormData();
40
+ formData.append('file', fs.createReadStream(tempFilePath), {
41
+ filename: `audiofile_${user._id}_${id}.wav`,
42
+ contentType: 'audio/mpeg'
43
+ });
44
+
45
+
46
+ return new Promise((resolve, reject) => {
47
+
48
+ //const formData = new FormData();
49
+ //formData.append('file', file, { filename: 'audiofile_'+user._id+'_'+id+'.mp3', contentType: 'audio/mpeg' });
50
+
51
+ axios({
52
+ url: this.API_URL + "/files/users",
53
+ headers: {
54
+ ...formData.getHeaders(),
55
+ "Authorization": user.token
56
+ },
57
+ data: formData,
58
+ method: 'POST'
59
+ }).then((resbody) => {
60
+ if(resbody?.data){
61
+
62
+ // Step 4: Pulisci il file temporaneo
63
+ fs.unlinkSync(tempFilePath);
64
+
65
+ let fileUrl = this.API_URL + "/files?path="+resbody?.data.filename
66
+ resolve(fileUrl);
67
+ }
68
+
69
+ }).catch((err) => {
70
+ console.log('err', err)
71
+ reject(err);
72
+ })
73
+
74
+ })
75
+ }
76
+
77
+ }
78
+
79
+ module.exports = { UploadService };