diff --git a/programs/handle/home_assistant/bin/converse.py b/programs/handle/home_assistant/bin/converse.py index b6452f3..ed589a5 100755 --- a/programs/handle/home_assistant/bin/converse.py +++ b/programs/handle/home_assistant/bin/converse.py @@ -3,6 +3,7 @@ import json import logging import sys +import time from pathlib import Path from urllib.request import Request, urlopen @@ -19,6 +20,7 @@ def main(): ) parser.add_argument("token_file", help="Path to file with authorization token") parser.add_argument("--language", help="Language code to use") + parser.add_argument("--conversation_id_timeout", type=int, default=30, help="Invalidate conversation_id and start a new conversation if this number of seconds has passed since the last interaction") parser.add_argument( "--debug", action="store_true", help="Print DEBUG messages to console" ) @@ -32,6 +34,10 @@ def main(): if args.language: data_dict["language"] = args.language + conversation_id_file = Path(_DIR.parent.as_posix()+"/share/conversation_id") + if conversation_id_file.is_file() and time.time() - conversation_id_file.stat().st_mtime < args.conversation_id_timeout: + data_dict["conversation_id"] = conversation_id_file.read_text() + data = json.dumps(data_dict, ensure_ascii=False).encode("utf-8") request = Request(args.url, data=data, headers=headers) @@ -44,6 +50,11 @@ def main(): .get("speech", "") ) print(response_text) + conversation_id = response.get("conversation_id") + + if conversation_id: + conversation_id_file.parent.mkdir(parents=True, exist_ok=True) + conversation_id_file.write_text(conversation_id) if __name__ == "__main__": diff --git a/rhasspy3/configuration.yaml b/rhasspy3/configuration.yaml index 73b068b..95e04b0 100644 --- a/rhasspy3/configuration.yaml +++ b/rhasspy3/configuration.yaml @@ -389,12 +389,13 @@ programs: # 2. Put long-lived access token in etc/token home_assistant: command: | - bin/converse.py --language "${language}" "${url}" "${token_file}" + bin/converse.py --language "${language}" --conversation_id_timeout "${conversation_id_timeout}" "${url}" "${token_file}" adapter: | handle_adapter_text.py template_args: url: "http://localhost:8123/api/conversation/process" token_file: "${data_dir}/token" + conversation_id_timeout: 30 language: "" # Intent only: answer English date/time requests