diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b10964..bd7db81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +### v0.2.0 +- Update to Dialogflow API 2.0 +- Fix routing issue + ### v0.1.7 Clean up the public API interface for Apprentice (#24) diff --git a/README.md b/README.md index bc8ac5e..52a38f6 100644 --- a/README.md +++ b/README.md @@ -61,10 +61,11 @@ from apprentice import Apprentice apr = Apprentice(__name__) -@apr.action() +@apr.route('/', methods=['POST']) def hello_world(*args, **kwargs): reply = 'Hello world!' return apr.response(reply) + ``` ## Contributing diff --git a/apprentice/__version__.py b/apprentice/__version__.py index e35982a..dfd69f9 100644 --- a/apprentice/__version__.py +++ b/apprentice/__version__.py @@ -1,3 +1,3 @@ -VERSION = (0, 1, 7) +VERSION = (0, 2, 0) __version__ = '.'.join(map(str, VERSION)) diff --git a/apprentice/commands.py b/apprentice/commands.py index 01230d1..b287dff 100644 --- a/apprentice/commands.py +++ b/apprentice/commands.py @@ -10,10 +10,11 @@ apr = Apprentice(__name__) -@apr.action() +@apr.route('/', methods=['POST']) def hello_world(*args, **kwargs): reply = 'Hello world!' return apr.response(reply) + """ REQUIREMENTS_CONTENT = """apprentice diff --git a/apprentice/format.py b/apprentice/format.py index f628a1c..635e969 100644 --- a/apprentice/format.py +++ b/apprentice/format.py @@ -10,24 +10,27 @@ def response(data): return resp -def intent_response(text, expect_user_response=True): +def query_result(text, expect_user_response=True): return { - 'speech': text, - 'displayText': None, - 'messages': [ + 'fulfillmentText': text, + 'fulfillmentMessages': [ { - "type": 0, - "speech": text + "platform": "ACTIONS_ON_GOOGLE", + "text": { + "text": [ + text + ] + } } ], - 'data': { + 'payload': { 'google': { "expect_user_response": expect_user_response, "is_ssml": True, "permissions_request": None, } }, - 'contextOut': [], + 'outputContexts': [], 'source': 'webhook' } @@ -36,19 +39,8 @@ class Apprentice(Flask): def __init__(self, name, *args, **kwargs): self.name = name - self.flask = Flask(__name__) super().__init__(__name__, *args, **kwargs) - def action(self, route='/'): - def decorator(function): - @self.flask.route(route, methods=['POST', 'GET']) - def wrapper(*args, **kwargs): - return function(*args, **kwargs) - - return wrapper - - return decorator - def response(self, reply): - res = intent_response(reply) + res = query_result(reply) return response(res) diff --git a/example/main.py b/example/main.py index 4f837ab..236a82c 100644 --- a/example/main.py +++ b/example/main.py @@ -29,7 +29,7 @@ } -@apr.action() +@apr.route('/', methods=['POST']) def cool_fact_generator(*args, **kwargs): reply = _fact_response('name') return apr.response(reply) diff --git a/example/test_webhook.py b/example/test_webhook.py index 04bab56..4eb405d 100644 --- a/example/test_webhook.py +++ b/example/test_webhook.py @@ -16,37 +16,46 @@ def api_response(historical_fact_response): class TestCoolFactGenerator: def test_returns_response_with_json_when_called(self, birth_post_data): - with apr.flask.test_client() as c: + with apr.test_client() as c: res = c.post('/', json=birth_post_data) assert res.json == { - 'contextOut': [], - 'data': { + 'fulfillmentMessages': [ + { + 'platform': 'ACTIONS_ON_GOOGLE', + 'text': { + 'text': [ + 'Today in the year 308, At ' + 'Carnuntum, Emperor emeritus ' + 'Diocletian confers with Galerius, ' + 'Augustus of the East, and ' + 'Maximianus, the recently returned ' + 'former Augustus of the West, in ' + 'an attempt to end the civil wars ' + 'of the Tetrarchy.' + ] + } + } + ], + 'fulfillmentText': 'Today in the year 308, At Carnuntum, Emperor ' + 'emeritus Diocletian confers with Galerius, ' + 'Augustus of the East, and Maximianus, the ' + 'recently returned former Augustus of the ' + 'West, in an attempt to end the civil wars of ' + 'the Tetrarchy.', + 'outputContexts': [], + 'payload': { 'google': { 'expect_user_response': True, 'is_ssml': True, 'permissions_request': None - }, + } }, - 'displayText': None, - 'messages': [{ - 'speech': 'Today in the year 308, At Carnuntum, Emperor ' - 'emeritus Diocletian confers with Galerius, ' - 'Augustus of the East, and Maximianus, the recently ' - 'returned former Augustus of the West, in an ' - 'attempt to end the civil wars of the Tetrarchy.', - 'type': 0 - }], - 'source': 'webhook', - 'speech': 'Today in the year 308, At Carnuntum, Emperor ' - 'emeritus Diocletian confers with Galerius, Augustus ' - 'of the East, and Maximianus, the recently returned ' - 'former Augustus of the West, in an attempt to end ' - 'the civil wars of the Tetrarchy.' + 'source': 'webhook' } def test_returns_content_type_json_when_called(self, birth_post_data): - with apr.flask.test_client() as c: + with apr.test_client() as c: res = c.post('/', json=birth_post_data) assert res.headers['Content-Type'] == 'application/json' diff --git a/tests/test_core.py b/tests/test_core.py index bec4f78..4f71374 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -8,47 +8,57 @@ class TestIntentResponse: def test_returns_object_when_given_text(self): text = 'Hello world' - json_response = format.intent_response(text, expect_user_response=True) + json_response = format.query_result(text, expect_user_response=True) assert json_response == { - 'contextOut': [], - 'data': { + 'outputContexts': [], + 'payload': { 'google': { 'expect_user_response': True, 'is_ssml': True, 'permissions_request': None }, }, - 'displayText': None, - 'messages': [{ - 'speech': 'Hello world', - 'type': 0 - }], + 'fulfillmentMessages': [ + { + "platform": "ACTIONS_ON_GOOGLE", + "text": { + "text": [ + 'Hello world' + ] + } + } + ], 'source': 'webhook', - 'speech': 'Hello world' + 'fulfillmentText': 'Hello world' } def test_returns_expect_user_response_when_set_to_false(self): text = 'Hello world' - json_response = format.intent_response(text, - expect_user_response=False) + json_response = format.query_result(text, + expect_user_response=False) assert json_response == { - 'contextOut': [], - 'data': { + 'outputContexts': [], + 'payload': { 'google': { 'expect_user_response': False, 'is_ssml': True, 'permissions_request': None }, }, - 'displayText': None, - 'messages': [{ - 'speech': 'Hello world', - 'type': 0 - }], + 'fulfillmentMessages': [ + { + "platform": "ACTIONS_ON_GOOGLE", + "text": { + "text": [ + 'Hello world' + ] + } + } + ], 'source': 'webhook', - 'speech': 'Hello world' + 'fulfillmentText': 'Hello world' }