Back to blog

Telegram bot send voice message tutorial

Jonathan Lis|
voice-notesapi

TITLE: How to Send Voice Messages in Telegram Bots: Complete Tutorial with Code Examples

Building a Telegram bot that can send voice messages opens up exciting possibilities for user engagement. Whether you're creating an assistant bot, educational tool, or entertainment application, voice messages add a personal touch that text simply can't match. In this comprehensive telegram bot send voice message tutorial, we'll walk through everything you need to know to implement this feature using modern APIs and best practices.

Why Voice Messages Matter in Telegram Bots

Voice messages create more engaging and accessible bot experiences. They're particularly valuable for:

  • Language learning applications
  • Accessibility features for visually impaired users
  • Creating more natural conversational experiences
  • Delivering important notifications with emphasis
  • Educational content that benefits from audio explanation

Prerequisites and Setup

Before diving into our telegram bot send voice message tutorial, ensure you have:

  • A Telegram bot token from @BotFather
  • Basic knowledge of your preferred programming language
  • A text-to-speech service (we'll use Svara API for high-quality voice synthesis)

Getting Your Telegram Bot Ready

First, create a new bot through Telegram's BotFather:

  1. Message @BotFather on Telegram
  2. Use /newbot command
  3. Follow the prompts to name your bot
  4. Save the bot token securely

Method 1: Using Node.js with Telegraf

Here's how to implement voice message functionality using Node.js and the Telegraf library:

const { Telegraf } = require('telegraf');
const axios = require('axios');
const fs = require('fs');

const bot = new Telegraf('YOUR_TELEGRAM_BOT_TOKEN');

async function generateVoiceMessage(text) {
  try {
    const response = await axios.post('https://api.svarapi.io/v1/speech', {
      text: text,
      voice: 'en-US-Neural2-A',
      speed: 1.0
    }, {
      headers: {
        'Authorization': 'Bearer YOUR_SVARA_API_KEY',
        'Content-Type': 'application/json'
      },
      responseType: 'stream'
    });
    
    return response.data;
  } catch (error) {
    console.error('Error generating voice:', error);
    throw error;
  }
}

bot.command('speak', async (ctx) => {
  const message = ctx.message.text.replace('/speak ', '');
  
  if (!message) {
    return ctx.reply('Please provide text after the /speak command');
  }
  
  try {
    const audioStream = await generateVoiceMessage(message);
    const filename = `voice_${Date.now()}.ogg`;
    
    // Save stream to file
    const writer = fs.createWriteStream(filename);
    audioStream.pipe(writer);
    
    writer.on('finish', () => {
      ctx.replyWithVoice({ source: filename })
        .then(() => fs.unlinkSync(filename)) // Clean up file
        .catch(console.error);
    });
    
  } catch (error) {
    ctx.reply('Sorry, I couldn\'t generate the voice message.');
  }
});

bot.launch();

Method 2: Python Implementation with python-telegram-bot

For Python developers, here's a complete implementation:

import logging
import requests
import tempfile
import os
from telegram import Update
from telegram.ext import Application, CommandHandler, ContextTypes

# Enable logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

TELEGRAM_TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN'
SVARA_API_KEY = 'YOUR_SVARA_API_KEY'

async def generate_voice_message(text: str) -> bytes:
    """Generate voice message using Svara API"""
    url = 'https://api.svarapi.io/v1/speech'
    headers = {
        'Authorization': f'Bearer {SVARA_API_KEY}',
        'Content-Type': 'application/json'
    }
    data = {
        'text': text,
        'voice': 'en-US-Neural2-F',
        'speed': 1.0,
        'format': 'ogg'
    }
    
    response = requests.post(url, headers=headers, json=data)
    response.raise_for_status()
    return response.content

async def speak_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
    """Handle the /speak command"""
    if not context.args:
        await update.message.reply_text('Please provide text after /speak command')
        return
    
    text = ' '.join(context.args)
    
    try:
        # Generate voice message
        audio_data = await generate_voice_message(text)
        
        # Create temporary file
        with tempfile.NamedTemporaryFile(delete=False, suffix='.ogg') as temp_file:
            temp_file.write(audio_data)
            temp_file.flush()
            
            # Send voice message
            with open(temp_file.name, 'rb') as audio_file:
                await update.message.reply_voice(audio_file)
        
        # Clean up temporary file
        os.unlink(temp_file.name)
        
    except Exception as e:
        logger.error(f'Error generating voice message: {e}')
        await update.message.reply_text('Sorry, I couldn\'t generate the voice message.')

def main():
    """Start the bot"""
    application = Application.builder().token(TELEGRAM_TOKEN).build()
    
    # Add command handler
    application.add_handler(CommandHandler('speak', speak_command))
    
    # Start the bot
    application.run_polling()

if __name__ == '__main__':
    main()

Method 3: Using cURL for Quick Testing

For rapid prototyping or testing, you can use cURL to interact with both APIs:

# First, generate the voice message with Svara
curl -X POST https://api.svarapi.io/v1/speech \
  -H "Authorization: Bearer YOUR_SVARA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "text": "Hello from my Telegram bot!",
    "voice": "en-US-Neural2-A",
    "format": "ogg"
  }' \
  --output voice_message.ogg

