Skip to content

Commit

Permalink
Major refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Pashutan Modaresi committed Jan 29, 2019
1 parent ac8aaa2 commit 3a718bd
Show file tree
Hide file tree
Showing 22 changed files with 169 additions and 55 deletions.
98 changes: 70 additions & 28 deletions docs/autogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,88 @@
EXCLUDE = {
}

# For each class to document, it is possible to:
# 1) Document only the class: [classA, classB, ...]
# 2) Document all its methods: [classA, (classB, "*")]
# 3) Choose which methods to document (methods listed as strings):
# [classA, (classB, ["method1", "method2", ...]), ...]
# 4) Choose which methods to document (methods listed as qualified names):
# [classA, (classB, [module.classB.method1, module.classB.method2, ...]), ...]

PAGES = [
{
'page': 'fbotics.md',
'page': '_templates/generic_template/generic_template.md',
'classes': [
fbotics.models.payloads.generic_template.GenericTemplatePayload,
fbotics.models.payloads.generic_template.GenericElement,
fbotics.models.payloads.generic_template.GenericDefaultAction,
],
},
{
'page': '_templates/button_template/button_template.md',
'classes': [
fbotics.models.payloads.button_template.ButtonTemplatePayload,
],
},
{
'page': 'send/client.md',
'methods': [
fbotics.Client.send_message,
fbotics.Client.retrieve_supported_tags,
],
},
{
'page': 'buttons.md',
'page': 'quick_replies/quick_replies.md',
'classes': [
fbotics.models.quick_reply.QuickReply,
],
},
{
'page': 'send/request.md',
'classes': [
fbotics.models.request.Request,
],
},
{
'page': 'send/recipient.md',
'classes': [
fbotics.models.recipient.Recipient,
],
},
{
'page': 'buttons/buttons.md',
'classes': [
fbotics.models.buttons.WebUrlButton,
fbotics.models.buttons.CallButton,
fbotics.models.buttons.PostbackButton
],
},
{
'page': 'message.md',
'page': 'send/message.md',
'classes': [
fbotics.models.message.Message,
],
},
{
'page': 'templates.md',
'page': 'send/attachment.md',
'classes': [
fbotics.models.payloads.button_template.ButtonTemplatePayload,
fbotics.models.payloads.generic_template.GenericTemplatePayload,
fbotics.models.payloads.generic_template.GenericElement,
fbotics.models.payloads.generic_template.GenericDefaultAction,
fbotics.models.payloads.rich_media.RichMediaPayload
fbotics.models.attachment.Attachment,
],
}
},
]

ROOT = 'http://fbotics.io/'

def get_class_attr(Cls) -> []:
import re
return [a for a, v in Cls.__dict__.items()
if not re.match('<function.*?>', str(v))
and not (a.startswith('__') and a.endswith('__'))]

def get_class_attr_val(cls):
attr = get_class_attr(type(cls))
attr_dict = {}
for a in attr:
attr_dict[a] = getattr(cls, a)
return attr_dict


def get_function_signature(function, method=True):
wrapped = getattr(function, '_original_function', None)

if wrapped is None:
signature = inspect.getargspec(function)
else:
Expand Down Expand Up @@ -93,17 +129,23 @@ def get_function_signature(function, method=True):


def get_class_signature(cls):
try:
class_signature = get_function_signature(cls.__init__)
class_signature = class_signature.replace('__init__', cls.__name__)
except (TypeError, AttributeError):
# in case the class inherits from object and does not
# define __init__
class_signature = "{clean_module_name}.{cls_name}()".format(
clean_module_name=clean_module_name(cls.__module__),
cls_name=cls.__name__
)
return post_process_signature(class_signature)
signature = '%s.%s(' % (cls.__module__, cls.__name__)
class_attr_value_dict = get_class_attr_val(cls())
class_attr_value_dict.pop("_schema")
for k, v in class_attr_value_dict.items():
signature += str(k) + "=" + str(v) + ', '
signature = signature[:-2] + ')'
#try:
# class_signature = get_function_signature(cls.__init__)
# class_signature = class_signature.replace('__init__', cls.__name__)
#except (TypeError, AttributeError):
# # in case the class inherits from object and does not
# # define __init__
# class_signature = "{clean_module_name}.{cls_name}()".format(
# clean_module_name=clean_module_name(cls.__module__),
# cls_name=cls.__name__
# )
return post_process_signature(signature)


def post_process_signature(signature):
Expand Down
21 changes: 17 additions & 4 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
site_name: FBotics Documentation
theme: readthedocs
theme: material
site_description: 'Documentation for FBotics, the Python Client for Facebook Send API'
docs_dir: sources
site_url: https://pasmod.github.io/fbotics/


dev_addr: '0.0.0.0:8000'

nav:
- Home: index.md
- Send API: fbotics.md
- Send API:
- Client: send/client.md
- Request: send/request.md
- Recipient: send/recipient.md
- Message: send/message.md
- Attachment: send/attachment.md
- Quick Replies:
- Quick Reply: quick_replies/quick_replies.md
- Example: quick_replies/example.md
- Buttons: buttons.md
- Button Template:
- Template Payload: _templates/button_template/button_template.md
- Example: _templates/button_template/example.md
- Generic Template:
- Template Payload: _templates/generic_template/generic_template.md
- Example: _templates/generic_template/example.md
- Message: message.md
- Template Payloads: templates.md
- Template Payloads: button_template.md
3 changes: 3 additions & 0 deletions docs/templates/_templates/button_template/button_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The button template allows you to send a structured message that includes text and buttons.

{{autogenerated}}
2 changes: 2 additions & 0 deletions docs/templates/_templates/button_template/example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

