Skip to content

Commit

Permalink
Add functionality to send list templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Pashutan Modaresi committed Mar 18, 2019
1 parent 851ab8b commit ae94a28
Show file tree
Hide file tree
Showing 17 changed files with 334 additions and 114 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -27,6 +27,7 @@ FBotics currently supports sending following message types:
- Templates
- Button Template
- Generic Template
- List Template

## Installation

Expand Down Expand Up @@ -76,4 +77,4 @@ To create coverage report:

```sh
make coverage
```
```
8 changes: 6 additions & 2 deletions docs/autogen.py
Expand Up @@ -18,12 +18,16 @@
EXCLUDE = {}

PAGES = [
{
"page": "list_template/list_template.md",
"classes": [
fbotics.models.payloads.list_template.ListTemplatePayload,
],
},
{
"page": "generic_template/generic_template.md",
"classes": [
fbotics.models.payloads.generic_template.GenericTemplatePayload,
fbotics.models.payloads.generic_template.GenericElement,
fbotics.models.payloads.generic_template.GenericDefaultAction,
],
},
{
Expand Down
5 changes: 4 additions & 1 deletion docs/mkdocs.yml
Expand Up @@ -21,4 +21,7 @@ nav:
- Button Template: button_template/examples.md
- Generic Template:
- Template Payload: generic_template/generic_template.md
- Example: generic_template/example.md
- Example: generic_template/example.md
- List Template:
- Template Payload: list_template/list_template.md
- Example: list_template/example.md
22 changes: 10 additions & 12 deletions docs/templates/generic_template/example.md
Expand Up @@ -3,9 +3,7 @@ This is an example to send Generic Templates using FBotics:
```python
from fbotics.client import Client
from fbotics.models.buttons import WebUrlButton
from fbotics.models.payloads.generic_template import (
GenericElement,
)
from fbotics.models.payloads.element import Element
from fbotics.models.quick_reply import QuickReply

client = Client(page_access_token=PAGE_ACCESS_TOKEN)
Expand All @@ -17,14 +15,14 @@ buttons = [
)
]

ge = GenericElement(
dict(
title="Title1",
image_url="http://i67.tinypic.com/262vb5l.jpg",
subtitle="Subtitle1",
buttons=buttons,
)
)
ge = Element(
dict(
title="Title1",
image_url="http://i67.tinypic.com/262vb5l.jpg",
subtitle="Subtitle1",
buttons=buttons,
)
)

