The Seven Year Itch.

MENU

Flask 封装阿里大于短信服务

August 15, 2018 • Read: 773 • Python,Flask

收先使用pip安装好第三方 "aliyunsdkcore"

pipenv install aliyunsdkcore

新建alidayu.py文件 ,加入以下代码,官方的命名实在是。。需要用的我自己改了

# -*- encoding:utf-8 -*-
"""
@ Created by Seven on  2018/08/14
"""

from aliyunsdkcore.request import RpcRequest


class SendSmsRequest(RpcRequest):

    def __init__(self):
        RpcRequest.__init__(self, 'Dysmsapi', '2017-05-25', 'SendSms')

    def get_template_code(self):
        return self.get_query_params().get('TemplateCode')

    def set_template_code(self, template_code):
        self.add_query_param('TemplateCode', template_code)

    def get_PhoneNumbers(self):
        return self.get_query_params().get('PhoneNumbers')

    def set_phone_numbers(self, set_phone_numbers):
        self.add_query_param('PhoneNumbers', set_phone_numbers)

    def get_SignName(self):
        return self.get_query_params().get('SignName')

    def set_sign_name(self, set_sign_name):
        self.add_query_param('SignName', set_sign_name)

    def get_ResourceOwnerAccount(self):
        return self.get_query_params().get('ResourceOwnerAccount')

    def set_ResourceOwnerAccount(self, ResourceOwnerAccount):
        self.add_query_param('ResourceOwnerAccount', ResourceOwnerAccount)

    def get_TemplateParam(self):
        return self.get_query_params().get('TemplateParam')

    def set_template_param(self, template_param):
        self.add_query_param('TemplateParam', template_param)

    def get_ResourceOwnerId(self):
        return self.get_query_params().get('ResourceOwnerId')

    def set_ResourceOwnerId(self, ResourceOwnerId):
        self.add_query_param('ResourceOwnerId', ResourceOwnerId)

    def get_OwnerId(self):
        return self.get_query_params().get('OwnerId')

    def set_OwnerId(self, OwnerId):
        self.add_query_param('OwnerId', OwnerId)

    def get_SmsUpExtendCode(self):
        return self.get_query_params().get('SmsUpExtendCode')

    def set_SmsUpExtendCode(self, SmsUpExtendCode):
        self.add_query_param('SmsUpExtendCode', SmsUpExtendCode)

    def get_OutId(self):
        return self.get_query_params().get('OutId')

    def set_out_id(self, out_id):
        self.add_query_param('OutId', out_id)

功能实现

# -*- encoding:utf-8 -*-
"""
@ Created by Seven on  2018/08/15 
"""
import uuid

from aliyunsdkcore.client import AcsClient as connection
from aliyunsdkcore.profile import region_provider
from flask import current_app

from app.libs.alidayu import SendSmsRequest

sign_name = current_app.config['ALI_NAME']
template_code = current_app.config['ALI_CODE']

acs_client = connection(current_app.config['ALI_ID'], current_app.config['ALI_KEY'],
                        current_app.config['ALI_REGION'])

region_provider.modify_point(current_app.config['ALI_PRODUCT_NAME'],
                             current_app.config['ALI_REGION'],
                             current_app.config['ALI_DOMAIN'])


def send_sms(phone_numbers, param=None):
    __business_id = uuid.uuid1()
    sms_request = SendSmsRequest()
    sms_request.set_template_code(template_code)

    # 短信模板变量参数
    if param is not None:
        sms_request.set_template_param(param)

    # 设置业务请求流水号,必填。
    sms_request.set_out_id(__business_id)
    # 短信签名
    sms_request.set_sign_name(sign_name)

    sms_request.set_phone_numbers(phone_numbers)

    sms_response = acs_client.do_action_with_exception(sms_request)

    return sms_response

使用

    params = {'number': 1024} # 这里的"number"一定要是审核后模板里面的那个变量。
    message = send_sms('13111111111', params)
Tags: None
Archives QR Code
QR Code for this page
Tipping QR Code
Leave a Comment

已有 2 条评论
  1. emmm,标题是不是应该是阿里云短信服务

    1. @Ryoma短信云服务就是阿里大鱼呀.