diff options
author | Frederick Yin <fkfd@fkfd.me> | 2022-02-08 16:11:43 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2022-02-08 16:11:43 +0800 |
commit | 54ba3f5ac95d002b485fa8f1f530de95ed2debda (patch) | |
tree | 08b23be5a64a28a1600f5173e2dca75381567cde | |
parent | ccef2ce14d14da2ccfcfa74778b8ec1e2a3839f3 (diff) |
SMS API
-rw-r--r-- | jimbrella/sms.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/jimbrella/sms.py b/jimbrella/sms.py new file mode 100644 index 0000000..e83dc54 --- /dev/null +++ b/jimbrella/sms.py @@ -0,0 +1,45 @@ +import logging +import json +from alibabacloud_dysmsapi20170525.client import Client as DysmsapiClient +from alibabacloud_dysmsapi20170525 import models as dysmsapi_models +from alibabacloud_tea_openapi import models as open_api_models +from .config import config + + +class SMS: + """SMS notification client. + + This client talks to Aliyun's shit-in-a-box API when JImbrella needs to + remind a tenant to return their overdue umbrella. + """ + + def __init__(self): + """Initialize Aliyun client""" + self.config = open_api_models.Config( + access_key_id=config.get("sms", "access_key_id"), + access_key_secret=config.get("sms", "access_key_secret"), + ) + self.client = DysmsapiClient(self.config) + + def _send( + self, + phone_numbers: str, + sign_name: str, + template_code: str, + template_param: dict, + ): + """Call API to send generic SMS""" + req = dysmsapi_models.SendSmsRequest( + phone_numbers=phone_numbers, + sign_name=sign_name, + template_code=template_code, + template_param=json.dumps(template_param), + ) + resp = self.client.send_sms(req) + if resp.body.code != "OK": + logging.warning( + "API call to send SMS notification to %s failed", phone_numbers + ) + + def remind_overdue(self, tenant_phone: str, tenant_name: str, key: int): + self._send(tenant_phone, "TBD", "TBD", {"name": tenant_name, "key": key}) |