Skip to content

Commit a875306

Browse files
committed
Merge branch 'master' of github.com:andrewgy8/apprentice
2 parents bf734b3 + 19f9493 commit a875306

File tree

8 files changed

+290
-75
lines changed

8 files changed

+290
-75
lines changed

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[*]
2+
insert_final_newline = true
3+
trim_trailing_whitespace = true
4+
end_of_line = lf
5+
charset = utf-8
6+
indent_style = space
7+
8+
[*.py]
9+
indent_size = 4
10+
max_line_length = 79
11+
12+
[*.{yml,yaml}]
13+
indent_size = 2

CHANGELOG.md

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

3+
### Unreleased
4+
5+
- SimpleResponse and BasicCardResponse per the docs
6+
- Add .editorconfig
7+
- Update example to use the new response objects
8+
39
### v0.3.1
410
- Remove nested directories from init command
511

apprentice/format.py

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from flask import Flask, make_response
44

5+
from .responses import SimpleResponse
6+
57

68
class Apprentice(Flask):
79

@@ -13,32 +15,15 @@ def generate_text_response(self, reply):
1315
response_object = self.query_result(reply)
1416
return self.generate_response(response_object)
1517

18+
def response_from_object(self, response_obj):
19+
return self.generate_response(response_obj.build())
20+
1621
def generate_response(self, data):
1722
resp = json.dumps(data, indent=4)
1823
resp = make_response(resp)
1924
resp.headers['Content-Type'] = 'application/json'
2025
return resp
2126

2227
def query_result(self, text, expect_user_response=True):
23-
return {
24-
'fulfillmentText': text,
25-
'fulfillmentMessages': [
26-
{
27-
"platform": "ACTIONS_ON_GOOGLE",
28-
"text": {
29-
"text": [
30-
text
31-
]
32-
}
33-
}
34-
],
35-
'payload': {
36-
'google': {
37-
"expect_user_response": expect_user_response,
38-
"is_ssml": True,
39-
"permissions_request": None,
40-
}
41-
},
42-
'outputContexts': [],
43-
'source': 'webhook'
44-
}
28+
response = SimpleResponse(text, expect_user_response)
29+
return response.build()

apprentice/responses.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
class _BaseResponse:
2+
3+
def __init__(self, speech, expect_reply, display_text=None):
4+
self.speech = speech
5+
self.display_text = display_text if display_text else speech
6+
self.expect_reply = expect_reply
7+
self.base_data = {
8+
'payload': {
9+
'google': {
10+
'expect_user_response': False,
11+
'is_ssml': True,
12+
'permissions_request': None,
13+
"richResponse": {
14+
"items": []
15+
}
16+
}
17+
},
18+
'source': 'webhook'
19+
}
20+
21+
def build(self):
22+
message = {
23+
"simpleResponse": {
24+
"textToSpeech": self.speech,
25+
"displayText": self.display_text
26+
}
27+
}
28+
payload = self.base_data['payload']['google']
29+
payload['richResponse']['items'].append(message)
30+
payload['expect_user_response'] = self.expect_reply
31+
return self.base_data
32+
33+
34+
class SimpleResponse(_BaseResponse):
35+
""" Simple text response.
36+
Simple response with both text to speech and display settings.
37+
"""
38+
pass
39+
40+
41+
class BasicCardResponse(_BaseResponse):
42+
43+
def __init__(self,
44+
speech,
45+
title,
46+
image_url,
47+
image_accessibility_text,
48+
button,
49+
expect_reply):
50+
super().__init__(speech, expect_reply)
51+
self.title = title
52+
self.image_uri = image_url
53+
self.image_accessibility_text = image_accessibility_text
54+
self.button = button
55+
56+
def build(self):
57+
super().build()
58+
message = {
59+
"basicCard": {
60+
"buttons": [self.button],
61+
"image": {
62+
"url": self.image_uri,
63+
"accessibilityText": self.image_accessibility_text
64+
},
65+
"title": self.title
66+
}
67+
}
68+
payload = self.base_data['payload']['google']
69+
payload['richResponse']['items'].append(message)
70+
return self.base_data

