-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
100 lines (76 loc) · 3.12 KB
/
test.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
const axios = require('axios');
const { twilioConfig } = require('./src/config/twilio.config');
// Twilio API base URL
const twilioBaseUrl = 'https://api.twilio.com/2010-04-01';
// Encode the Twilio API key as a base64 string
const credentials = Buffer.from(`${twilioConfig.accountSid}:${twilioConfig.authToken}`).toString('base64');
const authHeader = `Basic ${credentials}`;
// Twilio account details
const accountId = 'ACc95dd4d2c9fde917389558818b899607';
const callId = 'CAdbb9c1da81c82c0e6d29389987d7cd9f';
// Twilio API URLs
const callUrl = `${twilioBaseUrl}/Accounts/${accountId}/Calls/${callId}.json`;
axios.request({
method: 'get',
maxBodyLength: Infinity,
url: callUrl,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': authHeader,
}
})
.then((response) => {
const recordingsUrl = `https://api.twilio.com/${response.data.subresource_uris.recordings}`;
const balanceUrl = `https://api.twilio.com/2010-04-01/Accounts/${accountId}/Balance.json`;
let callCounts = {}; // Object to store call counts for each owned number
// Fetch recordings
axios.get(recordingsUrl, {
headers: {
'Authorization': authHeader,
}
})
.then((response) => {
const data = response.data;
const recordings = data.recordings;
recordings.forEach(recording => {
const phoneNumber = recording.to;
callCounts[phoneNumber] = (callCounts[phoneNumber] || 0) + 1;
console.log(recording.media_url);
});
})
.catch((error) => {
console.log("Error fetching recordings:", error);
});
})
.catch((error) => {
console.log("Error fetching call details:", error);
});
// ! this is for future reference
// ! twilioApiModule.js
// const axios = require('axios');
// const { twilioConfig } = require('./src/config/twilio.config');
// const twilioBaseUrl = 'https://api.twilio.com/2010-04-01';
// const getMediaUrl = async (callId) => {
// const credentials = Buffer.from(`${twilioConfig.accountSid}:${twilioConfig.authToken}`).toString('base64');
// const authHeader = `Basic ${credentials}`;
// const callUrl = `${twilioBaseUrl}/Accounts/${twilioConfig.accountSid}/Calls/${callId}.json`;
// try {
// const response = await axios.get(callUrl, {
// headers: {
// 'Content-Type': 'application/x-www-form-urlencoded',
// 'Authorization': authHeader,
// }
// });
// const recordingsUrl = `${twilioBaseUrl}${response.data.subresource_uris.recordings}`;
// const recordingsResponse = await axios.get(recordingsUrl, {
// headers: {
// 'Authorization': authHeader,
// }
// });
// const mediaUrl = recordingsResponse.data.recordings[0].media_url;
// return mediaUrl;
// } catch (error) {
// throw error;
// }
// };
// module.exports = { getMediaUrl };