ai newsMo., 2. Nov.
EilmeldungApple Intelligence Finally Lands in China — Powered by Alibaba's Qwen

Create AI Voice Responses for Slack Bots

Why Voice‑Enabled Slack Bots Are Worth It Most Slack bots reply with plain text, but imagine a bot that can read out alerts, summarize meeting notes, or even deliver a personalized welcome in the user��s own voice. Adding text‑to‑speech (TTS) turns a static message into a richer, more acc

dev.toMontag 02:19 · 02.11.6 Min. Lesezeit
Bild: dev.to

Most Slack bots reply with plain text, but imagine a bot that can read out alerts, summarize meeting notes, or even deliver a personalized welcome in the user��s own voice. Adding text‑to‑speech (TTS) turns a static message into a richer, more accessible experience—especially for remote teams that spend a lot of time in channels while multitasking.

All the code is runnable locally, and the only external dependency you need is an ElevenLabs API key (sign‑up is free and you can start generating voice clips instantly).

You can install the required Python packages with:

pip install slack_bolt slack_sdk requests Enter fullscreen mode Exit fullscreen mode 1. Create a Slack App Go to api.slack.com/apps → Create New App. Choose From scratch, give it a name (e.g., VoiceBot ), and pick your workspace. Under OAuth & Permissions add the following scopes: chat:write – to post messages. files:write – to upload audio files. app_mentions:read – to listen for @VoiceBot mentions. Install the app to your workspace and copy the Bot User OAuth Token ( xoxb-… ).

