Skip to content

短信

功能介绍#

短信插件抽象处理认证过程中如手机验证码/通知等短信发送需求过程,以便于开发者通过重载函数方式接入各大厂商提供的短信服务

实现思路#

开发者在开发短信插件时,仅需继承短信插件基类并重载send_sms函数即可,处理流程如下:

sequenceDiagram
    participant C as 平台核心
    participant B as 短信插件

    C->>B: 加载插件
    B->>C: 注册监听短信事件

    rect rgb(191, 223, 255)
    C->>B: 发出发送短信事件(SEND_SMS)
    B->>C: 相应事件并根据自身配置发送短信,返回结果(成功/失败)
    end

抽象函数#

基类定义#

arkid.core.extension.sms.SmsExtension (Extension) #

Source code in arkid/core/extension/sms.py
class SmsExtension(Extension):

    TYPE = "sms"

    @property
    def type(self):
        return SmsExtension.TYPE

    def load(self):
        self.listen_event(SEND_SMS, self.event_send_sms)
        super().load()

    def event_send_sms(self,event,**kwargs):
        """ 发送短信时间

        Args:
            event (Event): 事件
        事件数据(event.data)结构说明:
            ``` json
            {
                "config_id": "xxxx", # 必传,插件运行时配置ID
                "mobile": "xxxx", # 必传,电话号码
                "code": "xxxx", # 非必传,验证码
                "areacode": "xxxx", # 非必传,区号
                "username": "xxxx" # 非必传,用户名
                ...
            }

            ```

        Returns:
            发送短信结果
        """
        if event.packages == self.package or self.package in event.packages:
            return self.send_sms(event,**kwargs)


    @abstractmethod   
    def send_sms(self, event:Event, **kwargs):
        """ 发送短信

        Args:
            event (Event): 事件
        """
        pass

event_send_sms(self, event, **kwargs) #

发送短信时间

Parameters:

Name Type Description Default
event Event

事件

required

事件数据(event.data)结构说明:

{
    "config_id": "xxxx", # 必传,插件运行时配置ID
    "mobile": "xxxx", # 必传,电话号码
    "code": "xxxx", # 非必传,验证码
    "areacode": "xxxx", # 非必传,区号
    "username": "xxxx" # 非必传,用户名
    ...
}

Returns:

Type Description

发送短信结果

Source code in arkid/core/extension/sms.py
def event_send_sms(self,event,**kwargs):
    """ 发送短信时间

    Args:
        event (Event): 事件
    事件数据(event.data)结构说明:
        ``` json
        {
            "config_id": "xxxx", # 必传,插件运行时配置ID
            "mobile": "xxxx", # 必传,电话号码
            "code": "xxxx", # 非必传,验证码
            "areacode": "xxxx", # 非必传,区号
            "username": "xxxx" # 非必传,用户名
            ...
        }

        ```

    Returns:
        发送短信结果
    """
    if event.packages == self.package or self.package in event.packages:
        return self.send_sms(event,**kwargs)

load(self) #

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

Source code in arkid/core/extension/sms.py
def load(self):
    self.listen_event(SEND_SMS, self.event_send_sms)
    super().load()

send_sms(self, event, **kwargs) #

发送短信

Parameters:

Name Type Description Default
event Event

事件

required
Source code in arkid/core/extension/sms.py
@abstractmethod   
def send_sms(self, event:Event, **kwargs):
    """ 发送短信

    Args:
        event (Event): 事件
    """
    pass

示例#

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

评论