Skip to content

Commit

Permalink
Parametro Check y Mix de Create() para Template y Message
Browse files Browse the repository at this point in the history
  • Loading branch information
rogelioLpz committed Aug 28, 2019
1 parent 9f9651c commit b6465a1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 45 deletions.
42 changes: 42 additions & 0 deletions botmaker/resources/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
from botmaker.exc import InvalidPhoneNumber
from botmaker.helpers import sanitize_phone_number


class Resource:
_client = None
_endpoint = None

@classmethod
def commonCreate(
cls,
from_: str,
to: str,
chat_platform: str,
check_phone: bool,
**data):
from_ = sanitize_phone_number(from_)
if check_phone:
if chat_platform == 'whatsapp' and check_phone:
checked = cls._client.check_whatsapp_contact(from_, to)
if not checked:
raise InvalidPhoneNumber(
f"'{to} is not from valid WhatsApp contact")
else:
to = checked
else:
to = sanitize_phone_number(to)
else:
to = sanitize_phone_number(to)
to = sanitize_phone_number(to)
body = dict(
chatPlatform=chat_platform,
chatChannelNumber=from_,
platformContactId=to,
)

if cls._endpoint == '/message/v3':
body['messageText'] = data['message_text']
resp = cls._client.post(cls._endpoint, body)
return cls(resp['id'], from_, to, data['message_text'])
if cls._endpoint == '/intent/v2':
body['ruleNameOrId'] = data['template']
body['params'] = data['params']
resp = cls._client.post(cls._endpoint, body)
return cls(resp['id'], from_, to, data['template'], data['params'])
29 changes: 7 additions & 22 deletions botmaker/resources/messages.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
from dataclasses import dataclass

from botmaker.exc import InvalidPhoneNumber
from botmaker.helpers import sanitize_phone_number

from .base import Resource


Expand All @@ -21,27 +17,16 @@ def create(
from_: str,
to: str,
message_text: str,
check_phone: bool = False,
chat_platform: str = 'whatsapp'
):
"""
Based on
https://botmakeradmin.github.io/docs/es/#/messages-api?id=enviando-mensajes-a-los-usuarios
"""
from_ = sanitize_phone_number(from_)
if chat_platform == 'whatsapp':
checked = cls._client.check_whatsapp_contact(from_, to)
if not checked:
raise InvalidPhoneNumber(
f"'{to} is not from valid WhatsApp contact")
else:
to = checked
else:
to = sanitize_phone_number(to)
data = dict(
chatPlatform=chat_platform,
chatChannelNumber=from_,
platformContactId=to,
messageText=message_text
)
resp = cls._client.post(cls._endpoint, data)
return cls(resp['id'], from_, to, message_text)
return cls.commonCreate(
from_,
to,
chat_platform,
check_phone,
message_text=message_text)
31 changes: 8 additions & 23 deletions botmaker/resources/template_messages.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
from dataclasses import dataclass, field

from botmaker.exc import InvalidPhoneNumber
from botmaker.helpers import sanitize_phone_number

from .base import Resource


Expand All @@ -23,28 +19,17 @@ def create(
to: str,
template: str,
chat_platform: str = 'whatsapp',
check_phone: bool = True,
**params
):
"""
Based on
https://botmakeradmin.github.io/docs/es/#/messages-api?id=templates-messages
"""
from_ = sanitize_phone_number(from_)
if chat_platform == 'whatsapp':
checked = cls._client.check_whatsapp_contact(from_, to)
if not checked:
raise InvalidPhoneNumber(
f"'{to} is not from valid WhatsApp contact")
else:
to = checked
else:
to = sanitize_phone_number(to)
data = dict(
chatPlatform=chat_platform,
chatChannelNumber=from_,
platformContactId=to,
ruleNameOrId=template,
params=params
)
resp = cls._client.post(cls._endpoint, data)
return cls(resp['id'], from_, to, template, params)
return cls.commonCreate(
from_,
to,
chat_platform,
check_phone,
template=template,
params=params)
1 change: 1 addition & 0 deletions tests/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ def test_invalid_whatsapp_number_message(client):
client.messages.create(
'5215500000000',
'123',
check_phone=True,
message_text='message test',
)

0 comments on commit b6465a1

Please sign in to comment.