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

62 lines
1.9 KiB
Python
Raw Normal View History

import json
from pathlib import Path
2020-12-21 16:08:28 +01:00
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))
2020-12-21 16:08:28 +01:00
def generic_handler(self, intent_id, message):
title = self.questions[intent_id]["title"]
dialog = self.dialog_renderer.render(intent_id, {})
2020-12-21 16:08:28 +01:00
url = self.settings.get("OSD_API", "http://localhost:5050/send")
emission = {
"plugin": "manual",
"data": {
"title": title,
2020-12-21 16:08:28 +01:00
"description": dialog
},
2021-01-04 18:44:57 +01:00
"time": len(dialog) * 50 + 3000
2020-12-21 16:08:28 +01:00
}
emit(url, "switchPlugin", emission)
self.speak(dialog)
def create_skill():
return CarManual()