Voice-Plugins/car-manual-skill/__init__.py

62 lines
1.9 KiB
Python

import json
from pathlib import Path
from mycroft import MycroftSkill, intent_file_handler
import requests
def emit(osd_url, event, data):
json = {
"event": event,
"data": data
}
print(json)
return requests.post(osd_url, json=json)
class CarManual(MycroftSkill):
def __init__(self):
MycroftSkill.__init__(self)
# Generate the files
root_path = str(Path(__file__).absolute().parent)
root_locale_path = root_path + "/locale/en-us"
with open(root_path + "/questions.json") as questions_file:
questions = json.load(questions_file)
self.questions = questions
for qid, question in questions.items():
with open(f"{root_locale_path}/{qid}.intent", "w") as intent_file:
for intent in question["questions"]:
intent_file.write(intent)
intent_file.write("\n")
with open(f"{root_locale_path}/{qid}.dialog", "w") as dialog_file:
for dialog in question["answers"]:
dialog_file.write(dialog)
dialog_file.write("\n")
def initialize(self):
def handler(intent):
return lambda message: self.generic_handler(intent, message)
for key in self.questions:
self.register_intent(f"{key}.intent", handler(key))
def generic_handler(self, intent_id, message):
title = self.questions[intent_id]["title"]
dialog = self.dialog_renderer.render(intent_id, {})
url = self.settings.get("OSD_API", "http://localhost:5050/send")
emission = {
"plugin": "manual",
"data": {
"title": title,
"description": dialog
},
"time": 10000
}
emit(url, "switchPlugin", emission)
self.speak(dialog)
def create_skill():
return CarManual()