Sprach-Bot
/join holt ein Sprach-Ticket und verbindet sich, wenn @livekit/rtc-node installiert ist, mit dem Sprachkanal.
Bevor du beginnst
Erstelle eine Anwendung, installiere sie auf einem Testserver und setze drei Umgebungsvariablen (siehe Starte deinen Bot):
export ZRZUR_BOT_TOKEN=YOUR_BOT_TOKEN
export ZRZUR_API_URL=https://api.zrzur.com
export ZRZUR_WS_URL=wss://events.zrzur.comDer Code
// Voice: `/join` gets a ticket for the caller's voice channel. Audio goes
// through LiveKit; with `@livekit/rtc-node` installed the bot connects and
// can publish an audio track. Without it, the bot only shows the ticket step.
import { Client } from "@zrzur/sdk/bot";
const bot = new Client({
token: process.env.ZRZUR_BOT_TOKEN,
apiUrl: process.env.ZRZUR_API_URL,
wsUrl: process.env.ZRZUR_WS_URL,
});
bot.command("join", async (i) => {
const channel = i.option("channel");
await i.defer({ ephemeral: true });
// POST /channels/{id}/join_call → { url, token } (needs Connect)
const ticket = await bot.voice.join(channel);
let livekit;
try {
livekit = await import("@livekit/rtc-node");
} catch {
return i.editReply(
`Got a voice ticket for node ${ticket.node}. Install @livekit/rtc-node to connect.`,
);
}
const room = new livekit.Room();
await room.connect(ticket.url, ticket.token, { autoSubscribe: false });
// publish audio: create an AudioSource, a LocalAudioTrack from it, then
// room.localParticipant.publishTrack(track, options) and feed 48 kHz frames
await i.editReply("Joined the voice channel.");
});
await bot.login();
await bot.syncCommands([
{
name: "join",
description: "Join a voice channel",
options: [
{
type: "Channel",
name: "channel",
description: "Voice channel",
required: true,
channel_types: ["Voice"],
},
],
},
]);
console.log("ready");
Starte es mit node voice.mjs.
Probiere es aus
Führe /join channel:#voice aus. Der Bot braucht Connect (und Speak, um gehört zu werden).
Was es zeigt
POST /channels/{target}/join_callliefert die LiveKit-Adresse und ein Token.- Der Ton selbst läuft über LiveKit; siehe Sprache zum Senden von Ton.
- Unser Live-Test deckt den Ticket-Schritt dieser Datei ab; das Senden von Ton wurde separat mit einem echten LiveKit-Server geprüft.