Skip to content

Commit d7d2d3a

Browse files
author
Pashutan Modaresi
committed
Add functionality to send receipt templates
1 parent c2bca68 commit d7d2d3a

14 files changed

Lines changed: 448 additions & 66 deletions

File tree

docs/autogen.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,22 @@
1919

2020
PAGES = [
2121
{
22-
"page": "list_template/list_template.md",
22+
"page": "receipt_template/receipt_template.md",
2323
"classes": [
24-
fbotics.models.payloads.list_template.ListTemplatePayload,
24+
fbotics.models.payloads.receipt_template.ReceiptTemplatePayload,
25+
fbotics.models.payloads.receipt_template.Element,
26+
fbotics.models.payloads.receipt_template.Address,
27+
fbotics.models.payloads.receipt_template.Summary,
28+
fbotics.models.payloads.receipt_template.Adjustment,
2529
],
2630
},
31+
{
32+
"page": "list_template/list_template.md",
33+
"classes": [fbotics.models.payloads.list_template.ListTemplatePayload],
34+
},
2735
{
2836
"page": "generic_template/generic_template.md",
29-
"classes": [
30-
fbotics.models.payloads.generic_template.GenericTemplatePayload,
31-
],
37+
"classes": [fbotics.models.payloads.generic_template.GenericTemplatePayload],
3238
},
3339
{
3440
"page": "client/client.md",

docs/mkdocs.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@ nav:
2525
- List Template:
2626
- Template Payload: list_template/list_template.md
2727
- Example: list_template/example.md
28+
- Receipt Template:
29+
- Template Payload: receipt_template/receipt_template.md
30+
- Example: receipt_template/example.md
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
This is an example to send Receipt Templates using FBotics:
2+
3+
```python
4+
from fbotics.client import Client
5+
from fbotics.models.quick_reply import QuickReply
6+
from fbotics.models.payloads.receipt_template import (
7+
Element,
8+
Adjustment,
9+
Summary,
10+
Address,
11+
)
12+
13+
14+
client = Client(page_access_token=PAGE_ACCESS_TOKEN)
15+
16+
e1 = Element(
17+
dict(
18+
title="Classic White T-Shirt",
19+
subtitle="100% Soft and Luxurious Cotton",
20+
quantity=2,
21+
price=50,
22+
currency="USD",
23+
image_url="http://petersapparel.parseapp.com/img/whiteshirt.png",
24+
)
25+
)
26+
27+
e2 = Element(
28+
dict(
29+
title="Classic Gray T-Shirt",
30+
subtitle="100% Soft and Luxurious Cotton",
31+
quantity=1,
32+
price=25,
33+
currency="USD",
34+
image_url="http://petersapparel.parseapp.com/img/grayshirt.png",
35+
)
36+
)
37+
38+
address = Address(
39+
dict(
40+
street_1="1 Hacker Way",
41+
street_2="",
42+
city="Menlo Park",
43+
postal_code="94025",
44+
state="CA",
45+
country="US",
46+
)
47+
)
48+
49+
summary = Summary(
50+
dict(subtotal=75.00, shipping_cost=4.95, total_tax=6.19, total_cost=56.14)
51+
)
52+
53+
adjustments = [
54+
Adjustment(dict(name="New Customer Discount", amount=20)),
55+
Adjustment(dict(name="$10 Off Coupon", amount=10)),
56+
]
57+
58+
qr1 = QuickReply(
59+
dict(
60+
content_type="text",
61+
title="Yes",
62+
payload="payload1",
63+
image_url="http://i64.tinypic.com/1hothh.png",
64+
)
65+
)
66+
67+
qr2 = QuickReply(
68+
dict(
69+
content_type="text",
70+
title="No",
71+
payload="payload2",
72+
image_url="http://i63.tinypic.com/2pqpbth.png",
73+
)
74+
)
75+
76+
response = client.send_receipt_template(
77+
recipient_id=RECIPIENT_ID,
78+
quick_replies=[qr1, qr2],
79+
elements=[e1, e2],
80+
recipient_name="Stephane Crozatier",
81+
order_number="12345678902",
82+
currency="USD",
83+
payment_method="Visa 2345",
84+
timestamp="1428444852",
85+
address=address,
86+
summary=summary,
87+
adjustments=adjustments,
88+
)
89+
```
90+
91+
<p float="center">
92+
<img src="https://scontent-frx5-1.xx.fbcdn.net/v/t39.2365-6/23208289_889588377870772_7170760265053503488_n.png?_nc_cat=103&_nc_ht=scontent-frx5-1.xx&oh=7c03f776fea86f91a52c799d6eb02825&oe=5D42A68C" width="40%" />
93+
</p>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The receipt template allows you to send an order confirmation as a structured message. The template may include an order summary, payment details, and shipping information.
2+
3+
4+
{{autogenerated}}

fbotics/_version.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def versions_from_parentdir(parentdir_prefix, root, verbose):
120120
dirname = os.path.basename(root)
121121
if dirname.startswith(parentdir_prefix):
122122
return {
123-
"version": dirname[len(parentdir_prefix):],
123+
"version": dirname[len(parentdir_prefix) :],
124124
"full-revisionid": None,
125125
"dirty": False,
126126
"error": None,
@@ -132,8 +132,8 @@ def versions_from_parentdir(parentdir_prefix, root, verbose):
132132

133133
if verbose:
134134
print(
135-
"Tried directories %s but none started with prefix %s"
136-
% (str(rootdirs), parentdir_prefix)
135+
"Tried directories %s but none started with prefix %s"
136+
% (str(rootdirs), parentdir_prefix)
137137
)
138138
raise NotThisMethod("rootdir doesn't start with parentdir_prefix")
139139

@@ -190,7 +190,7 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
190190
# starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of
191191
# just "foo-1.0". If we see a "tag: " prefix, prefer those.
192192
TAG = "tag: "
193-
tags = set([r[len(TAG):] for r in refs if r.startswith(TAG)])
193+
tags = set([r[len(TAG) :] for r in refs if r.startswith(TAG)])
194194
if not tags:
195195
# Either we're using git < 1.8.3, or there really are no tags. We use
196196
# a heuristic: assume all version tags have a digit. The old git %d
@@ -207,7 +207,7 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
207207
for ref in sorted(tags):
208208
# sorting will prefer e.g. "2.0" over "2.0rc1"
209209
if ref.startswith(tag_prefix):
210-
r = ref[len(tag_prefix):]
210+
r = ref[len(tag_prefix) :]
211211
if verbose:
212212
print("picking %s" % r)
213213
return {
@@ -307,7 +307,7 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
307307
tag_prefix,
308308
)
309309
return pieces
310-
pieces["closest-tag"] = full_tag[len(tag_prefix):]
310+
pieces["closest-tag"] = full_tag[len(tag_prefix) :]
311311

312312
# distance: number of commits since tag
313313
pieces["distance"] = int(mo.group(2))

fbotics/client/__init__.py

Lines changed: 101 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from fbotics.models.payloads.button_template import ButtonTemplatePayload
88
from fbotics.models.payloads.generic_template import GenericTemplatePayload
99
from fbotics.models.payloads.list_template import ListTemplatePayload
10+
from fbotics.models.payloads.receipt_template import ReceiptTemplatePayload
1011
from fbotics.models.payloads.rich_media import RichMediaPayload
1112
from fbotics.models.recipient import Recipient
1213

@@ -18,13 +19,13 @@ def __init__(self, page_access_token=None):
1819
self.page_access_token = page_access_token
1920

2021
def send_button_template(
21-
self,
22-
recipient_id=None,
23-
user_ref=None,
24-
phone_number=None,
25-
text=None,
26-
quick_replies=None,
27-
buttons=None,
22+
self,
23+
recipient_id=None,
24+
user_ref=None,
25+
phone_number=None,
26+
text=None,
27+
quick_replies=None,
28+
buttons=None,
2829
):
2930
"""Sends a button template to the recipient.
3031
@@ -47,12 +48,12 @@ def send_button_template(
4748
return response
4849

4950
def send_generic_template(
50-
self,
51-
recipient_id=None,
52-
user_ref=None,
53-
phone_number=None,
54-
elements=None,
55-
quick_replies=None,
51+
self,
52+
recipient_id=None,
53+
user_ref=None,
54+
phone_number=None,
55+
elements=None,
56+
quick_replies=None,
5657
):
5758
"""Sends a generic template to the recipient.
5859
@@ -75,13 +76,13 @@ def send_generic_template(
7576
return response
7677

7778
def send_list_template(
78-
self,
79-
recipient_id=None,
80-
user_ref=None,
81-
phone_number=None,
82-
elements=None,
83-
buttons=None,
84-
quick_replies=None
79+
self,
80+
recipient_id=None,
81+
user_ref=None,
82+
phone_number=None,
83+
elements=None,
84+
buttons=None,
85+
quick_replies=None,
8586
):
8687
"""Sends a list template to the recipient.
8788
@@ -104,13 +105,67 @@ def send_list_template(
104105
response = self._post(message, recipient_id, user_ref, phone_number)
105106
return response
106107

108+
def send_receipt_template(
109+
self,
110+
recipient_id=None,
111+
user_ref=None,
112+
phone_number=None,
113+
recipient_name=None,
114+
elements=None,
115+
order_number=None,
116+
currency=None,
117+
payment_method=None,
118+
timestamp=None,
119+
address=None,
120+
summary=None,
121+
adjustments=None,
122+
quick_replies=None,
123+
):
124+
"""Sends a receipt template to the recipient.
125+
126+
# Arguments
127+
recipient_id: page specific id of the recipient
128+
user_ref: optional. user_ref from the checkbox plugin
129+
phone_number: Optional. Phone number of the recipient with the format +1(212)555-2368. Your bot must be approved for Customer Matching to send messages this way.
130+
elements: Optional. Array of a maximum of 100 element objects that describe items in the order. Sort order of the elements is not guaranteed.
131+
order_number: The order number. Must be unique.
132+
currency: The currency of the payment.
133+
payment_method: The payment method used. Providing enough information for the customer to decipher which payment method and account they used is recommended. This can be a custom string, such as, "Visa 1234".
134+
timestamp: Optional. Timestamp of the order in seconds.
135+
address: Optional. The shipping address of the order.
136+
summary: The payment summary.
137+
adjustments: Optional. An array of payment objects that describe payment adjustments, such as discounts.
138+
quick_replies: An array of objects the describe the quick reply buttons to send. A maximum of 11 quick replies are supported.
139+
140+
"""
141+
142+
receipt_template_payload = ReceiptTemplatePayload(
143+
dict(
144+
template_type="receipt",
145+
recipient_name=recipient_name,
146+
elements=elements,
147+
order_number=order_number,
148+
currency=currency,
149+
payment_method=payment_method,
150+
timestamp=timestamp,
151+
address=address,
152+
summary=summary,
153+
adjustments=adjustments,
154+
)
155+
)
156+
attachment = Attachment(dict(type="template", payload=receipt_template_payload))
157+
message = Message({"quick_replies": quick_replies, "attachment": attachment})
158+
159+
response = self._post(message, recipient_id, user_ref, phone_number)
160+
return response
161+
107162
def send_quick_replies(
108-
self,
109-
recipient_id=None,
110-
user_ref=None,
111-
phone_number=None,
112-
text=None,
113-
quick_replies=None,
163+
self,
164+
recipient_id=None,
165+
user_ref=None,
166+
phone_number=None,
167+
text=None,
168+
quick_replies=None,
114169
):
115170
"""Sends quick replies to the recipient.
116171
@@ -147,12 +202,12 @@ def send_text(self, recipient_id=None, user_ref=None, phone_number=None, text=No
147202
return response
148203

149204
def send_image(
150-
self,
151-
recipient_id=None,
152-
user_ref=None,
153-
phone_number=None,
154-
url=None,
155-
quick_replies=None,
205+
self,
206+
recipient_id=None,
207+
user_ref=None,
208+
phone_number=None,
209+
url=None,
210+
quick_replies=None,
156211
):
157212
"""Sends an image to the recipient.
158213
@@ -171,12 +226,12 @@ def send_image(
171226
return response
172227

173228
def send_audio(
174-
self,
175-
recipient_id=None,
176-
user_ref=None,
177-
phone_number=None,
178-
url=None,
179-
quick_replies=None,
229+
self,
230+
recipient_id=None,
231+
user_ref=None,
232+
phone_number=None,
233+
url=None,
234+
quick_replies=None,
180235
):
181236
"""Sends an audio to the recipient.
182237
@@ -195,12 +250,12 @@ def send_audio(
195250
return response
196251

197252
def send_file(
198-
self,
199-
recipient_id=None,
200-
user_ref=None,
201-
phone_number=None,
202-
url=None,
203-
quick_replies=None,
253+
self,
254+
recipient_id=None,
255+
user_ref=None,
256+
phone_number=None,
257+
url=None,
258+
quick_replies=None,
204259
):
205260
"""Sends a file to the recipient.
206261
@@ -241,8 +296,8 @@ def _post(self, message, recipient_id=None, user_ref=None, phone_number=None):
241296
response = requests.post(API_URL, params=params, json=request.to_primitive())
242297
json_response = response.json()
243298
if (
244-
response.status_code == 400
245-
and json_response.get("error", {}).get("type", "") == "OAuthException"
299+
response.status_code == 400
300+
and json_response.get("error", {}).get("type", "") == "OAuthException"
246301
):
247302
raise OAuthException(json_response.get("error").get("message", ""))
248303
return response

0 commit comments

Comments
 (0)