Intent Handling with remote HTTP Server, PHP Webserver

Hello everybody,

I have a little problem again. I’m still a big Rhasspy noob, so I hope for your patience.

I use a remote HTTP server to handle intents, I specified a URL to a client socket (TCP protocol). On the side of this client I can print the JSON, that rhasspy transmits. I use PHP for this. Works perfectly.

The how-to says: "When an intent is recognized, Rhasspy will POST to handle.remote.url with the intent JSON. You should return JSON back, optionally with additional information. "

But I don’t know how to return the JSON. I tried:

$url = "RHASSPY_IP:PORT/api/events/intent";

$curl_header = array (

"cache-control: no-cache",

"content-type: application / json"

);

$ curl = curl_init ();

curl_setopt_array ($ curl, array (

CURLOPT_URL => $ url,

CURLOPT_RETURNTRANSFER => true

)

);

$ response = curl_exec ($ curl);

$ error = curl_error ($ curl);

curl_close ($ curl);

But then I get in the Rhasspy Log:

quart.serving: 192.168.178.31:41386 GET /api/events/intent 1.1 500 15 13650
[ERROR: 7642103] __main__: BadRequest (400)
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/quart/app.py", line 1471, in full_dispatch_request
result = await self.dispatch_request (request_context)
File "/usr/local/lib/python3.6/dist-packages/quart/app.py", line 1513, in dispatch_request
raise request_.routing_exception
quart.exceptions.BadRequest: BadRequest (400)

The listener then stops working and I have to restart Rhasspy. What do I have to return correctly?

Thanks for help!

Hey there!

To return JSON as a HTTP request response in PHP simply do:

header(‘Content-Type: application/json’); echo json_encode(...);

Just replace the ... by a nested associative array.

Hope this helps.

Hey fastjack,

thank you for your help! I tried:

header("Content-Type: application/json");
		$response = array(
							'status' => true,
							'message' => 'Success'
						);
		echo json_encode($response);

But I got the same error (BadRequest). I also tried:

$response = array(
							'status' => true,
							'message' => 'Success',
							'payload' => $json_raw
						);

$json_raw is the json I got from rhasspy. This also did not work.

I don’t know what to send back to rhasspy (the keys and values of the array). The docs of OpenApi (and Swagger.io) could not help me.

Any ideas?

I think you will find the expected response format here:

The remote handler expect a JSON response like:
{"speech": {"text": " ... "}}

Hope this helps.

I tried:

header("Content-Type: application/json");
		$response = array(
							'speech' => array('text' => 'Okay'
											)			
						);
		echo json_encode($response);

TTS is activated in Rhasspy but nothing to hear. I din’t get the BadRequest message, only:
[DEBUG:37843616] urllib3.connectionpool: Starting new HTTP connection (1): HOST_IP:PORT

(I changed HOST_IP:PORT)
After this rhasspy did not listen for wake words anymore.

On my remote server I got from Rhasspy:
POST / HTTP/1.1
Host: HOST_IP:PORT
User-Agent: python-requests/2.22.0
Accept-Encoding: gzip, deflate
Accept: /
Connection: keep-alive
Content-Length: 415
Content-Type: application/json

Then the json:
{"entities": [], "intent": {"confidence": 1.0, "name": "GetTime"}, "raw_text": "wie sp\u00e4t", "raw_tokens": ["wie", "sp\u00e4t", "ist", "es"], "recognize_seconds": 0, "siteId": "default", "slots": {}, "speech_confidence": 1, "text": "wie sp\u00e4t ist es", "time_sec": 0.06876134872436523, "tokens": ["wie", "sp\u00e4t", "ist", "es"], "transcribe_seconds": 0.0, "wakeId": "snowboy/Jilms.pmdl", "wav_seconds": 0.0}

I also tried (because response.raise_for_status() needs a response?):
if (substr($_GET[‘VALUE’], 0, 1) != “{”)
{
echo “200”;
}
else
{

		//header("Content-Type: application/json");
		$response = array(	
							'speech' => array('text' => 'Okay'
											)			
						);
		echo json_encode($response);
	}

I can see the echos in my console but the behavior of rhasspy does not change.

I also have my own remote server, but mine is in python. So I can’t help you with the PHP.

However, I learned empirically (from the errors in the rhasspy log file) that the responses have to contain the intent name and the time it took to handle the intent. The speech section is optional, if you want rhasspy to “talk”. To give an example:

{"intent": "GetTime", "time_sec":0.1, "speech": {"text": It is currently 12 o'clock" } }

Hope it helps.