# Then send it via Telegram Bot API
curl -X POST "https://api.telegram.org/bot{YOUR_BOT_TOKEN}/sendVoice" \
  -F chat_id="{CHAT_ID}" \
  -F voice=@voice_message.ogg

Advanced Features and Best Practices

Voice Customization

Svara API offers various voice options and parameters. Refer to the Svara documentation for comprehensive voice customization options:

const voiceOptions = {
  text: userMessage,
  voice: 'en-US-Neural2-C', // Different voice models
  speed: 0.9, // Adjust speaking speed
  pitch: 0, // Modify pitch
  volume: 0.8 // Control volume
};

Error Handling and Rate Limiting

Implement robust error handling and respect API rate limits:

import asyncio
from functools import wraps

def rate_limit(max_calls_per_minute=20):
    def decorator(func):
        calls = []
        
        @wraps(func)
        async def wrapper(*args, **kwargs):
            now = time.time()
            # Remove calls older than 1 minute
            calls[:] = [call_time for call_time in calls if now - call_time < 60]
            
            if len(calls) >= max_calls_per_minute:
                await asyncio.sleep(60 - (now - calls[0]))
            
            calls.append(now)
            return await func(*args, **kwargs)
        return wrapper
    return decorator

@rate_limit(max_calls_per_minute=15)
async def generate_voice_message(text):
    # Your voice generation logic here
    pass

File Management

Always clean up temporary audio files to prevent storage issues:

const path = require('path');

// Create unique filename
const filename = path.join(__dirname, 'temp', `voice_${Date.now()}_${Math.random().toString(36)}.ogg`);

// Ensure temp directory exists
if (!fs.existsSync(path.dirname(filename))) {
  fs.mkdirSync(path.dirname(filename), { recursive: true });
}

// Clean up after sending
setTimeout(() => {
  if (fs.existsSync(filename)) {
    fs.unlinkSync(filename);
  }
}, 5000); // Clean up after 5 seconds

Testing Your Voice Message Bot

  1. Deploy your bot code
  2. Start the bot application
  3. Send /speak Hello, this is a test message to your bot
  4. Verify the voice message is received and plays correctly

Troubleshooting Common Issues

Audio format compatibility: Ensure you're using OGG format for best Telegram compatibility.

File size limits: Telegram has a 50MB limit for voice messages. Keep messages concise or implement text splitting.

API rate limits: Both Telegram and Svara APIs have rate limits. Implement appropriate throttling.

Ready to Build Your Voice-Enabled Telegram Bot?

This telegram bot send voice message tutorial provides everything needed to create engaging voice-enabled Telegram bots. The combination of Telegram's robust bot platform and Svara's high-quality text-to-speech API creates powerful possibilities for user interaction.

Ready to get started? Try Svara's free tier with 50 free voice notes and no credit card required. Visit svarapi.io to create your account and start building voice-enabled applications today!

Ask Svara

Hey! I'm the Svara assistant. Ask me anything about integrating voice notes into your product.

Powered by Svara