qr1 = QuickReply(
dict(
Expand Down Expand Up @@ -53,4 +51,4 @@ response = client.send_generic_template(

<p float="center">
<img src="https://scontent-frx5-1.xx.fbcdn.net/v/t39.2365-6/22880422_1740199342956641_1916832982102966272_n.png?_nc_cat=107&_nc_ht=scontent-frx5-1.xx&oh=310487994971cafb35b23618567ef34c&oe=5CF2013C" width="40%" />
</p>
</p>
65 changes: 65 additions & 0 deletions docs/templates/list_template/example.md
@@ -0,0 +1,65 @@
This is an example to send List Templates using FBotics:

```python
from fbotics.client import Client
from fbotics.models.buttons import WebUrlButton
from fbotics.models.payloads.element Element
from fbotics.models.quick_reply import QuickReply

client = Client(page_access_token=PAGE_ACCESS_TOKEN)


buttons = [
WebUrlButton(
dict(type="web_url", url="http://www.google.com", title="Web URL Button")
)
]

e1 = Element(
dict(
title="Title1",
image_url="http://i67.tinypic.com/262vb5l.jpg",
subtitle="Subtitle1",
buttons=buttons,
)
)

e2 = Element(
dict(
title="Title1",
image_url="http://i67.tinypic.com/262vb5l.jpg",
subtitle="Subtitle1",
buttons=buttons,
)
)

qr1 = QuickReply(
dict(
content_type="text",
title="Yes",
payload="payload1",
image_url="http://i64.tinypic.com/1hothh.png",
)
)

qr2 = QuickReply(
dict(
content_type="text",
title="No",
payload="payload2",
image_url="http://i63.tinypic.com/2pqpbth.png",
)
)

response = client.send_generic_template(
recipient_id=RECIPIENT_ID,
quick_replies=[qr1, qr2],
elements=[e1, e2],
buttons=buttons
)
```

<p float="center">
<img src="https://scontent-frx5-1.xx.fbcdn.net/v/t39.2365-6/28126658_223368134877650_2823033985926430720_n.png?_nc_cat=104&_nc_ht=scontent-frx5-1.xx&oh=19307a15df1aea5808ba059de620dd41&oe=5D196422" width="40%" />
<img src="https://scontent-frx5-1.xx.fbcdn.net/v/t39.2365-6/21201919_1215144078631552_6152307842817720320_n.png?_nc_cat=104&_nc_ht=scontent-frx5-1.xx&oh=ec303f8709bf61895a3ac75ff338025e&oe=5D22406A" width="40%" />
</p>
3 changes: 3 additions & 0 deletions docs/templates/list_template/list_template.md
@@ -0,0 +1,3 @@
The list template is a list of 2-4 structured items with an optional global button rendered at the bottom. Each item may contain a thumbnail image, title, subtitle, and one button. You may also specify a default_action object that sets a URL that will be opened in the Messenger webview when the item is tapped.

{{autogenerated}}
12 changes: 6 additions & 6 deletions fbotics/_version.py
Expand Up @@ -120,7 +120,7 @@ def versions_from_parentdir(parentdir_prefix, root, verbose):
dirname = os.path.basename(root)
if dirname.startswith(parentdir_prefix):
return {
"version": dirname[len(parentdir_prefix) :],
"version": dirname[len(parentdir_prefix):],
"full-revisionid": None,
"dirty": False,
"error": None,
Expand All @@ -132,8 +132,8 @@ def versions_from_parentdir(parentdir_prefix, root, verbose):

if verbose:
print(
"Tried directories %s but none started with prefix %s"
% (str(rootdirs), parentdir_prefix)
"Tried directories %s but none started with prefix %s"
% (str(rootdirs), parentdir_prefix)
)
raise NotThisMethod("rootdir doesn't start with parentdir_prefix")

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

# distance: number of commits since tag
pieces["distance"] = int(mo.group(2))
Expand Down
111 changes: 71 additions & 40 deletions fbotics/client/__init__.py
Expand Up @@ -6,6 +6,7 @@
from fbotics.models.message import Message
from fbotics.models.payloads.button_template import ButtonTemplatePayload
from fbotics.models.payloads.generic_template import GenericTemplatePayload
from fbotics.models.payloads.list_template import ListTemplatePayload
from fbotics.models.payloads.rich_media import RichMediaPayload
from fbotics.models.recipient import Recipient

Expand All @@ -17,13 +18,13 @@ def __init__(self, page_access_token=None):
self.page_access_token = page_access_token

def send_button_template(
self,
recipient_id=None,
user_ref=None,
phone_number=None,
text=None,
quick_replies=None,
buttons=None,
self,
recipient_id=None,
user_ref=None,
phone_number=None,
text=None,
quick_replies=None,
buttons=None,
):
"""Sends a button template to the recipient.
Expand All @@ -46,12 +47,12 @@ def send_button_template(
return response

def send_generic_template(
self,
recipient_id=None,
user_ref=None,
phone_number=None,
elements=None,
quick_replies=None,
self,
recipient_id=None,
user_ref=None,
phone_number=None,
elements=None,
quick_replies=None,
):
"""Sends a generic template to the recipient.
Expand All @@ -60,7 +61,7 @@ def send_generic_template(
user_ref: optional. user_ref from the checkbox plugin
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.
elements: An array of element objects that describe instances of the generic template to be sent. Specifying multiple elements will send a horizontally scrollable carousel of templates. A maximum of 10 elements is supported.
buttons: Set of 1-3 buttons that appear as call-to-actions.
quick_replies: An array of objects the describe the quick reply buttons to send. A maximum of 11 quick replies are supported.
"""

Expand All @@ -73,13 +74,43 @@ def send_generic_template(
response = self._post(message, recipient_id, user_ref, phone_number)
return response

def send_list_template(
self,
recipient_id=None,
user_ref=None,
phone_number=None,
elements=None,
buttons=None,
quick_replies=None
):
"""Sends a list template to the recipient.
# Arguments
recipient_id: page specific id of the recipient
user_ref: optional. user_ref from the checkbox plugin
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.
elements: Array of objects that describe items in the list. Minimum of 2 elements required. Maximum of 4 elements is supported.
buttons: Button to display at the bottom of the list. Maximum of 1 button is supported.
quick_replies: An array of objects the describe the quick reply buttons to send. A maximum of 11 quick replies are supported.
"""

list_template_payload = ListTemplatePayload(
dict(template_type="list", elements=elements, buttons=buttons)
)
attachment = Attachment(dict(type="template", payload=list_template_payload))
message = Message({"quick_replies": quick_replies, "attachment": attachment})

response = self._post(message, recipient_id, user_ref, phone_number)
return response

def send_quick_replies(
self,
recipient_id=None,
user_ref=None,
phone_number=None,
text=None,
quick_replies=None,
self,
recipient_id=None,
user_ref=None,
phone_number=None,
text=None,
quick_replies=None,
):
"""Sends quick replies to the recipient.
Expand Down Expand Up @@ -116,12 +147,12 @@ def send_text(self, recipient_id=None, user_ref=None, phone_number=None, text=No
return response

def send_image(
self,
recipient_id=None,
user_ref=None,
phone_number=None,
url=None,
quick_replies=None,
self,
recipient_id=None,
user_ref=None,
phone_number=None,
url=None,
quick_replies=None,
):
"""Sends an image to the recipient.
Expand All @@ -140,12 +171,12 @@ def send_image(
return response

def send_audio(
self,
recipient_id=None,
user_ref=None,
phone_number=None,
url=None,
quick_replies=None,
self,
recipient_id=None,
user_ref=None,
phone_number=None,
url=None,
quick_replies=None,
):
"""Sends an audio to the recipient.
Expand All @@ -164,12 +195,12 @@ def send_audio(
return response

def send_file(
self,
recipient_id=None,
user_ref=None,
phone_number=None,
url=None,
quick_replies=None,
self,
recipient_id=None,
user_ref=None,
phone_number=None,
url=None,
quick_replies=None,
):
"""Sends a file to the recipient.
Expand Down Expand Up @@ -210,8 +241,8 @@ def _post(self, message, recipient_id=None, user_ref=None, phone_number=None):
response = requests.post(API_URL, params=params, json=request.to_primitive())
json_response = response.json()
if (
response.status_code == 400
and json_response.get("error", {}).get("type", "") == "OAuthException"
response.status_code == 400
and json_response.get("error", {}).get("type", "") == "OAuthException"
):
raise OAuthException(json_response.get("error").get("message", ""))
return response

0 comments on commit ae94a28

Please sign in to comment.