1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
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
notify a tenant of some significant event.
"""
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_number: str,
template_code: str,
template_param: dict,
):
"""Call API to send generic SMS"""
req = dysmsapi_models.SendSmsRequest(
phone_numbers=phone_number,
sign_name=config.get("sms", "signature"),
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 (%s: %s)",
phone_number,
resp.body.code,
resp.body.message,
)
def borrow_success(self, phone: str, name: str, date: str, umbid: int):
"""Current template: jimbrella_takeaway_0
${name}同学,您已于${date}成功借用${umbid}号信用伞,请在三日内归还
"""
self._send(
phone,
config.get("sms", "template_borrow_success"),
{"name": name, "date": date, "umbid": umbid},
)
def return_success(self, phone: str, name: str, date: str, umbid: int):
"""Current template: jimbrella_giveback_0
${name}同学,您已于${date}成功归还${umbid}号信用伞
"""
self._send(
phone,
config.get("sms", "template_return_success"),
{"name": name, "date": date, "umbid": umbid},
)
def remind_overdue(self, phone: str, name: str, date: str, umbid: int):
"""Current template: jimbrella_overdue_0
${name}同学,您在${date}借用的${umbid}号信用伞已超过3天时限,请及时归还
"""
self._send(
phone,
config.get("sms", "template_remind_overdue"),
{"name": name, "date": date, "umbid": umbid},
)
|