适配新告警短信模板
This commit is contained in:
@@ -41,6 +41,11 @@ class SmsNotificationService(DatabaseServiceBase):
|
||||
child_name = child_name or context.get("child_name")
|
||||
address = address or context.get("address")
|
||||
|
||||
child_name = self._normalize_conference(child_name)
|
||||
if not child_name:
|
||||
session_logger.warning(device_id, "sms", "告警短信跳过:缺少符合模板要求的孩子姓名")
|
||||
return
|
||||
|
||||
recipients = await self._list_device_family_phone_numbers(device_id=device_id)
|
||||
if not recipients:
|
||||
session_logger.info(device_id, "sms", "告警短信跳过:没有可用家长手机号")
|
||||
@@ -76,6 +81,11 @@ class SmsNotificationService(DatabaseServiceBase):
|
||||
if not self._is_enabled():
|
||||
return
|
||||
|
||||
child_name = self._normalize_conference(child_name)
|
||||
if not child_name:
|
||||
session_logger.warning(device_id, "sms", "留言短信跳过:缺少符合模板要求的孩子姓名")
|
||||
return
|
||||
|
||||
recipients = await self._list_device_family_phone_numbers(device_id=device_id)
|
||||
if not recipients:
|
||||
session_logger.info(device_id, "sms", "留言短信跳过:没有可用家长手机号")
|
||||
@@ -365,11 +375,14 @@ class SmsNotificationService(DatabaseServiceBase):
|
||||
) -> dict[str, str]:
|
||||
return {
|
||||
"code": event_label,
|
||||
"conference": (child_name or "伴伴设备")[:20],
|
||||
"conference": self._normalize_conference(child_name),
|
||||
"address": self._sanitize_sms_address(address),
|
||||
"time": event_time,
|
||||
}
|
||||
|
||||
def _normalize_conference(self, child_name: str | None) -> str:
|
||||
return str(child_name or "").strip()[:20]
|
||||
|
||||
def _sanitize_sms_address(self, address: str | None) -> str:
|
||||
raw_address = str(address or "").strip()
|
||||
if not raw_address or _SMS_COORDINATE_PATTERN.match(raw_address):
|
||||
|
||||
@@ -42,8 +42,8 @@ def configure_enabled_sms(monkeypatch):
|
||||
monkeypatch.setattr("banban.service.sms_notification.settings.aliyun_sms_access_key_id", "access-key-id")
|
||||
monkeypatch.setattr("banban.service.sms_notification.settings.aliyun_sms_access_key_secret", "access-key-secret")
|
||||
monkeypatch.setattr("banban.service.sms_notification.settings.aliyun_sms_sign_name", "短信签名")
|
||||
monkeypatch.setattr("banban.service.sms_notification.settings.aliyun_sms_template_code", "SMS_507185027")
|
||||
monkeypatch.setattr("banban.service.sms_notification.settings.aliyun_sms_alarm_template_code", "")
|
||||
monkeypatch.setattr("banban.service.sms_notification.settings.aliyun_sms_template_code", "")
|
||||
monkeypatch.setattr("banban.service.sms_notification.settings.aliyun_sms_alarm_template_code", "SMS_509735147")
|
||||
monkeypatch.setattr("banban.service.sms_notification.settings.aliyun_sms_leave_message_template_code", "")
|
||||
|
||||
|
||||
@@ -77,7 +77,7 @@ async def test_notify_alarm_sends_to_unique_family_phone_numbers(monkeypatch):
|
||||
)
|
||||
|
||||
assert [item["phone_number"] for item in sent] == ["13800138000", "13900139000"]
|
||||
assert all(item["template_code"] == "SMS_507185027" for item in sent)
|
||||
assert all(item["template_code"] == "SMS_509735147" for item in sent)
|
||||
assert sent[0]["template_params"]["conference"] == "孩子"
|
||||
assert sent[0]["template_params"]["address"] == "杭州市"
|
||||
|
||||
@@ -115,6 +115,27 @@ def test_sms_address_sanitizer_falls_back_for_coordinates():
|
||||
assert service._sanitize_sms_address("30.245,120.215") == "请打开小程序查看"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_notify_alarm_skips_when_child_name_is_missing(monkeypatch):
|
||||
configure_enabled_sms(monkeypatch)
|
||||
service = SmsNotificationService()
|
||||
sent = []
|
||||
|
||||
async def fake_send_sms(**kwargs):
|
||||
sent.append(kwargs)
|
||||
return {"ok": True, "code": "OK", "message": "OK", "request_id": "request-id"}
|
||||
|
||||
monkeypatch.setattr(service, "_send_sms", fake_send_sms)
|
||||
|
||||
await service.notify_alarm(
|
||||
device_id="TalkingQ_XQSN00001005",
|
||||
child_name=" ",
|
||||
address="杭州市",
|
||||
)
|
||||
|
||||
assert sent == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_notify_alarm_dedupes_same_device_and_phone(monkeypatch):
|
||||
configure_enabled_sms(monkeypatch)
|
||||
|
||||
Reference in New Issue
Block a user