DEV Community

César Freitas
César Freitas

Posted on

Add text-to-speech to a Discord or Telegram bot via API

Adding Voice to Your Discord or Telegram Bot: A Text-to-Speech Solution

As a developer building a Discord or Telegram bot, you're probably used to the limitations of text-based communication. However, you might find that your users would appreciate the ability to hear their chat in action. But how do you add audio to your bot without having to develop your own audio synthesis capabilities? In this post, we'll explore how the Multilingual Text-to-Speech API can help solve this problem.

The Problem

When building a Discord or Telegram bot, you're often limited by the platform's text-based communication. While it's great for sending messages and receiving responses, it can be frustrating when users want to hear their chat in action. This is where adding text-to-speech functionality comes in – allowing your users to hear their messages as spoken words.

The Solution

The Multilingual Text-to-Speech API offers a simple solution to this problem. With its API, you can convert text into natural speech in nine languages, using 54 different voices. This means that no matter where your bot's users are from, they'll be able to hear their messages as spoken words.

What's more impressive is that the API returns both MP3 and WAV audio formats in a single request. This makes it incredibly easy to integrate into your existing bot infrastructure.

Typical Request/Response

So, what does a typical request and response look like using this API? In simple terms:

  • You send a request to the API with the text you want converted into speech.
  • The API processes the request and returns a single audio file – either MP3 or WAV.
  • This audio file is sent back to your bot, where it can be played for the user.

While this might seem straightforward, it's still an important step in understanding how to use the API effectively. By breaking down the process into smaller steps, you'll be better equipped to integrate text-to-speech functionality into your existing codebase.

Conclusion

Adding text-to-speech functionality to a Discord or Telegram bot is easier than ever. With the Multilingual Text-to-Speech API, you can convert text into natural speech in nine languages, using 54 different voices – all with just one request.

If you're interested in trying out this API for yourself, RapidAPI offers a free tier that's perfect for indie developers and makers. With its vast collection of APIs, including the Multilingual Text-to-Speech API, RapidAPI is an excellent resource to have in your toolkit.

Don't be afraid to give it a try – you might just find that adding voice to your bot makes all the difference for your users.

Quick example

curl -X POST 'https://multilingual-text-to-speech2.p.rapidapi.com/v1/tts' \
  -H 'X-RapidAPI-Key: YOUR_RAPIDAPI_KEY' \
  -H 'X-RapidAPI-Host: multilingual-text-to-speech2.p.rapidapi.com' \
  -H 'Content-Type: application/json' \
  -d '{"text":"Hello world","voice":"af_alloy","format":"mp3"}'
Enter fullscreen mode Exit fullscreen mode

Try it: the Multilingual Text-to-Speech API is live on RapidAPI with a free tier — get your key here.

Top comments (0)