-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
130 lines (108 loc) · 4.73 KB
/
index.js
File metadata and controls
130 lines (108 loc) · 4.73 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const axios = require('axios');
const io = require('socket.io-client');
class ChatterBox {
constructor(config) {
if (!config.authorizationToken) {
throw new Error('Authorization token is required');
}
this.authorizationToken = config.authorizationToken;
this.apiBaseUrl = config.apiBaseUrl || 'https://bot.chatter-box.io';
this.wsBaseUrl = config.wsBaseUrl || 'https://ws.chatter-box.io';
}
async getTemporaryToken(expiresIn = 3600) {
try {
if (expiresIn < 60 || expiresIn > 86400) {
throw new Error('Expiration time must be between 60 and 86400 seconds');
}
const response = await axios.post(`${this.apiBaseUrl}/token`, {
expiresIn
}, {
headers: {
Authorization: `Bearer ${this.authorizationToken}`,
'Content-Type': 'application/json'
}
});
return {
token: response.data.token,
expiresIn: response.data.expiresIn
};
} catch (error) {
if (error.response && error.response.data && error.response.data.message) {
throw new Error(error.response.data.message);
} else if (error.request) {
throw new Error('No response from server. Please check your network or try again later.');
} else {
throw new Error('Unexpected error: ' + error.message);
}
}
}
async sendBot({platform, meeting_id, meeting_password, bot_name = 'ChatterBox', webhook_url, model = 'nova-3', language = 'multi', noTranscriptTimeoutSeconds, noParticipantsLeftTimeoutSeconds}) {
try {
if (!platform || (typeof meeting_id !== 'string' && typeof meeting_id !== 'number') || String(meeting_id).trim() === '') {
throw new Error('Platform and meeting ID are required');
}
if (!this.authorizationToken) {
throw new Error('Authorization token is missing or invalid');
}
if (noTranscriptTimeoutSeconds !== undefined && typeof noTranscriptTimeoutSeconds !== 'number') {
throw new Error('noTranscriptTimeoutSeconds must be a number when provided');
}
if (noParticipantsLeftTimeoutSeconds !== undefined && typeof noParticipantsLeftTimeoutSeconds !== 'number') {
throw new Error('noParticipantsLeftTimeoutSeconds must be a number when provided');
}
const payload = {
platform,
meetingId: String(meeting_id),
meetingPassword: meeting_password || '',
botName: bot_name,
webhookUrl: webhook_url,
model,
language
};
if (noTranscriptTimeoutSeconds !== undefined) {
payload.noTranscriptTimeoutSeconds = noTranscriptTimeoutSeconds;
}
if (noParticipantsLeftTimeoutSeconds !== undefined) {
payload.noParticipantsLeftTimeoutSeconds = noParticipantsLeftTimeoutSeconds;
}
const response = await axios.post(`${this.apiBaseUrl}/join`, payload, {
headers: {
Authorization: `Bearer ${this.authorizationToken}`,
},
});
const sessionId = response.data.sessionId;
return { id: sessionId };
} catch (error) {
if (error.response && error.response.data && error.response.data.message) {
throw new Error(error.response.data.message);
} else if (error.request) {
throw new Error('No response from server. Please check your network or try again later.');
} else {
throw new Error('Unexpected error: ' + error.message);
}
}
}
connectSocket(sessionId, callbacks) {
const socket = io(this.wsBaseUrl, {
auth: {token: this.authorizationToken},
query: {sessionId},
});
socket.on('connect', () => {
socket.emit('joinSession', {sessionId});
});
socket.on('started', (data) => {
if (callbacks.onMeetingStarted) callbacks.onMeetingStarted(data);
});
socket.on('finished', (data) => {
if (callbacks.onMeetingFinished) callbacks.onMeetingFinished(data);
});
socket.on('transcript', (data) => {
if (callbacks.onTranscriptReceived) callbacks.onTranscriptReceived(data);
});
return socket;
}
}
function createChatterBoxClient(config) {
return new ChatterBox(config);
}
module.exports = {ChatterBox: createChatterBoxClient};