本文实现了使用阿里云《函数计算》+《短信服务》发送短信的简单功能,官方短信服务SDK功能全面,但较为冗杂,二爷精简了官方SDK,实现了一段代码(单文件)发送短信的功能。
阅读本文,你需要先了解阿里云函数计算、阿里云短信服务的基本常识。
试用本代码,需要预先在短信服务中申请短信签名、短信模板。
# -*- coding: utf-8 -*-
import uuid
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import RpcRequest
# 短信发送单文件轻量版(阿里云函数计算+Python3环境测试通过),Created on 2018-06-30
#注意不要更改
REGION = "cn-hangzhou"
PRODUCT_NAME = "Dysmsapi"
DOMAIN = "dysmsapi.aliyuncs.com"
ACCESS_KEY_ID = "您在阿里云申请的ACCESS_KEY_ID"
ACCESS_KEY_SECRET = "您在阿里云申请的ACCESS_KEY_SECRET"
acs_client = AcsClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET, REGION)
class SendSmsRequest(RpcRequest):
def __init__(self):
RpcRequest.__init__(self, 'Dysmsapi', '2017-05-25', 'SendSms')
def get_TemplateCode(self):
return self.get_query_params().get('TemplateCode')
def set_TemplateCode(self,TemplateCode):
self.add_query_param('TemplateCode',TemplateCode)
def get_PhoneNumbers(self):
return self.get_query_params().get('PhoneNumbers')
def set_PhoneNumbers(self,PhoneNumbers):
self.add_query_param('PhoneNumbers',PhoneNumbers)
def get_SignName(self):
return self.get_query_params().get('SignName')
def set_SignName(self,SignName):
self.add_query_param('SignName',SignName)
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_TemplateParam(self,TemplateParam):
self.add_query_param('TemplateParam',TemplateParam)
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_OutId(self,OutId):
self.add_query_param('OutId',OutId)
def send_sms(business_id, phone_numbers, sign_name, template_code, template_param=None):
smsRequest = SendSmsRequest()
smsRequest.set_OutId(business_id) #设置业务请求流水号,必填
smsRequest.set_PhoneNumbers(phone_numbers) #短信发送的号码列表,必填
smsRequest.set_SignName(sign_name) #短信签名
smsRequest.set_TemplateCode(template_code) #申请的短信模板编码,必填
if template_param is not None:
smsRequest.set_TemplateParam(template_param)
#短信模板变量参数
smsResponse = acs_client.do_action_with_exception(smsRequest) #调用短信发送接口,返回json
# TODO 业务处理
return smsResponse
def handler(event, context):
__business_id = uuid.uuid4()
params='{"product":"糕点"}'
return send_sms(__business_id,"13000000000","您的短信签名","您申请的短信模板",params)
执行结果数据为json格式,发送成功返回的数据:
{
"Message": "OK",
"RequestId": "6C801F2D-8DC4-4F39-9CBD-F3DC0C28C21B",
"BizId": "221005030328791139^0",
"Code": "OK"
}
发送失败返回的部分数据示例(手机号码错误):
{
"Message": "13135656invalid mobile number",
"RequestId": "45614ADE-587C-4E78-97A7-71DAA2582403",
"Code": "isv.MOBILE_NUMBER_ILLEGAL"
}
目前阿里云官方提供了更简单的代码,使用是 CommonRequest替代了 RpcRequest,代码量更减少了,推荐使用官方的方法。