import tempfile
import os.path

from gtts import gTTS

from rollbot import as_command
from rollbot.injection import Args, Config, Request, SenderName


@as_command
async def yell(args: Args, name: SenderName, target: Config("yell.endpoint"), req: Request):
    if len(args) == 0:
        return "You need to give me something to tell Audiobot to yell!"

    tmp_dir = tempfile.mkdtemp()
    tmp_file = os.path.join(tmp_dir, "yell.mp3")

    tts = gTTS(text=f"Incoming Message from {name}: {args}", lang="en", slow=False)
    tts.save(tmp_file)

    await req.post(target, data=open(tmp_file, "rb"))

    return "Passed the message along to Audiobot!"
