|
| 1 | +from enum import Enum |
| 2 | +from typing import List, Optional, Union |
| 3 | + |
| 4 | +try: |
| 5 | + from typing import Literal |
| 6 | +except ImportError: |
| 7 | + from typing_extensions import Literal |
| 8 | + |
| 9 | +from pydantic import AnyHttpUrl, Field, confloat, constr, validator |
| 10 | + |
| 11 | +from infobip_channels.core.models import ( |
| 12 | + CamelCaseModel, |
| 13 | + MessageBodyBase, |
| 14 | + UrlLengthValidatorMixin, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +class ValidityPeriodTimeUnitEnum(str, Enum): |
| 19 | + SECONDS = "SECONDS" |
| 20 | + MINUTES = "MINUTES" |
| 21 | + HOURS = "HOURS" |
| 22 | + DAYS = "DAYS" |
| 23 | + |
| 24 | + |
| 25 | +class CardWidth(str, Enum): |
| 26 | + SMALL = "SMALL" |
| 27 | + MEDIUM = "MEDIUM" |
| 28 | + |
| 29 | + |
| 30 | +class HeightEnum(str, Enum): |
| 31 | + SHORT = "SHORT" |
| 32 | + MEDIUM = "MEDIUM" |
| 33 | + TALL = "TALL" |
| 34 | + |
| 35 | + |
| 36 | +class AlignmentEnum(str, Enum): |
| 37 | + LEFT = "LEFT" |
| 38 | + RIGHT = "RIGHT" |
| 39 | + |
| 40 | + |
| 41 | +class OrientationEnum(str, Enum): |
| 42 | + HORIZONTAL = "HORIZONTAL" |
| 43 | + VERTICAL = "VERTICAL" |
| 44 | + |
| 45 | + |
| 46 | +class FileProperties(CamelCaseModel, UrlLengthValidatorMixin): |
| 47 | + url: AnyHttpUrl |
| 48 | + |
| 49 | + _MAX_URL_LENGTH = 1000 |
| 50 | + |
| 51 | + @validator("url", pre=True) |
| 52 | + def validate_url_length(cls, value: str) -> str: |
| 53 | + return super().validate_url_length(value) |
| 54 | + |
| 55 | + |
| 56 | +class ThumbnailProperties(CamelCaseModel, UrlLengthValidatorMixin): |
| 57 | + url: AnyHttpUrl |
| 58 | + |
| 59 | + _MAX_URL_LENGTH = 1000 |
| 60 | + |
| 61 | + @validator("url", pre=True) |
| 62 | + def validate_url_length(cls, value: str) -> str: |
| 63 | + return super().validate_url_length(value) |
| 64 | + |
| 65 | + |
| 66 | +class Suggestion(CamelCaseModel): |
| 67 | + text: constr(min_length=1, max_length=25) |
| 68 | + postback_data: constr(min_length=1, max_length=2048) |
| 69 | + |
| 70 | + |
| 71 | +class SuggestionReply(Suggestion): |
| 72 | + type: Literal["REPLY"] |
| 73 | + |
| 74 | + |
| 75 | +class SuggestionOpenUrl(Suggestion, UrlLengthValidatorMixin): |
| 76 | + type: Literal["OPEN_URL"] |
| 77 | + url: AnyHttpUrl |
| 78 | + |
| 79 | + _MAX_URL_LENGTH = 1000 |
| 80 | + |
| 81 | + @validator("url", pre=True) |
| 82 | + def validate_url_length(cls, value: str) -> str: |
| 83 | + return super().validate_url_length(value) |
| 84 | + |
| 85 | + |
| 86 | +class SuggestionDialPhone(Suggestion): |
| 87 | + type: Literal["DIAL_PHONE"] |
| 88 | + phone_number: Optional[constr(regex=r"\+?\d{5,15}")] = None # noqa: F722 |
| 89 | + |
| 90 | + |
| 91 | +class SuggestionShowLocation(Suggestion): |
| 92 | + type: Literal["SHOW_LOCATION"] |
| 93 | + latitude: confloat(ge=-90, le=90) |
| 94 | + longitude: confloat(ge=-180, le=180) |
| 95 | + label: Optional[constr(min_length=1, max_length=100)] = None |
| 96 | + |
| 97 | + |
| 98 | +class SuggestionRequestLocation(Suggestion): |
| 99 | + type: Literal["REQUEST_LOCATION"] |
| 100 | + |
| 101 | + |
| 102 | +class CardMedia(CamelCaseModel): |
| 103 | + file: FileProperties |
| 104 | + thumbnail: Optional[ThumbnailProperties] = None |
| 105 | + height: HeightEnum |
| 106 | + |
| 107 | + |
| 108 | +class CardContent(CamelCaseModel): |
| 109 | + title: Optional[constr(min_length=1, max_length=200)] = None |
| 110 | + description: Optional[constr(min_length=1, max_length=2000)] = None |
| 111 | + media: Optional[CardMedia] = None |
| 112 | + suggestions: Optional[ |
| 113 | + List[ |
| 114 | + Union[ |
| 115 | + SuggestionShowLocation, |
| 116 | + SuggestionDialPhone, |
| 117 | + SuggestionOpenUrl, |
| 118 | + SuggestionReply, |
| 119 | + SuggestionRequestLocation, |
| 120 | + ] |
| 121 | + ] |
| 122 | + ] = None |
| 123 | + |
| 124 | + @validator("suggestions") |
| 125 | + def validate_suggestions(cls, suggestions: List[Suggestion]) -> List[Suggestion]: |
| 126 | + if len(suggestions) > 4: |
| 127 | + raise ValueError("There can be only four suggestions in a card") |
| 128 | + return suggestions |
| 129 | + |
| 130 | + |
| 131 | +class Contents(CardContent): |
| 132 | + pass |
| 133 | + |
| 134 | + |
| 135 | +class ContentCarousel(CamelCaseModel): |
| 136 | + type: Literal["CAROUSEL"] |
| 137 | + card_width: CardWidth |
| 138 | + contents: List[Contents] |
| 139 | + suggestions: Optional[ |
| 140 | + List[ |
| 141 | + Union[ |
| 142 | + SuggestionShowLocation, |
| 143 | + SuggestionDialPhone, |
| 144 | + SuggestionOpenUrl, |
| 145 | + SuggestionReply, |
| 146 | + SuggestionRequestLocation, |
| 147 | + ] |
| 148 | + ] |
| 149 | + ] = None |
| 150 | + |
| 151 | + @validator("contents") |
| 152 | + def validate_contents(cls, contents: List[Contents]) -> List[Contents]: |
| 153 | + if len(contents) < 2 or len(contents) > 10: |
| 154 | + raise ValueError("There can be only 2 - 10 content objects in a Carousel") |
| 155 | + |
| 156 | + return contents |
| 157 | + |
| 158 | + |
| 159 | +class ContentCard(CamelCaseModel): |
| 160 | + type: Literal["CARD"] |
| 161 | + orientation: OrientationEnum |
| 162 | + alignment: AlignmentEnum |
| 163 | + content: CardContent |
| 164 | + suggestions: Optional[ |
| 165 | + List[ |
| 166 | + Union[ |
| 167 | + SuggestionShowLocation, |
| 168 | + SuggestionDialPhone, |
| 169 | + SuggestionOpenUrl, |
| 170 | + SuggestionReply, |
| 171 | + SuggestionRequestLocation, |
| 172 | + ] |
| 173 | + ] |
| 174 | + ] = None |
| 175 | + |
| 176 | + |
| 177 | +class ContentFile(CamelCaseModel): |
| 178 | + type: Literal["FILE"] |
| 179 | + file: FileProperties |
| 180 | + thumbnail: Optional[ThumbnailProperties] = None |
| 181 | + |
| 182 | + |
| 183 | +class ContentText(CamelCaseModel): |
| 184 | + type: Literal["TEXT"] |
| 185 | + text: constr(min_length=1, max_length=1000) |
| 186 | + suggestions: Optional[ |
| 187 | + List[ |
| 188 | + Union[ |
| 189 | + SuggestionShowLocation, |
| 190 | + SuggestionDialPhone, |
| 191 | + SuggestionOpenUrl, |
| 192 | + SuggestionReply, |
| 193 | + SuggestionRequestLocation, |
| 194 | + ] |
| 195 | + ] |
| 196 | + ] = [] |
| 197 | + |
| 198 | + |
| 199 | +class SmsFailover(CamelCaseModel): |
| 200 | + from_number: constr(min_length=1) = Field(alias="from") |
| 201 | + text: constr(min_length=1) |
| 202 | + validity_period: Optional[int] = None |
| 203 | + validity_period_time_unit: Optional[ValidityPeriodTimeUnitEnum] = None |
| 204 | + |
| 205 | + |
| 206 | +class RCSMessageBody(MessageBodyBase): |
| 207 | + from_number: Optional[str] = Field(alias="from") |
| 208 | + to: str |
| 209 | + validity_period: Optional[int] = None |
| 210 | + validity_period_time_unit: Optional[ValidityPeriodTimeUnitEnum] = None |
| 211 | + content: Union[ContentCarousel, ContentCard, ContentFile, ContentText] |
| 212 | + sms_failover: Optional[SmsFailover] = None |
| 213 | + notify_url: Optional[str] = None |
| 214 | + callback_data: Optional[str] = None |
| 215 | + message_id: Optional[str] = None |
0 commit comments