new code
This commit is contained in:
68
sms.py
68
sms.py
@@ -4,7 +4,7 @@ import sys
|
||||
import os
|
||||
import json
|
||||
|
||||
from typing import List
|
||||
from typing import List, Union, Dict, Any
|
||||
|
||||
from alibabacloud_dysmsapi20170525.client import Client as DysmsapiClient
|
||||
from alibabacloud_tea_openapi import models as open_api_models
|
||||
@@ -40,9 +40,10 @@ class SMS:
|
||||
@staticmethod
|
||||
def main(self,
|
||||
phone_number: str,
|
||||
template_param: str,
|
||||
template_param: Union[str, Dict[str, Any]] = None,
|
||||
template_code: str = settings.sms_template_code,
|
||||
sign_name: str = settings.sms_sign_name,
|
||||
**kwargs
|
||||
) -> dict:
|
||||
try:
|
||||
|
||||
@@ -55,9 +56,30 @@ class SMS:
|
||||
phone_number = phone_number
|
||||
# sign_name is passed as argument
|
||||
# template_code is passed as argument
|
||||
param_code = f"{{\"code\":\"{template_param}\"}}"
|
||||
|
||||
ConsoleClient.log(f"正在尝试发送短信... 模板代码: {template_code}, 签名: {sign_name}")
|
||||
# 构建参数字典
|
||||
params = {}
|
||||
if template_param:
|
||||
if isinstance(template_param, dict):
|
||||
params.update(template_param)
|
||||
elif isinstance(template_param, str):
|
||||
# 尝试解析 JSON 字符串,如果失败则作为 code 字段的值
|
||||
try:
|
||||
json_param = json.loads(template_param)
|
||||
if isinstance(json_param, dict):
|
||||
params.update(json_param)
|
||||
else:
|
||||
params['code'] = template_param
|
||||
except json.JSONDecodeError:
|
||||
params['code'] = template_param
|
||||
|
||||
# 添加额外的动态参数
|
||||
if kwargs:
|
||||
params.update(kwargs)
|
||||
|
||||
param_code = json.dumps(params)
|
||||
|
||||
ConsoleClient.log(f"正在尝试发送短信... 模板代码: {template_code}, 签名: {sign_name}, 参数: {param_code}")
|
||||
# 1.发送短信
|
||||
send_req = dysmsapi_models.SendSmsRequest(
|
||||
phone_numbers=phone_number,
|
||||
@@ -130,9 +152,10 @@ class SMS:
|
||||
@staticmethod
|
||||
async def main_async(self,
|
||||
phone_number: str,
|
||||
template_param: str,
|
||||
template_param: Union[str, Dict[str, Any]] = None,
|
||||
template_code: str = settings.sms_template_code,
|
||||
sign_name: str = settings.sms_sign_name,
|
||||
**kwargs
|
||||
) -> None:
|
||||
try:
|
||||
|
||||
@@ -143,19 +166,40 @@ class SMS:
|
||||
phone_number = phone_number
|
||||
# sign_name is passed as argument
|
||||
# template_code is passed as argument
|
||||
template_param = f"{{\"code\":\"{template_param}\"}}"
|
||||
|
||||
# 构建参数字典
|
||||
params = {}
|
||||
if template_param:
|
||||
if isinstance(template_param, dict):
|
||||
params.update(template_param)
|
||||
elif isinstance(template_param, str):
|
||||
# 尝试解析 JSON 字符串,如果失败则作为 code 字段的值
|
||||
try:
|
||||
json_param = json.loads(template_param)
|
||||
if isinstance(json_param, dict):
|
||||
params.update(json_param)
|
||||
else:
|
||||
params['code'] = template_param
|
||||
except json.JSONDecodeError:
|
||||
params['code'] = template_param
|
||||
|
||||
# 添加额外的动态参数
|
||||
if kwargs:
|
||||
params.update(kwargs)
|
||||
|
||||
param_code = json.dumps(params)
|
||||
|
||||
# 如果提供了命令行参数,则使用命令行参数
|
||||
|
||||
|
||||
|
||||
ConsoleClient.log(f"正在尝试发送短信... 模板代码: {template_code}, 签名: {sign_name}")
|
||||
ConsoleClient.log(f"正在尝试发送短信... 模板代码: {template_code}, 签名: {sign_name}, 参数: {param_code}")
|
||||
# 1.发送短信
|
||||
send_req = dysmsapi_models.SendSmsRequest(
|
||||
phone_numbers=phone_number,
|
||||
sign_name=sign_name,
|
||||
template_code=template_code,
|
||||
template_param=template_param
|
||||
template_param=param_code
|
||||
)
|
||||
try:
|
||||
send_resp = await client.send_sms_async(send_req)
|
||||
@@ -216,6 +260,10 @@ class SMS:
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
SMS = SMS()
|
||||
SMS.main(self=SMS, phone_number="18585164448", template_param="6666")
|
||||
sms = SMS()
|
||||
# 示例 1: 原有方式,仅传递 code
|
||||
# sms.main(self=sms, phone_number="18585164448", template_param="6666")
|
||||
|
||||
# 示例 2: 动态参数传递 (phone, price 等)
|
||||
sms.main(self=sms, phone_number="18585164448", code="6666")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user