example/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import requests
44

5-
from apprentice import Apprentice
5+
from apprentice import Apprentice, SimpleResponse
66

77
apr = Apprentice(__name__)
88

@@ -32,7 +32,8 @@
3232
@apr.route('/', methods=['POST'])
3333
def cool_fact_generator(*args, **kwargs):
3434
reply = _fact_response('name')
35-
return apr.generate_text_response(reply)
35+
obj = SimpleResponse(speech=reply, expect_reply=True)
36+
return apr.response_from_object(obj)
3637

3738

3839
def _fact_response(entity):

example/test_webhook.py

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,38 @@ def test_returns_response_with_json_when_called(self, birth_post_data):
2020
res = c.post('/', json=birth_post_data)
2121

2222
assert res.json == {
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': [],
4723
'payload': {
4824
'google': {
4925
'expect_user_response': True,
5026
'is_ssml': True,
51-
'permissions_request': None
27+
'permissions_request': None,
28+
'richResponse': {
29+
'items': [
30+
{
31+
'simpleResponse': {
32+
'textToSpeech':
33+
'Today in the year 308, At '
34+
'Carnuntum, Emperor emeritus '
35+
'Diocletian confers with Galerius, '
36+
'Augustus of the East, and '
37+
'Maximianus, the recently returned '
38+
'former Augustus of the West, in '
39+
'an attempt to end the civil wars '
40+
'of the Tetrarchy.',
41+
'displayText':
42+
'Today in the year 308, At '
43+
'Carnuntum, Emperor emeritus '
44+
'Diocletian confers with Galerius, '
45+
'Augustus of the East, and '
46+
'Maximianus, the recently returned '
47+
'former Augustus of the West, in '
48+
'an attempt to end the civil wars '
49+
'of the Tetrarchy.'
50+
51+
}
52+
}
53+
]
54+
}
5255
}
5356
},
5457
'source': 'webhook'

tests/test_core.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,25 @@ def test_returns_object_when_given_text(self):
1111
json_response = apr.query_result(text, expect_user_response=True)
1212

1313
assert json_response == {
14-
'outputContexts': [],
1514
'payload': {
1615
'google': {
1716
'expect_user_response': True,
1817
'is_ssml': True,
19-
'permissions_request': None
20-
},
21-
},
22-
'fulfillmentMessages': [
23-
{
24-
"platform": "ACTIONS_ON_GOOGLE",
25-
"text": {
26-
"text": [
27-
'Hello world'
18+
'permissions_request': None,
19+
'richResponse': {
20+
'items': [
21+
{
22+
"simpleResponse": {
23+
"textToSpeech": 'Hello world',
24+
"displayText": 'Hello world'
25+
26+
}
27+
}
2828
]
2929
}
30-
}
31-
],
30+
},
31+
},
3232
'source': 'webhook',
33-
'fulfillmentText': 'Hello world'
3433
}
3534

3635
def test_returns_expect_user_response_when_set_to_false(self):
@@ -39,26 +38,25 @@ def test_returns_expect_user_response_when_set_to_false(self):
3938
json_response = apr.query_result(text, expect_user_response=False)
4039

4140
assert json_response == {
42-
'outputContexts': [],
4341
'payload': {
4442
'google': {
4543
'expect_user_response': False,
4644
'is_ssml': True,
47-
'permissions_request': None
48-
},
49-
},
50-
'fulfillmentMessages': [
51-
{
52-
"platform": "ACTIONS_ON_GOOGLE",
53-
"text": {
54-
"text": [
55-
'Hello world'
45+
'permissions_request': None,
46+
'richResponse': {
47+
'items': [
48+
{
49+
"simpleResponse": {
50+
"textToSpeech": 'Hello world',
51+
"displayText": 'Hello world'
52+
53+
}
54+
}
5655
]
5756
}
58-
}
59-
],
57+
},
58+
},
6059
'source': 'webhook',
61-
'fulfillmentText': 'Hello world'
6260
}
6361

6462

0 commit comments

Comments
 (0)