shiftregister/shiftregister/messaging/message.py

56 lines
1.4 KiB
Python

from datetime import datetime
from enum import Enum, auto
from django.utils import timezone
from phonenumber_field.phonenumber import PhoneNumber
class MessageType(Enum):
INBOUND = auto()
OUTBOUND = auto()
class Message:
def __init__(
self,
key,
recipient="",
sender="",
text="",
type=None,
created_at=timezone.now(),
):
key = str(key)
recipient = str(recipient)
sender = str(sender)
text = str(text)
if not isinstance(type, MessageType):
raise TypeError("message type must be of type MessageType")
if not isinstance(created_at, datetime):
raise TypeError("message created_at must be of type datetime")
if (
type == MessageType.OUTBOUND
and not PhoneNumber.from_string(recipient).is_valid()
):
raise ValueError(
f"invalid recipient phone number for outbound message: {recipient}"
)
self.key = key
self.recipient = recipient
self.sender = sender
self.text = text
self.created_at = created_at
def __eq__(self, other):
if other is None:
return False
elif isinstance(other, Message):
other = other.key
else:
other = str(other)
return self.key == other