opencode-telegram-bot icon indicating copy to clipboard operation
opencode-telegram-bot copied to clipboard

[Feature]: option to deliver TTS replies as voice messages instead of audio files

Open georgernstgraf opened this issue 2 months ago • 1 comments

Problem With /tts enabled, spoken replies are sent with api.sendAudio as assistant-reply.mp3 (audio/mpeg); see src/bot/handlers/tts-response-handler.ts:44. Telegram groups every audio file of a chat into the shared music-player queue. On Telegram Android, playback then wraps around to the older audio files of the chat once repeat is active, so playing a single reply can end in an endless loop and the user has to open the player and switch the loop off after each message. The bot cannot control client-side playback, but it does control the delivery format.

Proposal Add an option, for example TTS_DELIVERY=audio|voice with audio as the default, that sends the synthesized speech with api.sendVoice instead of api.sendAudio. Voice messages require OGG/Opus: Google Cloud TTS supports audioEncoding: "OGG_OPUS" (currently "MP3", see src/app/services/tts-service.ts:105), and OpenAI-compatible endpoints support response_format: "opus" (currently "mp3", see line 140). ElevenLabs and Edge return MP3 today, so voice mode would either need a transcode step or would be documented as provider-dependent.

Done criteria With TTS_DELIVERY=voice, a spoken reply arrives as a Telegram voice message that does not enter the chat's music queue. Text replies and the existing TTS_DELIVERY=audio behaviour stay unchanged.

Additional context Observed on 0.25.3 with TTS_PROVIDER=google, voice de-DE-Studio-C, /tts in "all" mode, Telegram on Android. I could not verify whether voice messages avoid the auto-advance behaviour on every client, so the Opus delivery is the proposal rather than a proven guarantee. Related: #63 (closed, introduced TTS), #239 (open, touches tts-service).

georgernstgraf avatar Sep 26 '26 08:09 georgernstgraf

Happy to implement this if a maintainer agrees with the direction. My minimal scope would be:

  • Add TTS_DELIVERY=audio|voice (default audio, so current behaviour is unchanged).
  • In voice mode, ask for OGG/Opus only where the provider supports it: Google audioEncoding: "OGG_OPUS", OpenAI-compatible response_format: "opus".
  • Detect OGG/Opus in the response and use api.sendVoice; otherwise fall back to api.sendAudio, so providers that only return MP3 (Edge, and some OpenAI-compatible servers) keep working.
  • Update the README and add tests for the new option and the fallback.

No new dependencies and no transcode step, which keeps the diff small and avoids changing existing behaviour.

Tested with:

  • @grinev/opencode-telegram-bot 0.26.6, Node 24.22.2
  • OpenCode 2.2.5
  • A self-hosted Speaches 0.8.3 instance (Piper nl_BE voice) as the OpenAI-compatible TTS endpoint, read on Telegram Android

We currently run a local patch that requests WAV, transcodes it to OGG/Opus with ffmpeg and sends it via sendVoice with a duration; replies arrive as real voice messages with the waveform, and sending the MP3 as an audio file was what produced the wrong duration label.

One data point for the fallback: our Speaches instance accepts only mp3, flac, wav and pcm for response_format (HTTP 422 otherwise), so it would stay on the sendAudio path. The sendVoice path itself is confirmed working.

This comment comes from an OpenCode bot on behalf of its owner.

tijmenvangurp avatar Nov 01 '26 09:11 tijmenvangurp