-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.ts
More file actions
119 lines (92 loc) · 2.9 KB
/
app.ts
File metadata and controls
119 lines (92 loc) · 2.9 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
import 'dotenv/config';
import { io, Socket } from 'socket.io-client';
import { spawn } from 'node:child_process';
import player from 'play-sound';
type PlayUrl = {
url: string;
};
type PlayText = {
text: string;
volume: number;
};
type PlayClip = {
file: string;
volume: number;
};
type ServerToClientEvents = {
play_url: (data: PlayUrl) => void;
play_text: (data: PlayText) => void;
close: () => void;
};
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
interface ClientToServerEvents {}
// connect to socket for bot commands
// the basic idea is that we just proxy commands to the referenced HTTP API
function urlForRoom(roomName: string): string {
return `${process.env.SONOS_BRIDGE_URL}/${encodeURIComponent(roomName)}`;
}
function playClip(roomName: string, data: PlayClip): void {
const file = encodeURIComponent(data.file);
const volume = encodeURIComponent(data.volume);
if (process.env.USE_LOCAL_SOUNDS === 'true') {
player().play(`static/clips/${data.file}`);
} else {
fetch(`${urlForRoom(roomName)}/clip/${file}/${volume}`);
}
}
function localSay(text: string): void {
if (process.platform === 'darwin') {
spawn('say', [text]);
} else {
// NOTE: simple Windows support could be done with https://github.com/p-groarke/wsay or similar
console.warn('localSay is not supported on this platform.');
}
}
function sayClip(roomName: string, data: PlayText): void {
const text = encodeURIComponent(data.text);
const volume = encodeURIComponent(data.volume);
if (process.env.USE_LOCAL_SOUNDS === 'true') {
localSay(text);
} else {
fetch(`${urlForRoom(roomName)}/say/${text}/${volume}`);
}
}
function enumeratePlayers(callback: (roomName: string) => void): void {
const roomName = 'Back Office';
callback(roomName);
// TODO: at some point if we get another Sonos in here, we can add the
// enumerate rooms method back...
}
function registerListener(): void {
const serviceUrl = process.env.CLEARBOT_URL;
if (!serviceUrl) throw new Error('CLEARBOT_URL was not defined.');
console.log('Connecting to', serviceUrl);
const socket: Socket<ServerToClientEvents, ClientToServerEvents> =
io(serviceUrl);
socket.on('connect', () => {
console.log(`Connected to server: ${serviceUrl}`);
});
socket.on('play_url', data => {
console.log('Received play_url: ', data);
enumeratePlayers(roomName => {
// HACK: switch to new format for now...
const { url } = data;
playClip(roomName, {
file: url,
volume: 20,
});
});
});
socket.on('play_text', data => {
console.log('Received say: ', data);
enumeratePlayers(roomName => {
sayClip(roomName, data);
});
});
socket.on('close', () => {
console.log(`Lost contact with server: ${serviceUrl}`);
console.log("I don't know how to reconnect yet. Please help!");
process.exit(1);
});
}
registerListener();