Skip to content

ShortMessage

Features#

SMS plug -in abstract processing authentication process such as mobile phone verification code/Notice and other SMS sending requirements process,In order to facilitate developers to access the SMS services provided by major manufacturers through heavy loading functions

Implementation#

When developing SMS plug -in,Only inherit the SMS plug -in base class and load send_SMS function can,The processing process is as follows:

sequenceDiagram
    participant C as Platform core
    participant B as SMS plugin

    C->>B: Loading plug -in
    B->>C: Register to listen to SMS events

    rect rgb(191, 223, 255)
    C->>B: Send a SEND_SMS)
    B->>C: Corresponding events and send text messages according to your own configuration,Return result(success/fail)
    end

Abstract function#

Foundation definition#

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

Exemplary#

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

评论