tts.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. """
  2. MIT License
  3. Copyright (c) 2023 TheHamkerCat
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. import traceback
  21. from asyncio import get_running_loop
  22. from io import BytesIO
  23. from googletrans import Translator
  24. from gtts import gTTS
  25. from pyrogram import filters
  26. from pyrogram.types import Message
  27. from wbb import app
  28. def convert(text):
  29. audio = BytesIO()
  30. i = Translator().translate(text, dest="en")
  31. lang = i.src
  32. tts = gTTS(text, lang=lang)
  33. audio.name = lang + ".mp3"
  34. tts.write_to_fp(audio)
  35. return audio
  36. @app.on_message(filters.command("tts"))
  37. async def text_to_speech(_, message: Message):
  38. if not message.reply_to_message:
  39. return await message.reply_text("Reply to some text ffs.")
  40. if not message.reply_to_message.text:
  41. return await message.reply_text("Reply to some text ffs.")
  42. m = await message.reply_text("Processing")
  43. text = message.reply_to_message.text
  44. try:
  45. loop = get_running_loop()
  46. audio = await loop.run_in_executor(None, convert, text)
  47. await message.reply_audio(audio)
  48. await m.delete()
  49. audio.close()
  50. except Exception as e:
  51. await m.edit(e)
  52. e = traceback.format_exc()
  53. print(e)