video-stream/bot/tts.py

43 lines
1.2 KiB
Python
Raw Normal View History

2021-09-09 01:18:24 +00:00
# text to speech funtion
# ported from juvia group bot / module / tts.py
# Copyright (C) 2021 Veez Project
import traceback
from asyncio import get_running_loop
from io import BytesIO
from googletrans import Translator
from gtts import gTTS
from pyrogram import filters, Client
from pyrogram.types import Message
def convert(text):
audio = BytesIO()
i = Translator().translate(text, dest="en")
lang = i.src
tts = gTTS(text, lang=lang)
audio.name = lang + ".mp3"
tts.write_to_fp(audio)
return audio
@Client.on_message(filters.command("tts"))
async def text_to_speech(_, message: Message):
if not message.reply_to_message:
return await message.reply_text("💡 reply to some text...")
if not message.reply_to_message.text:
return await message.reply_text("💡 reply to some text...")
m = await message.reply_text("🔁 processing...")
text = message.reply_to_message.text
try:
loop = get_running_loop()
audio = await loop.run_in_executor(None, convert, text)
await message.reply_audio(audio)
await m.delete()
audio.close()
except Exception as e:
await m.edit(str(e))
es = traceback.format_exc()
print(es)