Skip to content

Commit

Permalink
Merge pull request johnwheeler#227 from w19787/master
Browse files Browse the repository at this point in the history
enhance core to support custom slot type "Entity resolution"
  • Loading branch information
johnwheeler authored Jun 15, 2018
2 parents 7456ea5 + 9854f84 commit 7065c96
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 1 deletion.
20 changes: 19 additions & 1 deletion flask_ask/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ def init_app(self, app, path='templates.yaml'):
if self._route is None:
raise TypeError("route is a required argument when app is not None")

self.app = app

app.ask = self

app.add_url_rule(self._route, view_func=self._flask_view_func, methods=['POST'])
Expand Down Expand Up @@ -804,6 +806,22 @@ def _map_player_request_to_func(self, player_request_type):

return partial(view_func, *arg_values)

def _get_slot_value(self, slot_object):
slot_name = slot_object.name
slot_value = getattr(slot_object, 'value', None)
resolutions = getattr(slot_object, 'resolutions', None)

if resolutions is not None:
resolutions_per_authority = getattr(resolutions, 'resolutionsPerAuthority', None)
if resolutions_per_authority is not None and len(resolutions_per_authority) > 0:
values = resolutions_per_authority[0].get('values', None)
if values is not None and len(values) > 0:
value = values[0].get('value', None)
if value is not None:
slot_value = value.get('name', slot_value)

return slot_value

def _map_params_to_view_args(self, view_name, arg_names):

arg_values = []
Expand All @@ -819,7 +837,7 @@ def _map_params_to_view_args(self, view_name, arg_names):
if intent.slots is not None:
for slot_key in intent.slots.keys():
slot_object = getattr(intent.slots, slot_key)
request_data[slot_object.name] = getattr(slot_object, 'value', None)
request_data[slot_object.name] = self._get_slot_value(slot_object=slot_object)

else:
for param_name in self.request:
Expand Down
114 changes: 114 additions & 0 deletions tests/test_integration_support_entity_resolution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import unittest
import json
import uuid

from flask_ask import Ask, statement
from flask import Flask


play_request = {
"version": "1.0",
"session": {
"new": False,
"sessionId": "amzn1.echo-api.session.f6ebc0ba-9d7a-4c3f-b056-b6c3f9da0713",
"application": {
"applicationId": "amzn1.ask.skill.26338c44-65da-4d58-aa75-c86b21271eb7"
},
"user": {
"userId": "amzn1.ask.account.AHR7KBC3MFCX7LYT6HJBGDLIGQUU3FLANWCZ",
}
},
"context": {
"AudioPlayer": {
"playerActivity": "IDLE"
},
"Display": {
"token": ""
},
"System": {
"application": {
"applicationId": "amzn1.ask.skill.26338c44-65da-4d58-aa75-c86b21271eb7"
},
"user": {
"userId": "amzn1.ask.account.AHR7KBC3MFCX7LYT6HJBGDLIGQUU3FLANWCZ",
},
"device": {
"deviceId": "amzn1.ask.device.AELNXV4JQJMF5QALYUQXHOZJ",
"supportedInterfaces": {
"AudioPlayer": {},
"Display": {
"templateVersion": "1.0",
"markupVersion": "1.0"
}
}
},
"apiEndpoint": "https://api.amazonalexa.com",
}
},
"request": {
"type": "IntentRequest",
"requestId": "amzn1.echo-api.request.4859a7e3-1960-4ed9-ac7b-854309346916",
"timestamp": "2018-04-04T06:28:23Z",
"locale": "en-US",
"intent": {
"name": "TestCustomSlotTypeIntents",
"confirmationStatus": "NONE",
"slots": {
"child_info": {
"name": "child_info",
"value": "friends info",
"resolutions": {
"resolutionsPerAuthority": [
{
"authority": "amzn1.er-authority.echo-sdk.amzn1.ask.skill.26338c44-65da-4d58-aa75-c86b21271eb7.child_info_type",
"status": {
"code": "ER_SUCCESS_MATCH"
},
"values": [
{
"value": {
"name": "friend_info",
"id": "FRIEND_INFO"
}
}
]
}
]
},
"confirmationStatus": "NONE"
}
}
},
"dialogState": "STARTED"
}
}


class CustomSlotTypeIntegrationTests(unittest.TestCase):
""" Integration tests of the custom slot type """

def setUp(self):
self.app = Flask(__name__)
self.app.config['ASK_VERIFY_REQUESTS'] = False
self.ask = Ask(app=self.app, route='/ask')
self.client = self.app.test_client()

@self.ask.intent('TestCustomSlotTypeIntents')
def custom_slot_type_intents(child_info):
return statement(child_info)

def tearDown(self):
pass

def test_custom_slot_type_intent(self):
""" Test to see if custom slot type value is correct """
response = self.client.post('/ask', data=json.dumps(play_request))
self.assertEqual(200, response.status_code)

data = json.loads(response.data.decode('utf-8'))
self.assertEqual('friend_info',
data['response']['outputSpeech']['text'])


if __name__ == '__main__':
unittest.main()

0 comments on commit 7065c96

Please sign in to comment.