Hi everyone, I want to share a simple python script I made for de HACS custom integration PyScript. It converts text to a mp3 file using the Cloud TTS engine and sends to a Telegram bot as an audio message. It can be tweak to use another TTS engine, it’s very simple.
import requests, json
@service
def tts_to_telegram(message=None, target=None):
"""yaml
name: TTS to Telegram
description: Send a voice message to a telegram bot
fields:
message:
description: Message to be turned into voice
example: Hello world!
required: true
selector:
text:
target:
description: Telegram ChatId
example: 99999999999
required: true
selector:
text:
"""
access_token = "XXXXXXXXXXXXXXXXXXXXXXX"
url = "http://localhost:8123/api/tts_get_url"
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json"
}
payload = {
"message": message,
"engine_id": "cloud"
}
r = task.executor(requests.post, url, data=json.dumps(payload), headers=headers)
response = json.loads((r.content).decode('utf-8'))
telegram_bot.send_voice(
authentication='digest',
url=response['url'],
target=target
)
And the you can use it as a service:
Hope you guys like it! Cheers!