Answer intent with mp3

I am trying to get my speakers to answer an intent with a sound, an mp3 file. I have a main server and then a few rpi satellites. I have a python script that handles the intents. So i create an MQTT client and connect to a broker.

I normally answer intents with a sentence and it gets published to hermes/tts/say. the little code snippet is below

site_id = nlu_payload["siteId"]
sentence = "answer to the intent"
client.publish("hermes/tts/say", json.dumps({"text": sentence, "siteId": site_id}))

So I was thinking there would be a different client.publish I could do for the mp3. something like

site_id = nlu_payload["siteId"]
sound = /path/to/mp3
client.publish("hermes/tts/say", json.dumps({"audio": sound, "siteId": site_id}))

But I am a little new with the hermes stuff so i wasnt sure if that was possible

I have seen other threads that mention cmd line commands that can be run. But wasnt sure what the requestID is or how to get it. Also not sure if that will work in a python script

mosquitto_pub -p 12183 -t hermes/audioServer/$siteId/playBytes/$requestId -s < /path/to/file.wav

or the hermes audio server. So i was thinking could i use hermes/audioServer/siteId/playBytes/requestId as the first part of the client.publish() but with the variables it expects, I wasnt sure how that would work.

So if anyone has got this to work before I would be grateful to learn how you did it

Thanks for the help!

mosquitto_pub -p 12183 -t hermes/audioServer/$siteId/playBytes/$requestId -f '/path/to/file.wav'

Hey Jens,

I have seen that before. would that work in a python script? this is an example of how I process an intent asking what day it is

if ("Day" == nlu_payload["intent"]["intentName"]):
    sentence ='%A, %B %d, %Y')
    site_id = nlu_payload["siteId"]
    client.publish("hermes/tts/say", json.dumps({"text": sentence, "siteId": site_id}))

So would I be able to take that command and do something like below?

if ("Day" == nlu_payload["intent"]["intentName"]):
    site_id = nlu_payload["siteId"]
    mosquitto_pub -p 12183 -t hermes/audioServer/$siteId/playBytes/$requestId -f '/path/to/file.mp3'

I am assuming not. I assumed i would have to still have client.publish() and somehow get a requestid

You can assign the requestId freely. When the wav file has been played, the requestId is returned at playFinished, thus completing the process.

You can use paho.mqtt (nice) for transmission or system:

import os
os.system("mosquitto_pub ...")

Greetings, Jens

Thank you Jens

this is the line I ended up using. I had to add a few parameters since I have a password set but got it working.

os.system("mosquitto_pub -p <port> -t hermes/audioServer/"+site+"/playBytes/test -f '/path/to/wav' -u <username> -P <password>")

Hey dblanc28,
Below is a modification of the very first python script I wrote (just changed how I’m calling aplay). I use it in conjunction with HA to play a variety of wav files in response to Rhasspy and HA automations. You coule swap out play for another audio player that supports MP3. I have it autostarting via systemd, work for my purposes (fun announcements, Rhasspy “Fire Phasers”!)

# !/usr/bin/env python
# encoding: utf-8
import paho.mqtt.client as mqtt #import the client, paho.mqtt.client, if you need to install: pip install paho-mqtt
import subprocess

def on_connect(client, userdata, flags, rc):
#  print("Connected with result code " + str(rc)) # for debugging, uncomment to check if connected, should be result code 0

def on_message(client, userdata, message):
#  print("message received " ,str(message.payload.decode("utf-8"))) # for debugging, uncomment to check message
#  print("message topic=",message.topic) # for debugging, uncomment to check topic
#  print("message qos=",message.qos) # for debugging, uncomment to check quality of service
#  print("message retain flag=",message.retain) # for debugging, uncomment to check retained message flag
    wavefile = ''
    wavefile = str(message.payload.decode("utf-8"))
#    print ("aplay" + " " + "--device" + " " + "plughw:1,0" + " " + wavefile) # for debugging, uncomment to check message
    subprocess.Popen(['aplay', '--device','plughw:1,0', wavefile])

client = mqtt.Client() 
client.on_connect = on_connect
client.on_message = on_message
client.connect("XXX.XXX.XXX.XXX", 1883, 60) # your mosquitto server info