短信插件:阿里云短信#
功能介绍#
为平台提供短信服务支持,短信服务商为阿里云短信。
配置指南#
抽象方法实现#
代码#
extension_root.com_longgui_sms_aliyun.AliyunSMSExtension (SmsExtension)
#
Source code in extension_root/com_longgui_sms_aliyun/__init__.py
class AliyunSMSExtension(SmsExtension):
def load(self):
self.register_settings_schema(SettingsSchema)
self.register_config_schema(ConfigSchema)
super().load()
def send_sms(self, event, **kwargs):
tenant = event.tenant
config_id = event.data.pop("config_id")
mobile = event.data.pop("mobile")
template_params = {}
settings = self.get_settings(tenant)
settings = SimpleNamespace(**settings.settings)
config = self.get_config_by_id(config_id).config
for key in config.get("template_params",["code"]):
template_params[key] = event.data.get(key,"")
template_params = json.dumps(template_params)
config = SimpleNamespace(**config)
aliyun_config = models.Config(
# 您的AccessKey ID,
access_key_id=settings.access_key_id,
# 您的AccessKey Secret,
access_key_secret=settings.access_key_secret,
# 地域ID
region_id=settings.region_id or None,
# 访问的域名
endpoint=settings.endpoint or None,
)
client = Client(aliyun_config)
send_sms_request = dysmsapi_20170525_models.SendSmsRequest(
phone_numbers=mobile,
sign_name=config.sign_name,
template_code=config.template_code,
template_param=template_params,
sms_up_extend_code=config.sms_up_extend_code or None,
out_id=config.out_id or None,
)
res = client.send_sms(send_sms_request)
return res.body.to_map()
load(self)
#
send_sms(self, event, **kwargs)
#
发送短信
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event |
Event |
事件 |
required |
Source code in extension_root/com_longgui_sms_aliyun/__init__.py
def send_sms(self, event, **kwargs):
tenant = event.tenant
config_id = event.data.pop("config_id")
mobile = event.data.pop("mobile")
template_params = {}
settings = self.get_settings(tenant)
settings = SimpleNamespace(**settings.settings)
config = self.get_config_by_id(config_id).config
for key in config.get("template_params",["code"]):
template_params[key] = event.data.get(key,"")
template_params = json.dumps(template_params)
config = SimpleNamespace(**config)
aliyun_config = models.Config(
# 您的AccessKey ID,
access_key_id=settings.access_key_id,
# 您的AccessKey Secret,
access_key_secret=settings.access_key_secret,
# 地域ID
region_id=settings.region_id or None,
# 访问的域名
endpoint=settings.endpoint or None,
)
client = Client(aliyun_config)
send_sms_request = dysmsapi_20170525_models.SendSmsRequest(
phone_numbers=mobile,
sign_name=config.sign_name,
template_code=config.template_code,
template_param=template_params,
sms_up_extend_code=config.sms_up_extend_code or None,
out_id=config.out_id or None,
)
res = client.send_sms(send_sms_request)
return res.body.to_map()