Skip to content

Commit

Permalink
V2 Dialogflow api (#27)
Browse files Browse the repository at this point in the history
* V2 Dialogflow api 

* Remove flask from apprentice class

* Update example app to reflect apprentice changes

* Update examples in readme

* Bump version and update changelog
  • Loading branch information
andrewgy8 authored Dec 6, 2018
1 parent 9faccbe commit 9329c87
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 63 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion apprentice/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
VERSION = (0, 1, 7)
VERSION = (0, 2, 0)

__version__ = '.'.join(map(str, VERSION))
3 changes: 2 additions & 1 deletion apprentice/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 12 additions & 20 deletions apprentice/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

Expand All @@ -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)
2 changes: 1 addition & 1 deletion example/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
}


@apr.action()
@apr.route('/', methods=['POST'])
def cool_fact_generator(*args, **kwargs):
reply = _fact_response('name')
return apr.response(reply)
Expand Down
49 changes: 29 additions & 20 deletions example/test_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
48 changes: 29 additions & 19 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}


Expand Down

0 comments on commit 9329c87

Please sign in to comment.