Enable Event Subscriptions → Subscribe to bot events → add app_mention. Set the request URL to your ngrok tunnel (e.g., https://abcd1234.ngrok.io/slack/events ). Save.

Sign up at the affiliate link: ElevenLabs – Free TTS API. After confirming your email, navigate to API Keys and copy the key. You’ll need it for the HTTP request that turns text into speech.

ElevenLabs offers a simple REST endpoint:

POST https://api.elevenlabs.io/v1/text-to-speech/{voice_id} Enter fullscreen mode Exit fullscreen mode voice_id can be a default voice (e.g., eleven_monolingual_v1 ) or a custom cloned voice you created in the dashboard. The body is JSON: { "text": "Your message", "model_id": "eleven_monolingual_v1" }. Include the header xi-api-key: YOUR_ELEVENLABS_API_KEY. Here’s a minimal Python helper:

import requests ELEVEN_API_KEY = " YOUR_ELEVENLABS_API_KEY " DEFAULT_VOICE = " eleven_monolingual_v1 " # change to your cloned voice ID if you have one def synthesize ( text: str ) -> bytes: url = f " https://api.elevenlabs.io/v1/text-to-speech/ { DEFAULT_VOICE } " headers = { " Accept ": " audio/mpeg ", " Content-Type ": " application/json ", " xi-api-key ": ELEVEN_API_KEY, } payload = { " text ": text, " model_id ": " eleven_monolingual_v1 " } response = requests. post ( url, json = payload, headers = headers ) response. raise_for_status () return response. content # raw MP3 bytes Enter fullscreen mode Exit fullscreen mode Tip: If you have a cloned voice, replace DEFAULT_VOICE with the ID you received after the cloning process. The same endpoint works for any voice you own.

Below is a complete, but concise, Bolt‑for‑Python app that listens for @VoiceBot mentions, turns the message (minus the mention) into speech, and posts the audio back to the channel.

import os from slack_bolt import App from slack_sdk import WebClient from slack_sdk.errors import SlackApiError # ---- Slack setup ------------------------------------------------- app = App ( token = os. getenv ( " SLACK_BOT_TOKEN " )) client = WebClient ( token = os. getenv ( " SLACK_BOT_TOKEN " )) # ---- ElevenLabs helper (copy from previous section) --------------- ELEVEN_API_KEY = os. getenv ( " ELEVEN_API_KEY " ) DEFAULT_VOICE = " eleven_monolingual_v1 " def synthesize ( text: str ) -> bytes: url = f " https://api.elevenlabs.io/v1/text-to-speech/ { DEFAULT_VOICE } " headers = { " Accept ": " audio/mpeg ", " Content-Type ": " application/json ", " xi-api-key ": ELEVEN_API_KEY, } payload = { " text ": text, " model_id ": " eleven_monolingual_v1 " } resp = requests. post ( url, json = payload, headers = headers ) resp. raise_for_status () return resp. content # ---- Event handler ------------------------------------------------ @app.event ( " app_mention " ) def handle_mention ( body, say, logger ): try: # Extract the raw text after the bot mention text = body [ " event " ][ " text " ] user = body [ " event " ][ " user " ] # Remove the mention syntax (<@Uxxxx>) so we only synthesize the actual message cleaned = " ". join ( part for part in text. split () if not part. startswith ( " <@ " )) if not cleaned. strip (): say ( f " <@ { user } > Hey, give me something to read! 🎤 " ) return logger. info ( f " Synthesizing: { cleaned } " ) audio_bytes = synthesize ( cleaned ) # Upload MP3 to Slack upload_resp = client. files_upload ( channels = body [ " event " ][ " channel " ], file = audio_bytes, filename = " voice_response.mp3 ", title = " Voice reply ", initial_comment = f " Here’s what I heard, <@ { user } >: ", ) logger. info ( f " Uploaded file: { upload_resp [ ' file ' ][ ' id ' ] } " ) except SlackApiError as e: logger. error ( f " Slack error: { e. response [ ' error ' ] } " ) say ( f " Sorry <@ { user } >, I hit a snag. " ) except Exception as exc: logger. exception ( exc ) say ( f " Something went wrong, <@ { user } >. Check the logs.

" ) # ---- Start the server --------------------------------------------- if __name__ == " __main__ ": # For local testing you can use `python app.py` and expose via ngrok app. start ( port = int ( os. getenv ( " PORT ", 3000 ))) Enter fullscreen mode Exit fullscreen mode How It Works Event trigger – Slack sends an app_mention payload whenever someone writes @VoiceBot …. Message cleaning – We strip out the <@Uxxxx> token so the TTS engine only receives the actual content. Call ElevenLabs – The synthesize function returns MP3 bytes.

Upload – files_upload posts the audio file to the same channel, automatically creating a playable message. That’s it! The bot now replies with a voice clip every time you mention it.

If you prefer not to run a full Bolt app, you can hit the ElevenLabs endpoint directly with curl:

curl -X POST "https://api.elevenlabs.io/v1/text-to-speech/eleven_monolingual_v1" \ -H "Accept: audio/mpeg" \ -H "Content-Type: application/json" \ -H "xi-api-key: YOUR_ELEVENLABS_API_KEY" \ -d '{"text":"Hello Slack! This is a test.", "model_id":"eleven_monolingual_v1"}' \ --output hello.mp3 Enter fullscreen mode Exit fullscreen mode Upload hello.mp3 manually in Slack ( + → Upload a file ) and you’ll hear the same quality you’d get from the bot.

All of these enhancements build on the same simple request/response pattern shown above.

Make sure your production URL is HTTPS; Slack will reject non‑TLS endpoints.

Adding voice to Slack bots doesn’t have to be a research‑paper‑level effort. With just a few lines of Python and a robust TTS provider like ElevenLabs, you can turn any textual response into a natural‑sounding audio clip—opening the door to more inclusive, engaging, and fun interactions.

Ready to give your Slack bot a voice? Grab an ElevenLabs API key today at the affiliate link below, copy the code, and start experimenting.

👉 Try ElevenLabs now: https://try.elevenlabs.io/kr07zfuqn1bp

Happy coding, and may your bots always be heard loud and clear!

Quelle: dev.to (dev.to). Original öffnen