Skip to content

Commit

Permalink
Add functions to send rich media
Browse files Browse the repository at this point in the history
  • Loading branch information
Pashutan Modaresi committed Feb 18, 2019
1 parent a5e1453 commit 8fff5a2
Show file tree
Hide file tree
Showing 15 changed files with 308 additions and 313 deletions.
41 changes: 26 additions & 15 deletions docs/autogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,18 @@
fbotics.models.payloads.generic_template.GenericDefaultAction,
],
},
{"page": "client/client.md", "methods": [Client.send_button_template]},
{
"page": "client/client.md",
"methods": [
Client.send_button_template,
Client.send_text,
Client.send_audio,
Client.send_image,
Client.send_file,
Client.send_generic_template,
Client.send_quick_replies,
],
},
{
"page": "quick_replies/quick_replies.md",
"classes": [fbotics.models.quick_reply.QuickReply],
Expand All @@ -51,7 +62,7 @@ def get_class_attr(Cls) -> []:
a
for a, v in Cls.__dict__.items()
if not re.match("<function.*?>", str(v))
and not (a.startswith("__") and a.endswith("__"))
and not (a.startswith("__") and a.endswith("__"))
]


Expand All @@ -76,7 +87,7 @@ def get_function_signature(function, method=True):
else:
args = signature.args
if defaults:
kwargs = zip(args[-len(defaults):], defaults)
kwargs = zip(args[-len(defaults) :], defaults)
args = args[: -len(defaults)]
else:
kwargs = []
Expand Down Expand Up @@ -139,8 +150,8 @@ def count_leading_spaces(s):
def process_list_block(docstring, starting_point, section_end, leading_spaces, marker):
ending_point = docstring.find("\n\n", starting_point)
block = docstring[
starting_point: (None if ending_point == -1 else ending_point - 1)
]
starting_point : (None if ending_point == -1 else ending_point - 1)
]
# Place marker for later reinjection.
docstring_slice = docstring[starting_point:section_end].replace(block, marker)
docstring = docstring[:starting_point] + docstring_slice + docstring[section_end:]
Expand Down Expand Up @@ -184,7 +195,7 @@ def process_docstring(docstring):
if "```" in docstring:
tmp = docstring[:]
while "```" in tmp:
tmp = tmp[tmp.find("```"):]
tmp = tmp[tmp.find("```") :]
index = tmp[3:].find("```") + 6
snippet = tmp[:index]
# Place marker in docstring for later reinjection.
Expand All @@ -209,9 +220,9 @@ def process_docstring(docstring):
leading_spaces = spaces
if leading_spaces:
snippet_lines = (
[snippet_lines[0]]
+ [line[leading_spaces:] for line in snippet_lines[1:-1]]
+ [snippet_lines[-1]]
[snippet_lines[0]]
+ [line[leading_spaces:] for line in snippet_lines[1:-1]]
+ [snippet_lines[-1]]
)
snippet = "\n".join(snippet_lines)
code_blocks.append(snippet)
Expand Down Expand Up @@ -351,10 +362,10 @@ def read_page_data(page_data, type):
continue
module_member = getattr(module, name)
if (
inspect.isclass(module_member)
and type == "classes"
or inspect.isfunction(module_member)
and type == "functions"
inspect.isclass(module_member)
and type == "classes"
or inspect.isfunction(module_member)
and type == "functions"
):
instance = module_member
if module.__name__ in instance.__module__:
Expand All @@ -368,7 +379,7 @@ def read_page_data(page_data, type):
if __name__ == "__main__":
readme = read_file("../README.md")
index = read_file("templates/index.md")
index = index.replace("{{autogenerated}}", readme[readme.find("##"):])
index = index.replace("{{autogenerated}}", readme[readme.find("##") :])
with open("sources/index.md", "w") as f:
f.write(index)

Expand Down Expand Up @@ -428,7 +439,7 @@ def read_page_data(page_data, type):
if os.path.exists(path):
template = read_file(path)
assert "{{autogenerated}}" in template, (
"Template found for " + path + " but missing {{autogenerated}}" " tag."
"Template found for " + path + " but missing {{autogenerated}}" " tag."
)
mkdown = template.replace("{{autogenerated}}", mkdown)
print("...inserting autogenerated content into template:", path)
Expand Down
1 change: 1 addition & 0 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ nav:
- Home: index.md
- Client: client/client.md
- Text Message: text/example.md
- Rich Media: rich_media/example.md
- Quick Replies:
- Quick Reply: quick_replies/quick_replies.md
- Example: quick_replies/example.md
Expand Down
39 changes: 39 additions & 0 deletions docs/templates/rich_media/example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
This is an example to send rich media using FBotics:

```python
from fbotics.client import Client
from fbotics.models.quick_reply import QuickReply

client = Client(page_access_token=PAGE_ACCESS_TOKEN)


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_image(
recipient_id=RECIPIENT_ID, url="http://i63.tinypic.com/2zfprph.png", quick_replies=[qr1, qr2]
)

response = client.send_audio(
recipient_id=recipient_id, url="http://www.pacdv.com/sounds/voices/mmm-1.wav", quick_replies=[qr1, qr2]
)

response = client.send_file(
recipient_id=recipient_id, url="http://www.xmlpdf.com/manualfiles/hello-world.pdf", quick_replies=[qr1, qr2]
)

```
12 changes: 6 additions & 6 deletions fbotics/_version.py
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit 8fff5a2

Please sign in to comment.