Skip to content

短信插件:阿里云短信#

功能介绍#

为平台提供短信服务支持,短信服务商为阿里云短信。

配置指南#

经由左侧菜单栏依次进入【租户管理】->【插件管理】,在插件租赁页面中找到阿里云短信插件卡片,点击租赁
插件租赁

租赁完成后,进入已租赁列表,找到阿里云短信插件卡片,点击租户配置,配置相关数据
* 域名默认为dysmsapi.aliyuncs.com
插件租户配置

租户配置完成后回到已租赁页面,在阿里云短信插件卡片上点击运行时配置,在弹出窗口中点击新建,输入对应的短信模板配置即可
插件运行时配置
插件运行时配置
* 注意: 模板参数一项中如不填则默认为["code"],适用于短信验证码

抽象方法实现#

代码#

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) #

抽象方法,插件加载的入口方法

Source code in extension_root/com_longgui_sms_aliyun/__init__.py
def load(self):
    self.register_settings_schema(SettingsSchema)
    self.register_config_schema(ConfigSchema)
    super().load()

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()

评论