阿里云发送短信,0.045/条,具体步骤如下:
一、接入短信发送API(SendSms)
1、实名认证阿里云账号
2、获得阿里云访问秘钥:ACCESS_KEY_ID 和 ACCESS_KEY_SECRET,
3、申请短信签名和短信模板:SignName 和 TemplateCode,
二、下载安装阿里云SDK——python
详情地址:https://help.aliyun.com/document_detail/53090.html?spm=5176.164075.850376.30.61f7f93fHiw8Op
二、代码编写接入
#!/usr/bin/env python # -*- coding: utf-8 -*- ''' Send a message to the specific phone using Aliyun SMS. ''' from aliyunsdkcore.client import AcsClient from aliyunsdkcore.profile import region_provider import SendSmsRequest import uuid # Do NOT change the REGION. REGION = "cn-hangzhou" PRODUCT_NAME = "Dysmsapi" DOMAIN = "dysmsapi.aliyuncs.com" # ACCESS_KEY_ID/ACCESS_KEY_SECRET. ACCESS_KEY_ID = "LTAIdzPgWfXxYNVH" ACCESS_KEY_SECRET = "6iFhEf0kxh80Xwm6zskR0b2d0d7hrt" # create a client acs_client = AcsClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET, REGION) region_provider.add_endpoint(PRODUCT_NAME, REGION, DOMAIN) def send_sms(business_id, phone_numbers, sign_name, template_code, template_param=None): # The obj.实例对象 smsRequest = SendSmsRequest.SendSmsRequest() # The Template Code is necessary.短信模板编码 smsRequest.set_TemplateCode(template_code) # The Template parameters are necessary.短信模板参数 if template_param is not None: smsRequest.set_TemplateParam(template_param) # The request number is necessary.业务请求流水号 smsRequest.set_OutId(business_id) # The sign name is necessary.短信签名 smsRequest.set_SignName(sign_name) # The destnation phone number is necessary.短信发送的号码列表 smsRequest.set_PhoneNumbers(phone_numbers) # Call the sms sending interface. Return json.调用短信发送接口,返回json smsResponse = acs_client.do_action_with_exception(smsRequest) return smsResponse # To list SHANE_NAME="python发短信测试" SHANE_NO="15056925802" # Main if __name__ == '__main__': __business_id = uuid.uuid1() print(__business_id) code = '123456' params="{\"code\":\"%s\"}" % (code) print(params) print(send_sms(__business_id, SHANE_NO, "王冬雪", "SMS_130920682", params))
其中使用到的配置文件:
# Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. from aliyunsdkcore.request import RpcRequest 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)
浙公网安备 33010602011771号