Home / Tutorials / Building an Amharic Voice Telegram Bot with Wesen REST API
Developer Intermediate • 4 min read

Building an Amharic Voice Telegram Bot with Wesen REST API

A Python Telegram bot that answers text messages with Amharic audio from the Wesen TTS API.

Prerequisites

  1. Python 3.10+
  2. A Telegram Bot Token (from BotFather)
  3. A Wesen AI API Key (from the Wesen Developer Console)
pip install python-telegram-bot requests

Step 1: Generate Speech

TTS jobs are asynchronous. POST /v1/job returns a jobId, GET /v1/job/{jobId} reports its state, and GET /v1/job/{jobId}/audio returns the file once the job is completed. configId is required: GET /v1/meta/voices lists the voices, and almaz is an Amharic one.

import os
import time
import requests

TTS_BASE_URL = "https://tts.api.wesen.ai"
HEADERS = {"X-API-Key": os.environ["WESEN_API_KEY"]}

def generate_amharic_voice(text: str, voice: str = "almaz") -> bytes:
    """Submit a TTS job, wait for it to finish, and return the MP3 bytes."""
    job = requests.post(
        f"{TTS_BASE_URL}/v1/job",
        json={"text": text, "configId": voice, "format": "mp3"},
        headers=HEADERS,
        timeout=30,
    )
    job.raise_for_status()
    job_id = job.json()["jobId"]

    for _ in range(60):
        status = requests.get(f"{TTS_BASE_URL}/v1/job/{job_id}", headers=HEADERS, timeout=30)
        status.raise_for_status()
        state = status.json()["state"]
        if state == "completed":
            audio = requests.get(f"{TTS_BASE_URL}/v1/job/{job_id}/audio", headers=HEADERS, timeout=60)
            audio.raise_for_status()
            return audio.content
        if state in ("failed", "cancelled"):
            raise RuntimeError(f"TTS job {job_id} ended as {state}")
        time.sleep(1)
    raise TimeoutError(f"TTS job {job_id} did not finish")

Step 2: Wire Telegram Bot Handlers

generate_amharic_voice blocks while it polls, so the handler runs it in a thread.

import asyncio
import io
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, filters, ContextTypes

async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
    welcome_text = "ሰላም! እኔ የአማርኛ ድምፅ ረዳት ነኝ። የፈለጉትን መልእክት ይጻፉልኝ፣ በድምፅ እመልሳለሁ።"
    await update.message.reply_text(welcome_text)

async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await update.message.chat.send_action("record_voice")
    audio_bytes = await asyncio.to_thread(generate_amharic_voice, update.message.text)

    voice_file = io.BytesIO(audio_bytes)
    voice_file.name = "response.mp3"
    await update.message.reply_voice(voice=voice_file)

if __name__ == "__main__":
    app = ApplicationBuilder().token("YOUR_TELEGRAM_BOT_TOKEN").build()
    app.add_handler(CommandHandler("start", start))
    app.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), handle_message))
    app.run_polling()

Step 3: Test

Run the script and send a message like "እንደምን አመሻችሁ!" to your bot. It replies with an Amharic voice note.