-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_assembly_token.js
More file actions
82 lines (70 loc) · 2.37 KB
/
Copy pathtest_assembly_token.js
File metadata and controls
82 lines (70 loc) · 2.37 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const WebSocket = require('ws');
const https = require('https');
const apiKey = process.env.ASSEMBLYAI_API_KEY || '';
function getToken() {
return new Promise((resolve, reject) => {
const req = https.request('https://api.assemblyai.com/v2/realtime/token', {
method: 'POST',
headers: {
'Authorization': apiKey,
'Content-Type': 'application/json'
}
}, (res) => {
let data = '';
res.on('data', (chunk) => data += chunk);
res.on('end', () => {
try {
const json = JSON.parse(data);
if (json.token) resolve(json.token);
else reject(json.error || data);
} catch(e) { reject(e); }
});
});
// Try transcription_config object
req.write(JSON.stringify({
expires_in: 480,
transcription_config: { model: 'universal-1' }
}));
req.end();
});
}
async function testConnection(token, params) {
return new Promise((resolve) => {
const url = `wss://api.assemblyai.com/v2/realtime/ws?sample_rate=16000&token=${token}${params}`;
console.log(`Testing: ${url}`);
const ws = new WebSocket(url);
const timeout = setTimeout(() => {
console.log(" [TIMEOUT] Success?");
ws.close();
resolve(true);
}, 3000);
ws.on('open', () => {
console.log(` [OPEN] Connected.`);
});
ws.on('message', (data) => {
const msg = JSON.parse(data.toString());
if (msg.error) {
console.log(` [SERVICE ERROR] ${msg.error}`);
}
});
ws.on('close', (code, reason) => {
clearTimeout(timeout);
console.log(` [CLOSED] Code: ${code}, Reason: ${reason}`);
resolve(false);
});
ws.on('error', (e) => {
console.log(` [ERROR] ${e.message}`);
});
});
}
async function run() {
try {
const token = await getToken();
console.log("Got token:", token.slice(0, 10) + "...");
console.log("--- Testing universal-1 with TOKEN ---");
await testConnection(token, '&model=universal-1');
} catch(e) {
console.error("Failed to get token:", e);
}
}
run();