Files
life-echo/api/app/adapters/sms/tencent.py
Sully fa42757916 feat: OpenTelemetry LGTM observability, dev tooling, and memoir UX fixes (#31)
* add staging ios app build script

* feat(api): add OpenTelemetry LGTM stack for local observability

Wire OTel traces, metrics, and logs through a collector to Tempo,
Prometheus, and Loki, with custom LLM instrumentation, dev compose overlay,
Grafana provisioning, env templates, and development.sh auto-start.

Co-authored-by: Cursor <cursoragent@cursor.com>

* feat: expand observability, harden dev tooling, and fix expo staging UX

Add business and LLM Prometheus metrics with Grafana dashboards, alerting,
and a metrics verification script. Wire telemetry through adapters and core
LLM paths, and document the local LGTM workflow.

Fix development.sh for macOS bash 3.2, open Grafana and eval-web in Chrome,
and repair eval-web auto-open (unbound EVAL_WEB_BROWSER_SCHEDULED). Merge
internal-eval into the main dev script with improved compose handling.

Require EXPO_PUBLIC_* at build time, improve iOS HTTP ATS for staging IPs,
show memoir empty state instead of load errors when no chapters exist, and
add jest env setup plus chapter list response normalization.

Co-authored-by: Cursor <cursoragent@cursor.com>

* chore: enable Grafana Assistant Cursor plugin

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: memoir empty state and repair withdrawn 0020_chapters_book_id stamp

Show empty memoir UI when the chapter list succeeds with no items; treat auth/404 as non-fatal. Extend alembic revision repair so local dev DBs stamped with the removed 0020_chapters_book_id migration can roll back and upgrade to 0019.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Kevin <kevin@brighteng.org>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-20 15:12:21 +08:00

89 lines
3.1 KiB
Python

"""Tencent Cloud SMS adapter — implements SmsSender port."""
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import (
TencentCloudSDKException,
)
from tencentcloud.sms.v20210111 import models as sms_models
from tencentcloud.sms.v20210111 import sms_client
from app.core.business_telemetry import business_span
from app.core.logging import get_logger
logger = get_logger(__name__)
CODE_EXPIRE_MINUTES = 5
class TencentSmsSender:
def __init__(
self,
secret_id: str,
secret_key: str,
sdk_app_id: str,
sign_name: str,
template_id: str,
template_param_count: int = 2,
):
self._secret_id = secret_id
self._secret_key = secret_key
self._sdk_app_id = sdk_app_id
self._sign_name = sign_name
self._template_id = template_id
self._template_param_count = template_param_count
def send_verification_code(self, phone: str, code: str) -> bool:
with business_span("sms.tencent.send"):
return self._send_verification_code_inner(phone, code)
def _send_verification_code_inner(self, phone: str, code: str) -> bool:
if not self._secret_id or not self._secret_key:
logger.error("Tencent SMS credentials not configured")
return False
cred = credential.Credential(self._secret_id, self._secret_key)
client = sms_client.SmsClient(cred, "ap-guangzhou")
param_configs = [
[code, str(CODE_EXPIRE_MINUTES)],
[code],
]
if self._template_param_count == 1:
param_configs = [[code], [code, str(CODE_EXPIRE_MINUTES)]]
for template_params in param_configs:
try:
req = sms_models.SendSmsRequest()
req.SmsSdkAppId = self._sdk_app_id
req.SignName = self._sign_name
req.TemplateId = self._template_id
req.TemplateParamSet = template_params
req.PhoneNumberSet = [f"+86{phone}"]
resp = client.SendSms(req)
if resp.SendStatusSet and resp.SendStatusSet[0].Code == "Ok":
return True
status = resp.SendStatusSet[0] if resp.SendStatusSet else None
error_code = status.Code if status else "UNKNOWN"
if "TemplateParamSetNotMatchApprovedTemplate" in error_code:
continue
logger.error(
"SMS send failed: {} - {}",
error_code,
status.Message if status else "",
)
return False
except TencentCloudSDKException as e:
if "TemplateParamSetNotMatchApprovedTemplate" in str(e):
continue
logger.error("Tencent SMS SDK error: {}", e)
return False
except Exception as e:
logger.error("SMS send exception: {}", e)
return False
logger.error("All SMS template param configs failed")
return False