Dev.to WebDev 🛠 Dev 👁 0 📖 5 min read

Create AI Voice Responses for Slack Bots

Why Voice‑Enabled Slack Bots Matter Slack is still the backbone of many teams’ communication, but most bots sit in the text realm. Imagine a bot that can speak to you, announce build statuses, read out meeting notes, o

Why Voice‑Enabled Slack Bots Matter

Slack is still the backbone of many teams’ communication, but most bots sit in the text realm. Imagine a bot that can speak to you, announce build statuses, read out meeting notes, or even clone your voice for a personal assistant. Voice responses bring a human touch, increase accessibility, and can make your bot feel more interactive.

The key to adding voice is twofold:

  1. Convert a Slack message (or a bot’s reply) to speech – Text‑to‑Speech (TTS).
  2. Optionally clone a voice – Voice‑cloning lets you give your bot a custom timbre.

The best combination for rapid, high‑quality results is ElevenLabs. Their API delivers near‑real‑time TTS with a wide voice library and advanced cloning support. Below, I’ll walk through how to hook ElevenLabs into a Slack bot using Python, show a quick JavaScript example, and give you best‑practice tips.

1. Set Up Your Slack App

  1. Create a Slack App in your workspace via https://api.slack.com/apps.
  2. Enable Bot Token Scopes – at minimum chat:write and app_mentions:read.
  3. Install the app to your workspace and note the Bot User OAuth Token (starts with xoxb-).
  4. Enable Event Subscriptions: point to your server’s /slack/events endpoint and subscribe to app_mention (or any event you want to trigger audio).

Tip: For local testing, use ngrok to expose your local server to the public internet.

2. Get Your ElevenLabs API Key

Sign up at ElevenLabs and grab your API key. The link below will take you straight to the sign‑up flow and automatically credit the affiliate program:

https://try.elevenlabs.io/kr07zfuqn1bp

3. Architecture Overview

Slack Event  →  Slack Bot (Python/Node)  →  ElevenLabs TTS  →  Audio File  →  Slack Message with Attachment
  1. Receive Slack event – parse the text that the bot should speak.
  2. Send text to ElevenLabs – get back an MP3 or WAV.
  3. Upload audio to Slack – use files.upload to attach the audio file.
  4. Reply – send a message that references the audio file.

4. Python Implementation

Below is a minimal Flask app that listens for app_mention events and replies with an audio file generated by ElevenLabs.

# app.py
import os
import json
import requests
from flask import Flask, request, jsonify

app = Flask(__name__)

SLACK_BOT_TOKEN = os.getenv("SLACK_BOT_TOKEN")
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
ELEVENLABS_URL = "https://api.elevenlabs.io/v1/text-to-speech/eleven_monolingual_v1"

# 1️⃣ Helper: Post file to Slack
def upload_to_slack(file_bytes, filename, channels):
    url = "https://slack.com/api/files.upload"
    headers = {"Authorization": f"Bearer {SLACK_BOT_TOKEN}"}
    files = {"file": (filename, file_bytes, "audio/mpeg")}
    data = {"channels": channels, "title": filename}
    resp = requests.post(url, headers=headers, files=files, data=data)
    return resp.json()

# 2️⃣ Helper: Generate TTS via ElevenLabs
def generate_tts(text, voice_id="EXAVITQu4vr4xnSDxMaL"):
    headers = {
        "xi-api-key": ELEVENLABS_API_KEY,
        "Content-Type": "application/json",
    }
    payload = {
        "text": text,
        "voice_settings": {
            "stability": 0.5,
            "similarity_boost": 0.75,
        },
    }
    resp = requests.post(ELEVENLABS_URL, headers=headers, json=payload)
    resp.raise_for_status()
    return resp.content  # binary MP3

@app.route("/slack/events", methods=["POST"])
def slack_events():
    data = request.get_json()
    # URL verification challenge
    if data.get("type") == "url_verification":
        return jsonify({"challenge": data["challenge"]})

    # Handle event callbacks
    event = data.get("event", {})
    if event.get("type") == "app_mention":
        user = event.get("user")
        channel = event.get("channel")
        text = event.get("text").replace("<@{}>".format(event.get("bot_id", "")), "").strip()
        # 3️⃣ Generate audio
        audio = generate_tts(text)
        # 4️⃣ Upload to Slack
        upload_to_slack(audio, "bot_response.mp3", channel)
        return "", 200

    return "", 404

