Telegram Voice Ai bot

 

Build a Voice-Interactive Telegram Bot Using GPT and Whisper

Voice-enabled bots are revolutionizing user engagement by adding an interactive layer to messaging. This guide will walk you through creating a Telegram bot that listens to voice messages, converts them into text, generates intelligent responses with GPT, and sends audio replies. Powered by OpenAI’s Whisper and GPT models, this bot handles voice interaction seamlessly.

Key Features of This Telegram Bot

  • Voice Message Handling: Processes incoming voice messages and converts them to text.
    • Intelligent Response Generation: Uses GPT to respond intelligently based on user input.
    • Audio Response Delivery: Converts GPT-generated text into audio responses.
    • Streamlined Experience: Provides real-time interaction in a single bot script.

How the Bot Works

The bot performs the following steps for each user interaction:

  1. Voice Message Reception: Listens for voice messages or a /start command.
    2. Conversion to Text (STT): Converts audio to text with OpenAI’s Whisper model.
    3. GPT-Driven Response: Sends the transcribed text to GPT to generate a thoughtful response.
    4. Text-to-Speech (TTS): Converts GPT’s response to an audio message.
    5. Audio Reply: Sends the audio response back to the user on Telegram.

Setting Up Your Bot

To get started, ensure you have the following requirements in place:
• Telegram Bot API Token: Obtainable from BotFather.
• OpenAI API Key: Available from OpenAI’s API platform.
• Libraries: Install pyTelegramBotAPI and openai libraries to handle the Telegram and OpenAI API interactions.

Command to install the necessary libraries:

pip install pyTelegramBotAPI openai

Code Overview

Step 1: Initial Setup

API_KEY = ‘YOUR_OPENAI_API_KEY’
TG_TOKEN = ‘YOUR_TELEGRAM_TOKEN’
openai.api_key = API_KEY
bot = telebot.TeleBot(TG_TOKEN)

Step 2: Handling Voice Messages

@bot.message_handler(content_types=[‘voice’])
def handle_voice_message(message):
process_message(message)

Step 3: Converting Voice to Text (STT)

def stt(file_path):
with open(file_path, ‘rb’) as audio_file:
transcription = openai.Audio.transcribe(model=’whisper-1′, file=audio_file)
return transcription.text

Step 4: Generating Responses with GPT

def ask_gpt(text):
response = openai.ChatCompletion.create(model=’gpt-4′, messages=[{‘role’: ‘system’, ‘content’: text}])
return response.choices[0].message.content

Step 5: Converting Text to Speech (TTS)

def tts(text, file_path):
response = openai.Audio.create(model=’tts-1′, voice=’alloy’, input=text)
with open(file_path, ‘wb’) as audio_file:
audio_file.write(response.content)

Running the Bot

To start the bot, execute the following command:

if __name__ == ‘__main__’:
bot.polling()

Applications and Use Cases

This bot setup can be extended to various applications:
• Customer Support: Handle customer inquiries with automated voice responses.
• Education: Serve as an interactive learning assistant, responding to voice queries with useful information.
• Entertainment: Build conversational experiences with creative responses.

Additional Tips for Building Your Voice-Interactive Bot

  • Enhance Conversations: Experiment with different GPT response styles for a more engaging interaction.
    • Optimize for Speed: Ensure the bot’s response time by monitoring API usage and optimizing code where necessary.
    • Secure Your Tokens: Always store your API keys securely and avoid exposing them in public repositories.

Conclusion

Creating a voice-interactive Telegram bot with OpenAI’s Whisper and GPT can significantly enhance user experience by making interactions more dynamic and accessible. By following this guide, you now have a fully functioning bot ready to handle voice messages and respond intelligently with audio replies.