Skip to content

Commit c45389b

Browse files
committed
prepare for messages release
1 parent 847df22 commit c45389b

27 files changed

+206
-104
lines changed

http_client/src/vonage_http_client/http_client.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,6 @@ def _parse_response(self, response: Response) -> Union[dict, None]:
225225
)
226226
self._last_response = response
227227
if 200 <= response.status_code < 300:
228-
if response.status_code == 204:
229-
return None
230228
try:
231229
return response.json()
232230
except JSONDecodeError:

messages/README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,72 @@ This package contains the code to use [Vonage's Messages API](https://developer.
66

77
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`.
88

9+
### How to Construct a Message
10+
11+
In order to send a message, you must construct a message object of the correct type. These are all found under `vonage_messages.models`.
12+
13+
```python
14+
from vonage_messages.models import Sms
15+
16+
message = Sms(
17+
from_='Vonage APIs',
18+
to='1234567890',
19+
text='This is a test message sent from the Vonage Python SDK',
20+
)
21+
```
22+
23+
This message can now be sent with
24+
25+
```python
26+
vonage_client.messages.send(message)
27+
```
28+
29+
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.
30+
31+
The different message models are listed at the bottom of the page.
32+
33+
Some message types have submodels with additional fields. In this case, import the submodels as well and use them to construct the overall options.
34+
35+
e.g.
36+
37+
```python
38+
from vonage_messages import MessengerImage, MessengerOptions, MessengerResource
39+
40+
messenger = MessengerImage(
41+
to='1234567890',
42+
from_='1234567890',
43+
image=MessengerResource(url='https://example.com/image.jpg'),
44+
messenger=MessengerOptions(category='message_tag', tag='invalid_tag'),
45+
)
46+
```
47+
948
### Send a message
1049

50+
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:
51+
52+
```python
53+
from vonage import Auth, Vonage
54+
from vonage_messages.models import Sms
55+
56+
vonage_client = Vonage(Auth(application_id='my-application-id', private_key='my-private-key'))
57+
58+
message = Sms(
59+
from_='Vonage APIs',
60+
to='1234567890',
61+
text='This is a test message sent from the Vonage Python SDK',
62+
)
63+
64+
vonage_client.messages.send(message)
65+
```
66+
67+
## Message Models
68+
69+
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:
70+
71+
```
72+
Sms
73+
MmsImage, MmsVcard, MmsAudio, MmsVideo
74+
WhatsappText, WhatsappImage, WhatsappAudio, WhatsappVideo, WhatsappFile, WhatsappTemplate, WhatsappSticker, WhatsappCustom
75+
MessengerText, MessengerImage, MessengerAudio, MessengerVideo, MessengerFile
76+
ViberText, ViberImage, ViberVideo, ViberFile
77+
```
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
from . import models
2-
from .enums import ChannelType, EncodingType, MessageType, WebhookVersion
32
from .messages import Messages
3+
from .responses import SendMessageResponse
44

5-
__all__ = [
6-
'models',
7-
'Messages',
8-
'ChannelType',
9-
'MessageType',
10-
'WebhookVersion',
11-
'EncodingType',
12-
]
5+
__all__ = ['models', 'Messages', 'SendMessageResponse']

messages/src/vonage_messages/messages.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from vonage_http_client.http_client import HttpClient
33

44
from .models import BaseMessage
5-
from .responses import MessageUuid
5+
from .responses import SendMessageResponse
66

77

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

2020
@validate_call
21-
def send(self, message: BaseMessage) -> MessageUuid:
21+
def send(self, message: BaseMessage) -> SendMessageResponse:
2222
"""Send a message using Vonage's Messages API.
2323
2424
Args:
25-
message (BaseMessage): The message to be sent.
25+
message (BaseMessage): The message to be sent as a Pydantic model.
26+
Use the provided models (in `vonage_messages.models`) to create messages and pass them in to this method.
2627
2728
Returns:
28-
MessageUuid: The unique identifier of the sent message.
29+
SendMessageResponse: Response model containing the unique identifier of the sent message.
30+
Access the identifier with the `message_uuid` attribute.
2931
"""
3032
response = self._http_client.post(
3133
self._http_client.api_host,
3234
'/v1/messages',
33-
message.model_dump(by_alias=True, exclude_none=True),
35+
message.model_dump(by_alias=True, exclude_none=True) or message,
3436
)
35-
36-
return MessageUuid(**response)
37+
return SendMessageResponse(**response)

messages/src/vonage_messages/models/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .base_message import BaseMessage
2+
from .enums import ChannelType, EncodingType, MessageType, WebhookVersion
23
from .messenger import (
34
MessengerAudio,
45
MessengerFile,
@@ -46,6 +47,9 @@
4647

4748
__all__ = [
4849
'BaseMessage',
50+
'ChannelType',
51+
'EncodingType',
52+
'MessageType',
4953
'MessengerAudio',
5054
'MessengerFile',
5155
'MessengerImage',
@@ -72,6 +76,7 @@
7276
'ViberVideo',
7377
'ViberVideoOptions',
7478
'ViberVideoResource',
79+
'WebhookVersion',
7580
'WhatsappAudio',
7681
'WhatsappAudioResource',
7782
'WhatsappContext',

messages/src/vonage_messages/models/base_message.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pydantic import BaseModel, Field
44
from vonage_utils.types.phone_number import PhoneNumber
55

6-
from ..enums import WebhookVersion
6+
from .enums import WebhookVersion
77

88

99
class BaseMessage(BaseModel):

messages/src/vonage_messages/models/messenger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
from pydantic import BaseModel, Field, model_validator
44

5-
from ..enums import ChannelType, MessageType
65
from .base_message import BaseMessage
6+
from .enums import ChannelType, MessageType
77

88

99
class MessengerResource(BaseModel):

messages/src/vonage_messages/models/mms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from pydantic import BaseModel, Field
44
from vonage_utils.types.phone_number import PhoneNumber
55

6-
from ..enums import ChannelType, MessageType
76
from .base_message import BaseMessage
7+
from .enums import ChannelType, MessageType
88

99

1010
class MmsResource(BaseModel):

messages/src/vonage_messages/models/sms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from pydantic import BaseModel, Field
44
from vonage_utils.types.phone_number import PhoneNumber
55

6-
from ..enums import ChannelType, EncodingType, MessageType
76
from .base_message import BaseMessage
7+
from .enums import ChannelType, EncodingType, MessageType
88

99

1010
class SmsOptions(BaseModel):

0 commit comments

Comments
 (0)