usingvarclient=newElevenLabsClient(apiKey);usingvarcts=newCancellationTokenSource(TimeSpan.FromSeconds(45));varvoiceId=Environment.GetEnvironmentVariable("ELEVENLABS_VOICE_ID")is{Length:>0}voiceIdValue?voiceIdValue:"21m00Tcm4TlvDq8ikWAM";// Connect to the realtime Text to Dialogue endpoint. The options default to// eleven_v3_conversational and MP3 at 44.1 kHz / 128 kbps.awaitusingvarsession=awaitclient.ConnectTextToDialogueRealtimeAsync(voiceId,newRealtimeTextToDialogueOptions(),cancellationToken:cts.Token);// Expressive audio tags such as [cheerfully] are interpreted by Eleven v3.awaitsession.SendTextAsync(voiceId,"[cheerfully] Realtime dialogue is ready to make every conversation feel alive!",newTurn:true,flush:true,cancellationToken:cts.Token);awaitsession.CloseSocketAsync(cts.Token);// Handle the typed server events and collect each streamed audio chunk.usingvaraudio=newMemoryStream();varreceivedFinal=false;awaitforeach(varupdateinsession.ReceiveUpdatesAsync(cts.Token)){if(update.TryPickTextToDialogueWebsocketAudioChunk(outvaraudioChunk)){varbytes=Convert.FromBase64String(audioChunk.Audio);awaitaudio.WriteAsync(bytes,cts.Token);}elseif(update.TryPickTextToDialogueWebsocketError(outvarerror)){thrownewInvalidOperationException($"ElevenLabs error: {error.Error} - {error.Message}");}elseif(update.IsTextToDialogueWebsocketFinal){receivedFinal=true;break;}}// Save the completed MP3 for playback.conststringoutputPath="eleven-v3-conversational.mp3";awaitFile.WriteAllBytesAsync(outputPath,audio.ToArray(),cts.Token);Console.WriteLine($"Saved {audio.Length} bytes to {Path.GetFullPath(outputPath)}");