python阿⾥云发短信教程详细(2)-Python3⼀、准备⼯作
1.安装阿⾥云SDK
pip install aliyun-python-sdk-core-v3
1
2.购买短信套餐
3.新建签名并等待审核通过
4.新建短信模板并等待审核通过
审核通过后,记下**【模版CODE】(参数2)**,程序中会⽤到。
5.获取ACCESS_KEY_ID
1)进⼊RAM访问控制
点击左上⾓的菜单栏(三个横线的图标);【产品与服务】-【监控管理】-【访问控制】
2)新建⽤户
【登录名称】、【显⽰名称】按规则输⼊即可,勾选上【编程访问】,点击确定
再次点击左侧菜单栏【⽤户】,页⾯将刷新
点击对应的【⽤户登录名称/显⽰名称】,进⼊后可创建Access Key,并添加授权(需要的两项授权如下图所⽰),记下【ACCESS_KEY_ID】(参数3)、【ACCESS_KEY_SECRET】(参数4),等会会⽤到。
注:⼀定要授权
⼆、代码
#!/usr/bin/env python
# coding=utf-8
import uuid
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.profile import region_provider
quest import RpcRequest
# 注意:不要更改
REGION = "cn-hangzhou"
PRODUCT_NAME = "Dysmsapi"
DOMAIN = "dysmsapi.aliyuncs"
# 注意:更改为⾃⼰的参数,参数从上⾯的教程
sign_name = "参数1"# 短信签名
template_code = "参数2"# 模板CODE
ACCESS_KEY_ID = "参数3"# ACCESS_KEY_ID
ACCESS_KEY_SECRET = "参数4"# ACCESS_KEY_ID
acs_client = AcsClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET, REGION)
region_provider.add_endpoint(PRODUCT_NAME, REGION, DOMAIN)
class SendSmsRequest(RpcRequest):
def__init__(self):
RpcRequest.__init__(self, 'Dysmsapi', '2017-05-25', 'SendSms')
def get_TemplateCode(self):
_query_params().get('TemplateCode')
def set_TemplateCode(self,TemplateCode):
self.add_query_param('TemplateCode',TemplateCode)
def get_PhoneNumbers(self):
_query_params().get('PhoneNumbers')
def set_PhoneNumbers(self,PhoneNumbers):
self.add_query_param('PhoneNumbers',PhoneNumbers)
def get_SignName(self):
_query_params().get('SignName')
def set_SignName(self,SignName):
self.add_query_param('SignName',SignName)
def get_ResourceOwnerAccount(self):
_query_params().get('ResourceOwnerAccount')
def set_ResourceOwnerAccount(self,ResourceOwnerAccount):
self.add_query_param('ResourceOwnerAccount',ResourceOwnerAccount)
def get_TemplateParam(self):
_query_params().get('TemplateParam')
def set_TemplateParam(self,TemplateParam):
self.add_query_param('TemplateParam',TemplateParam)
短信套餐def get_ResourceOwnerId(self):
_query_params().get('ResourceOwnerId')
def set_ResourceOwnerId(self,ResourceOwnerId):
self.add_query_param('ResourceOwnerId',ResourceOwnerId)
def get_OwnerId(self):
_query_params().get('OwnerId')
def set_OwnerId(self,OwnerId):
self.add_query_param('OwnerId',OwnerId)
def get_SmsUpExtendCode(self):
_query_params().get('SmsUpExtendCode')
def set_SmsUpExtendCode(self,SmsUpExtendCode):
self.add_query_param('SmsUpExtendCode',SmsUpExtendCode)
def get_OutId(self):
_query_params().get('OutId')
def set_OutId(self,OutId):
self.add_query_param('OutId',OutId)
def send_sms(phone_numbers, template_param=None):
business_id = uuid.uuid4()
sms_request = SendSmsRequest()
sms_request.set_TemplateCode(template_code) # 短信模板变量参数
if template_param is not None:
sms_request.set_TemplateParam(template_param)
sms_request.set_OutId(business_id) # 设置业务请求流⽔号,必填。
sms_request.set_SignName(sign_name) # 短信签名
sms_request.set_PhoneNumbers(phone_numbers) # 短信发送的号码列表,必填。
sms_response = acs_client.do_action_with_exception(sms_request) # 调⽤短信发送接⼝,返回json return sms_response
if__name__ == "__main__":
result = send_sms("需发送的⼿机号", "{'code':'需要发送的验证码'}")
print(str(result, encoding='utf-8'))
三、运⾏及结果
短信发送成功!
发布评论