|
3 | 3 | import logging |
4 | 4 | import re |
5 | 5 | import sys |
| 6 | +from typing import Dict, Optional |
6 | 7 |
|
7 | 8 | logger = logging.getLogger("urbanairship") |
8 | 9 |
|
@@ -347,3 +348,82 @@ def post(self): |
347 | 348 | ) |
348 | 349 |
|
349 | 350 | return response |
| 351 | + |
| 352 | + |
| 353 | +class SmsCustomResponse: |
| 354 | + """Respond to a mobile originated message based on a keyword consumed by your |
| 355 | + custom response webhook, using a mobile-originated ID. Please see the documentation |
| 356 | + at https://docs.airship.com/api/ua/?http#operation-api-sms-custom-response-post for |
| 357 | + details on use of this feature. |
| 358 | +
|
| 359 | + One of `mms` or `sms` is required. |
| 360 | +
|
| 361 | + :param airship: [required] An urbanairship.Airship instance, created with |
| 362 | + bearer token authentication. |
| 363 | + :param mobile_originated_id: [required] The identifier that you received through |
| 364 | + your SMS webhook corresponding to the mobile-originated message that you're |
| 365 | + issuing a custom response to. |
| 366 | + :param sms: [optional] An SMS platform override object, created using |
| 367 | + `ua.sms()`. This defines the message sent in response. |
| 368 | + :param mms: [optional] An MMS platform override object, created using |
| 369 | + `us.mms()`. The defines the message sent in response. |
| 370 | + """ |
| 371 | + |
| 372 | + def __init__( |
| 373 | + self, |
| 374 | + airship, |
| 375 | + mobile_originated_id: str, |
| 376 | + sms: Optional[Dict] = None, |
| 377 | + mms: Optional[Dict] = None, |
| 378 | + ) -> None: |
| 379 | + self.airship = airship |
| 380 | + self.mobile_originated_id = mobile_originated_id |
| 381 | + self.sms = sms |
| 382 | + self.mms = mms |
| 383 | + |
| 384 | + @property |
| 385 | + def sms(self) -> Optional[Dict]: |
| 386 | + return self._sms |
| 387 | + |
| 388 | + @sms.setter |
| 389 | + def sms(self, value: Optional[Dict]) -> None: |
| 390 | + self._sms = value |
| 391 | + |
| 392 | + @property |
| 393 | + def mms(self) -> Optional[Dict]: |
| 394 | + return self._mms |
| 395 | + |
| 396 | + @mms.setter |
| 397 | + def mms(self, value: Optional[Dict]) -> None: |
| 398 | + self._mms = value |
| 399 | + |
| 400 | + @property |
| 401 | + def _payload(self) -> Dict: |
| 402 | + if all((self.mms, self.sms)): |
| 403 | + raise ValueError("Cannot use both mms and sms.") |
| 404 | + if all((self.sms is None, self.mms is None)): |
| 405 | + raise ValueError("One of mms or sms must be set.") |
| 406 | + |
| 407 | + payload = {"mobile_originated_id": self.mobile_originated_id} |
| 408 | + |
| 409 | + if self.sms is not None: |
| 410 | + payload["sms"] = self.sms |
| 411 | + if self.mms is not None: |
| 412 | + payload.update(self.mms) |
| 413 | + |
| 414 | + return payload |
| 415 | + |
| 416 | + def send(self) -> Dict: |
| 417 | + """Sends the response using the mobile_originated_id value |
| 418 | +
|
| 419 | + :return: An API response dictionary |
| 420 | + """ |
| 421 | + response = self.airship.request( |
| 422 | + method="POST", |
| 423 | + body=json.dumps(self._payload), |
| 424 | + url=self.airship.urls.get("sms_custom_response_url"), |
| 425 | + content_type="application/json", |
| 426 | + version=3, |
| 427 | + ) |
| 428 | + |
| 429 | + return response.json() |
0 commit comments