{{autogenerated}}
2 changes: 2 additions & 0 deletions docs/templates/_templates/generic_template/example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

{{autogenerated}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The generic template allows you to send a structured message that includes an image, text and buttons. A generic template with multiple templates described in the elements array will send a horizontally scrollable carousel of items, each composed of an image, text and buttons.

{{autogenerated}}
File renamed without changes.
15 changes: 0 additions & 15 deletions docs/templates/fbotics.md

This file was deleted.

2 changes: 2 additions & 0 deletions docs/templates/quick_replies/example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{autogenerated}}

4 changes: 4 additions & 0 deletions docs/templates/quick_replies/quick_replies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Quick Replies allow you to get message recipient input by sending buttons in a message. When a quick reply is tapped, the value of the button is sent in the conversation, and the Messenger Platform sends a messages event to you webhook.

{{autogenerated}}

1 change: 1 addition & 0 deletions docs/templates/send/attachment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{autogenerated}}
22 changes: 22 additions & 0 deletions docs/templates/send/client.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## Client
In order to create an instance of the Client, a page access token is needed.

```python
from fbotics import Client

client = Client(page_access_token="PAGE_ACCESS_TOKEN")
```

The client can be used to send requests (messages) to the recipients.

```python
from fbotics import Client
from fbotics.models.request import Request

client = Client(page_access_token="PAGE_ACCESS_TOKEN")
request = Request(...)
client.send(request)
```

{{autogenerated}}

1 change: 1 addition & 0 deletions docs/templates/send/message.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{autogenerated}}
1 change: 1 addition & 0 deletions docs/templates/send/recipient.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{autogenerated}}
1 change: 1 addition & 0 deletions docs/templates/send/request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{autogenerated}}
1 change: 1 addition & 0 deletions fbotics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Client(object):
def __init__(self, page_access_token=None):
self.page_access_token = page_access_token


def send_message(
self,
recipient_id=None,
Expand Down
7 changes: 7 additions & 0 deletions fbotics/models/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ def payload_claim_function(field, data):


class Attachment(Model):
"""The following can be included in the attachment object: Rich media messages including images, audios, videos, or files and Templates including generic template, button template, receipt template, or list template.
# Arguments
type: Type of attachment, may be image, audio, video, file or template. For assets, max file size is 25MB.
payload: Payload of attachment
"""
type = StringType(
required=True,
choices=[
Expand Down
12 changes: 4 additions & 8 deletions fbotics/models/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@ class Message(Model):
attachment: attachment object. Previews the URL. Used to send messages with media or Structured Messages. text or attachment must be set.
quick_replies: Optional. Array of quick_reply to be sent with messages
metadata: Optional. Custom string that is delivered as a message echo. 1000 character limit.
# Examples
```python
from fbotics.models.message import Message
Message(dict(text="hello", quick_replies=[qr1, qr2]))
```
"""
text = StringType(required=False, max_length=2000)
attachment = ModelType(Attachment, required=False)
text = StringType(required=False, max_length=2000, serialize_when_none=False)
attachment = ModelType(Attachment, required=False, serialize_when_none=False)
quick_replies = ListType(ModelType(QuickReply), required=False)
metadata = StringType(required=False, max_length=1000)

def validate_text(self, data, value):
if data["text"] and not data["attachment"]:
if data["text"] and data["attachment"]:
raise ValidationError(
"Field text and attachment can'T be set at the same time.")
return value
9 changes: 9 additions & 0 deletions fbotics/models/quick_reply.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ def is_blank(string):


class QuickReply(Model):
"""Represent a message object in a request sent to Facebook.
# Arguments
content_type: Must be one of the following text: Sends a text button, location: Sends a button to collect the recipient's location, user_phone_number: Sends a button allowing recipient to send the phone number associated with their account.,user_email: Sends a button allowing recipient to send the email associated with their account.
title: Required if content_type is 'text'. The text to display on the quick reply button. 20 character limit.
payload: Required if content_type is 'text'. Custom data that will be sent back to you via the messaging_postbacks webhook event. 1000 character limit. May be set to an empty string if image_url is set.
image_url: Optional. URL of image to display on the quick reply button for text quick replies. Image should be a minimum of 24px x 24px. Larger images will be automatically cropped and resized. Required if title is an empty string.
"""
content_type = StringType(
required=True,
choices=[
Expand Down
9 changes: 9 additions & 0 deletions fbotics/models/recipient.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@


class Recipient(Model):
"""Model for the recipient of a message. All requests must include one of id, phone_number, or user_ref.
# Arguments
id: Either PSID, phone_number, or user_ref of the message recipient.
phone_number: Optional. Phone number of the recipient with the format +1(212)555-2368.
user_ref: Optional. user_ref from the checkbox plugin.
name: Optional. Used only if phone_number is set. Specifies the person's name in the format: {"first_name":"John", "last_name":"Doe"}
"""
id = StringType(required=False, serialize_when_none=False)
phone_number = StringType(required=False, serialize_when_none=False)
user_ref = StringType(required=False, serialize_when_none=False)
Expand Down
9 changes: 9 additions & 0 deletions fbotics/models/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@


class Request(Model):
"""Model for a request to be send by the client.
# Arguments
messaging_type: The messaging type of the message being sent.
recipient: recipient object.
message: message object. Cannot be sent with sender_action.
tag: Optional. The message tag string.
"""
messaging_type = StringType(
required=True,
choices=[
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ codecov==2.0.15

# Documentation
mkdocs==1.0.4
mkdocs-material

# Packaging
twine==1.12.1
Expand Down

0 comments on commit 3a718bd

Please sign in to comment.