Proper way to build intents, handling them and getting a TTS output

Hi,

As many of us (i suppose) I’m an ex snips user, trying to rebuild with Rhasspy what I’ve done with the now defuct voice assistant framework

My work on snips was mainly made by a number of skills written in python that handle the data to a web interface over hermes/mqtt (yes I took the hard way ahah…glad to use websockets now!) and give also a TTS output

So now I’m on Rhasspy, so far I’ve installed and did some preps and I’m happy with it, what is still not clear to me is how to handling the intents.
Documentation talk about manipulating the JSON payload with Homeassistant or Nodered…since I don’t need to control any hw device to me seems a bit of an overkill using those services but glad do be proven wrong

Searching around the only example I’ve seen of intent handing via code are the nodeJs snippets made by MatrixLabs guys

So whats the proper way? Is there some official example code in python or js that I’ve missed on the Rhasppy documentation and repo?

I don’t have any kind of preference here, also I’m a total noob on python so I just opt for the simplest and lighest solution, I would love have that cleared out and hear your opinion, thanks! :slight_smile:

I don’t know whether there are code examples or not.

Basically you can use Python or any programming language and subscribe to the websocket of rhasspy.

I’m using JavaScript for all my intent handling.

Do you have any knowledge about class based programming or JavaScript / NodeJS?

index.js:

require('dotenv').config()
const Rhasspy = require('./src/rhasspy')
const MopidyClient = require('./src/mopidyClient')
const Ws = require('./src/websocket')

const rhasspy = new Rhasspy()
const mopidyClient = new MopidyClient(rhasspy)
const ws = new Ws(rhasspy)
rhasspy.setMopidyClient(mopidyClient)
mopidyClient.init()
rhasspy.init()

websocket.js:

const IP = "ipYouWouldLikeToConnectToOfRhasspy"
const WebSocket = require("ws")

class WebSocketClass {
    constructor(rhasspy) {
        this.ws = new WebSocket("ws://" + IP + ":12101/api/events/intent")
        this.rhasspy = rhasspy
        this.ws.on("open", function open() {
            console.log("**Started Web Socket Client**")
            console.log("\n**Connected**\n");
        })

        this.ws.on("close", function close() {
            console.log("\n**Disconnected**\n")
        })

        this.ws.on("message", (data) => {
            data = JSON.parse(data);
            const today = new Date();
            const time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
            console.log("**Captured New Intent**" + time)
            console.log(data)
            this.rhasspy.handleIntent(data)
        })
    }
}

module.exports = WebSocketClass

rhasspy.js:

const https = require('http')
const fs = require('fs')
const Ws = require('./websocket')

class Rhasspy {
    constructor() {
        this.ws = Ws
        this.wavBuffer = undefined
        this.mopidyClient = undefined
    }

    init() {
        // parse alarm sound
        this.wavBuffer = fs.readFileSync('./assets/alarm-sound.wav')
        console.log("-----> Be sure to change to IP address! <-----")
        this.say('Online!')
    }

    // Intents are passed through here
    handleIntent(data) {
        if ("GetTime" === data.intent.name) {
            const today = new Date();
            this.say("Es ist " + today.getHours() + " Uhr " + today.getMinutes());
        }
       
    // Text to speech for string argument
    say(text) {
        console.log("Komme rein in say! TEXT:", text)
        const options = {
            hostname: process.env.WS_RHASSPY_IP,
            port: 12101,
            path: "/api/text-to-speech",
            method: "POST"
        }

        const req = https.request(options)
        req.on("error", error => {
            console.error(error)
        })

        req.write(text)
        req.end()
    }
}

module.exports = Rhasspy

I think that’s a pretty smooth way to handle intents. For sure I would still make a better organization of intents in the future.

I hope these snippets help you to get to the ground of intent handling using JS.

Hey @Bozor, sorry for late reply…had some issue with internet connection at home and the technician took a couple of days solve the issue

And thanks for the reply and sharing me that snipped code! Yes feel more confortable on javascript :slight_smile: is nice that you are using modules to keep the code a bit more clean

At the moment I managed to rewrite a couple of intents and is working fine!

1 Like