Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit d2fb9df

Browse files
author
bweigel
committed
adds alexa skill event
1 parent 7ef8be4 commit d2fb9df

File tree

3 files changed

+220
-0
lines changed

3 files changed

+220
-0
lines changed

lawip/alexa_skill_event.py

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"session": {
3+
"new": false,
4+
"sessionId": "amzn1.echo-api.session.[unique-value-here]",
5+
"attributes": {},
6+
"user": {
7+
"userId": "amzn1.ask.account.[unique-value-here]"
8+
},
9+
"application": {
10+
"applicationId": "amzn1.ask.skill.[unique-value-here]"
11+
}
12+
},
13+
"version": "1.0",
14+
"request": {
15+
"locale": "en-US",
16+
"timestamp": "2016-10-27T21:06:28Z",
17+
"type": "IntentRequest",
18+
"requestId": "amzn1.echo-api.request.[unique-value-here]",
19+
"intent": {
20+
"slots": {
21+
"Item": {
22+
"name": "Item",
23+
"value": "snowball"
24+
}
25+
},
26+
"name": "RecipeIntent"
27+
}
28+
},
29+
"context": {
30+
"AudioPlayer": {
31+
"playerActivity": "IDLE"
32+
},
33+
"System": {
34+
"device": {
35+
"supportedInterfaces": {
36+
"AudioPlayer": {}
37+
}
38+
},
39+
"application": {
40+
"applicationId": "amzn1.ask.skill.[unique-value-here]"
41+
},
42+
"user": {
43+
"userId": "amzn1.ask.account.[unique-value-here]"
44+
}
45+
}
46+
}
47+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from unittest2 import TestCase
2+
3+
from lawip.alexa_skill_event import AlexaSkillEvent
4+
from lawip.sns_event import SnsEvent
5+
6+
from lawip.test.util import get_event_dict
7+
8+
9+
class TestSnsEvent(TestCase):
10+
def setUp(self):
11+
event_dict = get_event_dict("alexa-event.json")
12+
self.event = AlexaSkillEvent.from_event(event_dict)
13+
14+
def test_alexa_skill(self):
15+
evt = self.event
16+
self.assertEqual(evt.event_version, "1.0")
17+
self.assertIsNotNone(evt.request)
18+
self.assertIsNotNone(evt.session)
19+
self.assertIsNotNone(evt.context)
20+
21+
def test_skill_session(self):
22+
session = self.event.session
23+
self.assertEqual(session.application.applicationId, "amzn1.ask.skill.[unique-value-here]")
24+
self.assertEqual(session.session_id, "amzn1.echo-api.session.[unique-value-here]")
25+
self.assertFalse(session.new)
26+
27+
def test_skill_request(self):
28+
request = self.event.request
29+
self.assertEqual(request.intent.name, "RecipeIntent")
30+
self.assertEqual(request.intent.slots.get("Item").value, "snowball")
31+
self.assertEqual(request.locale, "en-US")

0 commit comments

Comments
 (0)