Skip to content

Commit

Permalink
prepare for messages release
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkahan committed Apr 10, 2024
1 parent 847df22 commit c45389b
Show file tree
Hide file tree
Showing 27 changed files with 206 additions and 104 deletions.
2 changes: 0 additions & 2 deletions http_client/src/vonage_http_client/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,6 @@ def _parse_response(self, response: Response) -> Union[dict, None]:
)
self._last_response = response
if 200 <= response.status_code < 300:
if response.status_code == 204:
return None
try:
return response.json()
except JSONDecodeError:
Expand Down
67 changes: 67 additions & 0 deletions messages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,72 @@ This package contains the code to use [Vonage's Messages API](https://developer.

It is recommended to use this as part of the main `vonage` package. The examples below assume you've created an instance of the `vonage.Vonage` class called `vonage_client`.

### How to Construct a Message

In order to send a message, you must construct a message object of the correct type. These are all found under `vonage_messages.models`.

```python
from vonage_messages.models import Sms

message = Sms(
from_='Vonage APIs',
to='1234567890',
text='This is a test message sent from the Vonage Python SDK',
)
```

This message can now be sent with

```python
vonage_client.messages.send(message)
```

All possible message types from every message channel have their own message model. They are named following this rule: {Channel}{MessageType}, e.g. `Sms`, `MmsImage`, `MessengerAudio`, `WhatsappSticker`, `ViberVideo`, etc.

The different message models are listed at the bottom of the page.

Some message types have submodels with additional fields. In this case, import the submodels as well and use them to construct the overall options.

e.g.

```python
from vonage_messages import MessengerImage, MessengerOptions, MessengerResource

messenger = MessengerImage(
to='1234567890',
from_='1234567890',
image=MessengerResource(url='https://example.com/image.jpg'),
messenger=MessengerOptions(category='message_tag', tag='invalid_tag'),
)
```

### Send a message

To send a message, access the `Messages.send` method via the main Vonage object, passing in an instance of a subclass of `BaseMessage` like this:

```python
from vonage import Auth, Vonage
from vonage_messages.models import Sms

vonage_client = Vonage(Auth(application_id='my-application-id', private_key='my-private-key'))

message = Sms(
from_='Vonage APIs',
to='1234567890',
text='This is a test message sent from the Vonage Python SDK',
)

vonage_client.messages.send(message)
```

## Message Models

To send a message, instantiate a message model of the correct type as described above. This is a list of message models that can be used:

```
Sms
MmsImage, MmsVcard, MmsAudio, MmsVideo
WhatsappText, WhatsappImage, WhatsappAudio, WhatsappVideo, WhatsappFile, WhatsappTemplate, WhatsappSticker, WhatsappCustom
MessengerText, MessengerImage, MessengerAudio, MessengerVideo, MessengerFile
ViberText, ViberImage, ViberVideo, ViberFile
```
11 changes: 2 additions & 9 deletions messages/src/vonage_messages/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
from . import models
from .enums import ChannelType, EncodingType, MessageType, WebhookVersion
from .messages import Messages
from .responses import SendMessageResponse

__all__ = [
'models',
'Messages',
'ChannelType',
'MessageType',
'WebhookVersion',
'EncodingType',
]
__all__ = ['models', 'Messages', 'SendMessageResponse']
15 changes: 8 additions & 7 deletions messages/src/vonage_messages/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from vonage_http_client.http_client import HttpClient

from .models import BaseMessage
from .responses import MessageUuid
from .responses import SendMessageResponse


class Messages:
Expand All @@ -18,19 +18,20 @@ def __init__(self, http_client: HttpClient) -> None:
self._http_client = http_client

@validate_call
def send(self, message: BaseMessage) -> MessageUuid:
def send(self, message: BaseMessage) -> SendMessageResponse:
"""Send a message using Vonage's Messages API.
Args:
message (BaseMessage): The message to be sent.
message (BaseMessage): The message to be sent as a Pydantic model.
Use the provided models (in `vonage_messages.models`) to create messages and pass them in to this method.
Returns:
MessageUuid: The unique identifier of the sent message.
SendMessageResponse: Response model containing the unique identifier of the sent message.
Access the identifier with the `message_uuid` attribute.
"""
response = self._http_client.post(
self._http_client.api_host,
'/v1/messages',
message.model_dump(by_alias=True, exclude_none=True),
message.model_dump(by_alias=True, exclude_none=True) or message,
)

return MessageUuid(**response)
return SendMessageResponse(**response)
5 changes: 5 additions & 0 deletions messages/src/vonage_messages/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .base_message import BaseMessage
from .enums import ChannelType, EncodingType, MessageType, WebhookVersion
from .messenger import (
MessengerAudio,
MessengerFile,
Expand Down Expand Up @@ -46,6 +47,9 @@

__all__ = [
'BaseMessage',
'ChannelType',
'EncodingType',
'MessageType',
'MessengerAudio',
'MessengerFile',
'MessengerImage',
Expand All @@ -72,6 +76,7 @@
'ViberVideo',
'ViberVideoOptions',
'ViberVideoResource',
'WebhookVersion',
'WhatsappAudio',
'WhatsappAudioResource',
'WhatsappContext',
Expand Down
2 changes: 1 addition & 1 deletion messages/src/vonage_messages/models/base_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pydantic import BaseModel, Field
from vonage_utils.types.phone_number import PhoneNumber

from ..enums import WebhookVersion
from .enums import WebhookVersion


class BaseMessage(BaseModel):
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion messages/src/vonage_messages/models/messenger.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from pydantic import BaseModel, Field, model_validator

from ..enums import ChannelType, MessageType
from .base_message import BaseMessage
from .enums import ChannelType, MessageType


class MessengerResource(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion messages/src/vonage_messages/models/mms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from pydantic import BaseModel, Field
from vonage_utils.types.phone_number import PhoneNumber

from ..enums import ChannelType, MessageType
from .base_message import BaseMessage
from .enums import ChannelType, MessageType


class MmsResource(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion messages/src/vonage_messages/models/sms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from pydantic import BaseModel, Field
from vonage_utils.types.phone_number import PhoneNumber

from ..enums import ChannelType, EncodingType, MessageType
from .base_message import BaseMessage
from .enums import ChannelType, EncodingType, MessageType


class SmsOptions(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion messages/src/vonage_messages/models/viber.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from pydantic import BaseModel, Field, field_validator

from ..enums import ChannelType, MessageType
from .base_message import BaseMessage
from .enums import ChannelType, MessageType


class ViberAction(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion messages/src/vonage_messages/models/whatsapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from pydantic import BaseModel, ConfigDict, Field
from vonage_utils.types.phone_number import PhoneNumber

from ..enums import ChannelType, MessageType
from .base_message import BaseMessage
from .enums import ChannelType, MessageType


class WhatsappContext(BaseModel):
Expand Down
8 changes: 6 additions & 2 deletions messages/src/vonage_messages/responses.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from pydantic import BaseModel


class MessageUuid(BaseModel):
"""Response from Vonage's Messages API."""
class SendMessageResponse(BaseModel):
"""Response from Vonage's Messages API.
Attributes:
message_uuid (str): The UUID of the sent message.
"""

message_uuid: str
66 changes: 0 additions & 66 deletions messages/tests/_test_verify_v2.py

This file was deleted.

12 changes: 12 additions & 0 deletions messages/tests/data/invalid_error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"type": "https://developer.vonage.com/api-errors/messages#1150",
"title": "Invalid params",
"detail": "The value of one or more parameters is invalid.",
"instance": "bf0ca0bf927b3b52e3cb03217e1a1ddf",
"invalid_parameters": [
{
"name": "messenger.tag",
"reason": "invalid value"
}
]
}
6 changes: 6 additions & 0 deletions messages/tests/data/low_balance_error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "https://developer.nexmo.com/api-errors/#low-balance",
"title": "Low balance",
"detail": "This request could not be performed due to your account balance being low.",
"instance": "bf0ca0bf927b3b52e3cb03217e1a1ddf"
}
3 changes: 3 additions & 0 deletions messages/tests/data/send_message.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"message_uuid": "d8f86df1-dec6-442f-870a-2241be27d721"
}
Loading

0 comments on commit c45389b

Please sign in to comment.