Skip to content

Commit 9329c87

Browse files
authored
V2 Dialogflow api (#27)
* V2 Dialogflow api * Remove flask from apprentice class * Update example app to reflect apprentice changes * Update examples in readme * Bump version and update changelog
1 parent 9faccbe commit 9329c87

File tree

8 files changed

+80
-63
lines changed

8 files changed

+80
-63
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
### v0.2.0
4+
- Update to Dialogflow API 2.0
5+
- Fix routing issue
6+
37
### v0.1.7
48

59
Clean up the public API interface for Apprentice (#24)

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ from apprentice import Apprentice
6161
apr = Apprentice(__name__)
6262

6363

64-
@apr.action()
64+
@apr.route('/', methods=['POST'])
6565
def hello_world(*args, **kwargs):
6666
reply = 'Hello world!'
6767
return apr.response(reply)
68+
6869
```
6970

7071
## Contributing

apprentice/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
VERSION = (0, 1, 7)
1+
VERSION = (0, 2, 0)
22

33
__version__ = '.'.join(map(str, VERSION))

apprentice/commands.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
apr = Apprentice(__name__)
1111
1212
13-
@apr.action()
13+
@apr.route('/', methods=['POST'])
1414
def hello_world(*args, **kwargs):
1515
reply = 'Hello world!'
1616
return apr.response(reply)
17+
1718
"""
1819

1920
REQUIREMENTS_CONTENT = """apprentice

apprentice/format.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,27 @@ def response(data):
1010
return resp
1111

1212

13-
def intent_response(text, expect_user_response=True):
13+
def query_result(text, expect_user_response=True):
1414
return {
15-
'speech': text,
16-
'displayText': None,
17-
'messages': [
15+
'fulfillmentText': text,
16+
'fulfillmentMessages': [
1817
{
19-
"type": 0,
20-
"speech": text
18+
"platform": "ACTIONS_ON_GOOGLE",
19+
"text": {
20+
"text": [
21+
text
22+
]
23+
}
2124
}
2225
],
23-
'data': {
26+
'payload': {
2427
'google': {
2528
"expect_user_response": expect_user_response,
2629
"is_ssml": True,
2730
"permissions_request": None,
2831
}
2932
},
30-
'contextOut': [],
33+
'outputContexts': [],
3134
'source': 'webhook'
3235
}
3336

@@ -36,19 +39,8 @@ class Apprentice(Flask):
3639

3740
def __init__(self, name, *args, **kwargs):
3841
self.name = name
39-
self.flask = Flask(__name__)
4042
super().__init__(__name__, *args, **kwargs)
4143

42-
def action(self, route='/'):
43-
def decorator(function):
44-
@self.flask.route(route, methods=['POST', 'GET'])
45-
def wrapper(*args, **kwargs):
46-
return function(*args, **kwargs)
47-
48-
return wrapper
49-
50-
return decorator
51-
5244
def response(self, reply):
53-
res = intent_response(reply)
45+
res = query_result(reply)
5446
return response(res)

example/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
}
3030

3131

32-
@apr.action()
32+
@apr.route('/', methods=['POST'])
3333
def cool_fact_generator(*args, **kwargs):
3434
reply = _fact_response('name')
3535
return apr.response(reply)

example/test_webhook.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,46 @@ def api_response(historical_fact_response):
1616
class TestCoolFactGenerator:
1717

1818
def test_returns_response_with_json_when_called(self, birth_post_data):
19-
with apr.flask.test_client() as c:
19+
with apr.test_client() as c:
2020
res = c.post('/', json=birth_post_data)
2121

2222
assert res.json == {
23-
'contextOut': [],
24-
'data': {
23+
'fulfillmentMessages': [
24+
{
25+
'platform': 'ACTIONS_ON_GOOGLE',
26+
'text': {
27+
'text': [
28+
'Today in the year 308, At '
29+
'Carnuntum, Emperor emeritus '
30+
'Diocletian confers with Galerius, '
31+
'Augustus of the East, and '
32+
'Maximianus, the recently returned '
33+
'former Augustus of the West, in '
34+
'an attempt to end the civil wars '
35+
'of the Tetrarchy.'
36+
]
37+
}
38+
}
39+
],
40+
'fulfillmentText': 'Today in the year 308, At Carnuntum, Emperor '
41+
'emeritus Diocletian confers with Galerius, '
42+
'Augustus of the East, and Maximianus, the '
43+
'recently returned former Augustus of the '
44+
'West, in an attempt to end the civil wars of '
45+
'the Tetrarchy.',
46+
'outputContexts': [],
47+
'payload': {
2548
'google': {
2649
'expect_user_response': True,
2750
'is_ssml': True,
2851
'permissions_request': None
29-
},
52+
}
3053
},
31-
'displayText': None,
32-
'messages': [{
33-
'speech': 'Today in the year 308, At Carnuntum, Emperor '
34-
'emeritus Diocletian confers with Galerius, '
35-
'Augustus of the East, and Maximianus, the recently '
36-
'returned former Augustus of the West, in an '
37-
'attempt to end the civil wars of the Tetrarchy.',
38-
'type': 0
39-
}],
40-
'source': 'webhook',
41-
'speech': 'Today in the year 308, At Carnuntum, Emperor '
42-
'emeritus Diocletian confers with Galerius, Augustus '
43-
'of the East, and Maximianus, the recently returned '
44-
'former Augustus of the West, in an attempt to end '
45-
'the civil wars of the Tetrarchy.'
54+
'source': 'webhook'
4655
}
4756

4857
def test_returns_content_type_json_when_called(self, birth_post_data):
49-
with apr.flask.test_client() as c:
58+
with apr.test_client() as c:
5059
res = c.post('/', json=birth_post_data)
5160

5261
assert res.headers['Content-Type'] == 'application/json'

tests/test_core.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,57 @@ class TestIntentResponse:
88

99
def test_returns_object_when_given_text(self):
1010
text = 'Hello world'
11-
json_response = format.intent_response(text, expect_user_response=True)
11+
json_response = format.query_result(text, expect_user_response=True)
1212

1313
assert json_response == {
14-
'contextOut': [],
15-
'data': {
14+
'outputContexts': [],
15+
'payload': {
1616
'google': {
1717
'expect_user_response': True,
1818
'is_ssml': True,
1919
'permissions_request': None
2020
},
2121
},
22-
'displayText': None,
23-
'messages': [{
24-
'speech': 'Hello world',
25-
'type': 0
26-
}],
22+
'fulfillmentMessages': [
23+
{
24+
"platform": "ACTIONS_ON_GOOGLE",
25+
"text": {
26+
"text": [
27+
'Hello world'
28+
]
29+
}
30+
}
31+
],
2732
'source': 'webhook',
28-
'speech': 'Hello world'
33+
'fulfillmentText': 'Hello world'
2934
}
3035

3136
def test_returns_expect_user_response_when_set_to_false(self):
3237
text = 'Hello world'
33-
json_response = format.intent_response(text,
34-
expect_user_response=False)
38+
json_response = format.query_result(text,
39+
expect_user_response=False)
3540

3641
assert json_response == {
37-
'contextOut': [],
38-
'data': {
42+
'outputContexts': [],
43+
'payload': {
3944
'google': {
4045
'expect_user_response': False,
4146
'is_ssml': True,
4247
'permissions_request': None
4348
},
4449
},
45-
'displayText': None,
46-
'messages': [{
47-
'speech': 'Hello world',
48-
'type': 0
49-
}],
50+
'fulfillmentMessages': [
51+
{
52+
"platform": "ACTIONS_ON_GOOGLE",
53+
"text": {
54+
"text": [
55+
'Hello world'
56+
]
57+
}
58+
}
59+
],
5060
'source': 'webhook',
51-
'speech': 'Hello world'
61+
'fulfillmentText': 'Hello world'
5262
}
5363

5464

0 commit comments

Comments
 (0)