Skip to content

Commit

Permalink
Merge branch 'master' into decorator-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
narayana-plivo authored May 7, 2024
2 parents 07ee454 + 234904a commit fd4ed58
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
**Minor enhancements**
- updated decorator version

## [v4.52.0](https://github.com/plivo/plivo-python/tree/v4.52.0) (2024-05-07)
**Feature - Adding support for interactive whatsapp messages**
- Added new param `interactive` to [send message API](https://www.plivo.com/docs/sms/api/message#send-a-message) to support interactive `whatsapp` messages

## [4.51.0](https://github.com/plivo/plivo-python/tree/v4.51.0) (2024-05-02)
**Feature - Added SubAccount and GeoMatch for Create Masking Session API of Number Masking.**
- Added sub_account and geo_match support in MaskingSession APIs
Expand Down
4 changes: 4 additions & 0 deletions plivo/resources/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Messages(PlivoResourceInterface):
media_ids=[optional(of_type_exact(list))],
message_expiry=[optional(of_type(*six.integer_types))],
template=[optional(is_template())],
interactive=[optional(is_interactive())],
dlt_entity_id=[optional(of_type(six.text_type))],
dlt_template_id=[optional(of_type(six.text_type))],
dlt_template_category=[optional(of_type(six.text_type))]
Expand All @@ -60,6 +61,7 @@ def create(self,
media_ids=None,
message_expiry=None,
template=None,
interactive=None,
dlt_entity_id=None,
dlt_template_id=None,
dlt_template_category=None):
Expand All @@ -84,6 +86,8 @@ def create(self,
)
if template is not None:
template = template.__dict__
if interactive is not None:
interactive = interactive.__dict__
return self.client.request('POST', ('Message', ),
to_param_dict(self.create, locals()))

Expand Down
82 changes: 82 additions & 0 deletions plivo/utils/interactive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from plivo.utils.validators import validate_args, optional, of_type_exact, validate_list_items, validate_dict_items

class Header:
@validate_args(
type=[optional(of_type_exact(str, type(None)))],
text=[optional(of_type_exact(str, type(None)))],
media=[optional(of_type_exact(str, type(None)))]
)
def __init__(self, type=None, text=None, media=None):
self.type = type
self.text = text
self.media = media

class Body:
@validate_args(
text=[optional(of_type_exact(str, type(None)))]
)
def __init__(self, text=None):
self.text = text

class Footer:
@validate_args(
text=[optional(of_type_exact(str, type(None)))]
)
def __init__(self, text=None):
self.text = text

class Row:
@validate_args(
id=[optional(of_type_exact(str, type(None)))],
title=[optional(of_type_exact(str, type(None)))],
description=[optional(of_type_exact(str, type(None)))]
)
def __init__(self, id=None, title=None, description=None):
self.id = id
self.title = title
self.description = description

class Section:
@validate_args(
title=[optional(of_type_exact(str, type(None)))],
rows=[optional(validate_list_items(Row))]
)
def __init__(self, title=None, rows=None):
self.title = title
self.rows = rows if rows is not None else []

class Btn:
@validate_args(
id=[optional(of_type_exact(str, type(None)))],
title=[optional(of_type_exact(str, type(None)))],
cta_url=[optional(of_type_exact(str, type(None)))]
)
def __init__(self, id=None, title=None, cta_url=None):
self.id = id
self.title = title
self.cta_url = cta_url

class Action:
@validate_args(
buttons=[optional(validate_list_items(Btn))],
sections=[optional(validate_list_items(Section))]
)
def __init__(self, buttons=None, sections=None):
self.buttons = buttons if buttons is not None else []
self.sections = sections if sections is not None else []

class Interactive:
@validate_args(
type=[optional(of_type_exact(str, type(None)))],
header=[optional(validate_dict_items(Header))],
body=[optional(validate_dict_items(Body))],
footer=[optional(validate_dict_items(Footer))],
action=[optional(validate_dict_items(Action))]
)
def __init__(self, type=None, header=None, body=None, footer=None, action=None):
self.type = type
# Assign directly the validated dictionary or default to None if not provided
self.header = header
self.body = body
self.footer = footer
self.action = action
19 changes: 19 additions & 0 deletions plivo/utils/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,24 @@ def f(arg_name, value):

return f

def validate_dict_items(instance_type):
def validator(arg_name, value):
if not isinstance(value, dict):
return None, ["{} must be a dictionary".format(arg_name)]

errors = []
try:
instance_type(**value)
except TypeError as e:
errors.append(str(e))

if errors:
return None, errors
return value, []

return validator


is_valid_date = functools.partial(of_type, six.text_type)
is_phonenumber = functools.partial(of_type, six.text_type)
is_subaccount_id = functools.partial(all_of, of_type(six.text_type),
Expand All @@ -262,3 +280,4 @@ def f(arg_name, value):
regex(r'^\d{4}-\d{2}-\d{2} \d{2}:\d{2}(:\d{2}(\.\d{1,6})?)?$'))

is_template = functools.partial(of_type_exact,'plivo.utils.template.Template')
is_interactive = functools.partial(of_type_exact,'plivo.utils.interactive.Interactive')

0 comments on commit fd4ed58

Please sign in to comment.