if __name__ == "__main__":
    app.run(port=3000)

What this does

  • Listens for app_mention events.
  • Calls ElevenLabs’ TTS API with a default voice (eleven_monolingual_v1).
  • Uploads the resulting MP3 back to the channel.

Tip: Replace the voice_id with any voice from the ElevenLabs voice catalog or a cloned voice you’ve created.

5. JavaScript (Node.js) Example

If you prefer Node, here’s a quick express‑based version using node-fetch.

// server.js
require('dotenv').config();
const express = require('express');
const fetch = require('node-fetch');
const { IncomingForm } = require('formidable');

const app = express();
const SLACK_BOT_TOKEN = process.env.SLACK_BOT_TOKEN;
const ELEVENLABS_API_KEY = process.env.ELEVENLABS_API_KEY;
const ELEVENLABS_URL = 'https://api.elevenlabs.io/v1/text-to-speech/eleven_monolingual_v1';

app.use(express.json());

async function generateTTS(text) {
  const res = await fetch(ELEVENLABS_URL, {
    method: 'POST',
    headers: {
      'xi-api-key': ELEVENLABS_API_KEY,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      text,
      voice_settings: { stability: 0.5, similarity_boost: 0.75 },
    }),
  });
  return res.buffer(); // binary
}

async function uploadToSlack(fileBuffer, channels) {
  const form = new IncomingForm();
  form.append('channels', channels);
  form.append('title', 'bot_response.mp3');
  form.append('file', fileBuffer, { filename: 'bot_response.mp3' });

  const res = await fetch('https://slack.com/api/files.upload', {
    method: 'POST',
    headers: { Authorization: `Bearer ${SLACK_BOT_TOKEN}` },
    body: form,
  });
  return res.json();
}

app.post('/slack/events', async (req, res) => {
  const { type, challenge, event } = req.body;
  if (type === 'url_verification') return res.send({ challenge });

  if (event && event.type === 'app_mention') {
    const text = event.text.replace(/<@.*?>/, '').trim();
    const audio = await generateTTS(text);
    await uploadToSlack(audio, event.channel);
    return res.sendStatus(200);
  }
  res.sendStatus(404);
});

app.listen(3000, () => console.log('Listening on 3000'));

6. Voice Cloning 101

If you want your bot to sound like you (or your brand), ElevenLabs offers voice cloning:

  1. Record a short 30‑second clip of the target voice.
  2. Upload it to the ElevenLabs dashboard or via the API.
  3. Retrieve the voice_id for the cloned voice.
  4. Pass that voice_id to the TTS request.
# Example of cloning a voice
voice_id = "my_custom_voice"  # from ElevenLabs
audio = generate_tts("Hello, I’m your new bot!", voice_id=voice_id)

Cloned voices are perfect for brand consistency or accessibility (e.g., a bot that speaks in a user’s own voice).

7. Best Practices & Troubleshooting

Issue Fix
Audio is silent Verify the Content-Type header and that the file buffer is not empty.
Slack rejects file Make sure the file size is < 8 MB and the MIME type is audio/mpeg.
Rate limits ElevenLabs enforces a request limit (~60 req/min). Cache repeated prompts or throttle your bot.
Latency TTS generation takes ~1 sec per 100 words. If you need real‑time, pre‑generate common responses.
Security Store your tokens in environment variables and never commit them.

8. Deploying

  • Heroku / Render: Easy for small bots.
  • AWS Lambda + API Gateway: Serverless, scales with traffic.
  • Docker: Bundle your bot and run anywhere.

Whichever platform, remember to set the environment variables (SLACK_BOT_TOKEN, ELEVENLABS_API_KEY) and expose the /slack/events endpoint.

9. Next Steps

  1. Add more triggers – e.g., reaction_added or message.channels.
  2. Use SSML for richer speech (pauses, emphasis).
  3. Store audio in S3 or a CDN for faster re‑use.
  4. Integrate with other APIs (weather, GitHub) and speak the results.

Call to Action

Ready to give your Slack bot a voice that feels natural, expressive, and brand‑aligned? Head over to ElevenLabs with this link and sign up today:

https://try.elevenlabs.io/kr07zfuqn1bp

Give your team the conversational edge they need—start building voice‑enabled bots now!

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.