PyScript python script to send Text to Speech as audio messages to a Telegram Bot

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
def tts_to_telegram(message=None, target=None):
name: TTS to Telegram
description: Send a voice message to a telegram bot
    description: Message to be turned into voice
    example: Hello world!
    required: true
    description: Telegram ChatId
    example: 99999999999
    required: true
    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(, url, data=json.dumps(payload), headers=headers)
    response = json.loads((r.content).decode('utf-8'))


And the you can use it as a service:

Hope you guys like it! Cheers!


I found something fun to work with as well! Here is a simple Python code for integrating AI voiceover “say command” into WhatsApp:

import os
import pyautogui

def say_command(command):
    speak("Sending WhatsApp message: " + command)

if __name__ == "__main__":
    say_command("Hi there!")

This code will first speak the command “Hi there!” and then type it into WhatsApp. You can replace the “Hi there!” command with any message you want to send.

To run this code, you will need to install the pyautogui library. You can do this by running the following command in your terminal:

pip install pyautogui

Once you have installed the pyautogui library, you can run the code by saving it as a Python file and then running it from the command line. For example, if you save the code as, you can run it by running the following command:


This will send the message “Hi there!” to your WhatsApp contacts.

Now for the voice customization, you may use any AI Voiceover software and choose the voice you desire. Simply download the .mp3 file and update the data/file source for audio fetching.

Hope you all enjoy this! :innocent:

1 Like

Nice one! But I don’t understand where I need to place the code, in my HA server or in my laptop?