|
| 1 | +from collections import namedtuple |
| 2 | +from typing import Dict |
| 3 | + |
| 4 | +IntentSlot = namedtuple("IntentSlot", ['name', 'value']) |
| 5 | +AlexaSkillUser = namedtuple("AlexaSkillUser", ["userId"]) |
| 6 | +AlexaSkillApplication = namedtuple("AlexaSkillApplication", ["applicationId"]) |
| 7 | + |
| 8 | + |
| 9 | +def parse_intents(slots: Dict[str, Dict[str, str]]) -> Dict[str, IntentSlot]: |
| 10 | + return {slot_name: IntentSlot(slot.get("key"), |
| 11 | + slot.get("value")) for slot_name, slot in slots.items()} |
| 12 | + |
| 13 | + |
| 14 | +class AlexaIntent: |
| 15 | + def __init__(self, name: str, |
| 16 | + slots: Dict): |
| 17 | + self._name = name |
| 18 | + self._slots = parse_intents(slots) |
| 19 | + |
| 20 | + @classmethod |
| 21 | + def from_json(cls, intent: Dict): |
| 22 | + return cls(**intent) |
| 23 | + |
| 24 | + @property |
| 25 | + def name(self) -> str: |
| 26 | + return self._name |
| 27 | + |
| 28 | + @property |
| 29 | + def slots(self) -> Dict[str, IntentSlot]: |
| 30 | + return self._slots |
| 31 | + |
| 32 | + |
| 33 | +class AlexaSkillRequest: |
| 34 | + def __init__(self, locale: str, timestamp: str, |
| 35 | + type: str, requestId: str, |
| 36 | + intent: Dict = None, reason: str = None): |
| 37 | + self._locale = locale |
| 38 | + self._timestamp = timestamp |
| 39 | + self._request_id = requestId |
| 40 | + self._type = type |
| 41 | + self._intent = AlexaIntent(**intent) |
| 42 | + self._reason = reason |
| 43 | + |
| 44 | + @classmethod |
| 45 | + def from_json(cls, session: Dict): |
| 46 | + return cls(**session) |
| 47 | + |
| 48 | + @property |
| 49 | + def locale(self) -> str: |
| 50 | + return self._locale |
| 51 | + |
| 52 | + @property |
| 53 | + def timestamp(self) -> str: |
| 54 | + return self._timestamp |
| 55 | + |
| 56 | + @property |
| 57 | + def request_id(self) -> str: |
| 58 | + return self._request_id |
| 59 | + |
| 60 | + @property |
| 61 | + def type(self) -> str: |
| 62 | + return self._type |
| 63 | + |
| 64 | + @property |
| 65 | + def reason(self) -> str: |
| 66 | + return self._reason |
| 67 | + |
| 68 | + @property |
| 69 | + def intent(self) -> AlexaIntent: |
| 70 | + return self._intent |
| 71 | + |
| 72 | + |
| 73 | +class AlexaSkillSession: |
| 74 | + def __init__(self, new: bool, |
| 75 | + sessionId: str, |
| 76 | + user: Dict[str, str], |
| 77 | + application: Dict[str, str], |
| 78 | + attributes: Dict = None): |
| 79 | + self._new = new |
| 80 | + self._session_id = sessionId |
| 81 | + self._attributes = attributes |
| 82 | + self._user = AlexaSkillUser(**user) |
| 83 | + self._application = AlexaSkillApplication(**application) |
| 84 | + |
| 85 | + @classmethod |
| 86 | + def from_json(cls, session): |
| 87 | + return cls(**session) |
| 88 | + |
| 89 | + @property |
| 90 | + def new(self) -> bool: |
| 91 | + return self._new |
| 92 | + |
| 93 | + @property |
| 94 | + def session_id(self) -> str: |
| 95 | + return self._session_id |
| 96 | + |
| 97 | + @property |
| 98 | + def attributes(self) -> Dict: |
| 99 | + return self._attributes |
| 100 | + |
| 101 | + @property |
| 102 | + def user(self) -> AlexaSkillUser: |
| 103 | + return self._user |
| 104 | + |
| 105 | + @property |
| 106 | + def application(self) -> AlexaSkillApplication: |
| 107 | + return self._application |
| 108 | + |
| 109 | + |
| 110 | +class AlexaSkillContext: |
| 111 | + def __init__(self, AudioPlayer, System, **kwargs): |
| 112 | + pass |
| 113 | + |
| 114 | + |
| 115 | +class AlexaSkillEvent: |
| 116 | + def __init__(self, session: Dict, |
| 117 | + version: str, request: Dict, |
| 118 | + context: Dict): |
| 119 | + self._session = AlexaSkillSession(**session) |
| 120 | + self._version = version |
| 121 | + self._request = AlexaSkillRequest(**request) |
| 122 | + self._context = AlexaSkillContext(**context) |
| 123 | + |
| 124 | + @classmethod |
| 125 | + def from_event(cls, event): |
| 126 | + return cls(**event) |
| 127 | + |
| 128 | + @property |
| 129 | + def session(self) -> AlexaSkillSession: |
| 130 | + return self._session |
| 131 | + |
| 132 | + @property |
| 133 | + def event_version(self) -> str: |
| 134 | + return self._version |
| 135 | + |
| 136 | + @property |
| 137 | + def request(self) -> AlexaSkillRequest: |
| 138 | + return self._request |
| 139 | + |
| 140 | + @property |
| 141 | + def context(self) -> AlexaSkillContext: |
| 142 | + return self._context |
0 commit comments