阿里云短信python版本API开发示例包: https://help.aliyun.com/document_detail/55491.html?spm=a2c4g.11186631.6.578.161d5a6dfJUt8X
下载SDK python短信发送包:https://help.aliyun.com/document_detail/55359.html?spm=a2c4g.11186623.2.16.39c06e44yEn8JH
然后就可以安装python包:
├── aliyunsdkdybaseapi #原aliyunsdkdybaseapi库
│ └── request
├── aliyunsdkdysmsapi #原aliyunsdkdysmsapi库
│ ├── init.py
│ └── request
├── const.py #配置key、secret等参数
├── demo_sms_api.py #通过mns获取队列回执
├── demo_sms_query.py #短信发送状态查询
├── demo_sms_send.py #短信发送
├── mns_python_sdk #原mns_python_sdk
│ ├── bin
│ ├── mns
│ ├── sample
│ ├── sample.cfg
│ ├── sample.py
│ └── setup.py
└── setup.py
示例脚本:
# -*- coding: utf-8 -*-
import sys
from aliyunsdkdysmsapi.request.v20170525 import SendSmsRequest
from aliyunsdkdysmsapi.request.v20170525 import QuerySendDetailsRequest
from aliyunsdkcore.client import AcsClient
import uuid
import datetime
import time
from aliyunsdkcore.profile import region_provider
from aliyunsdkcore.http import method_type as MT
from aliyunsdkcore.http import format_type as FT
import const
import os, argparse, glob, os.path,time,re
"""
短信业务调用接口示例,版本号:v20170525
Created on 2017-06-12
"""
try:
reload(sys)
sys.setdefaultencoding('utf8')
except NameError:
pass
except Exception as err:
raise err
# 注意:不要更改
REGION = "cn-hangzhou"
PRODUCT_NAME = "Dysmsapi"
DOMAIN = "dysmsapi.aliyuncs.com"
acs_client = AcsClient(const.ACCESS_KEY_ID, const.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):
smsRequest = SendSmsRequest.SendSmsRequest()
# 申请的短信模板编码,必填
smsRequest.set_TemplateCode(template_code)
# 短信模板变量参数
if template_param is not None:
smsRequest.set_TemplateParam(template_param)
# 设置业务请求流水号,必填。
smsRequest.set_OutId(business_id)
# 短信签名
smsRequest.set_SignName(sign_name)
# 数据提交方式
# smsRequest.set_method(MT.POST)
# 数据提交格式
# smsRequest.set_accept_format(FT.JSON)
# 短信发送的号码列表,必填。
smsRequest.set_PhoneNumbers(phone_numbers)
# 调用短信发送接口,返回json
smsResponse = acs_client.do_action_with_exception(smsRequest)
# TODO 业务处理
return smsResponse
if __name__ == '__main__':
__business_id = uuid.uuid1()
#print(__business_id)
#params = "{\"code\":\"12345\",\"product\":\"云通信\"}"
#params = "{\"code\":\"20181204\"}"
#params = u'{"name":"wqb","code":"12345678","address":"bz","phone":"13000000000"}'
#print(send_sms(__business_id, "18510686562", "组学大讲堂", "SMS_137427054", params))
parser = argparse.ArgumentParser(description='This script use send sms auto')
parser.add_argument('-i', '--input', dest='input', required=True, help='input use info')
parser.add_argument('-t', '--SMS', dest='SMS', required=True, help='input template template SMS code')
parser.add_argument('-p', '--params', dest='params', required=True,help='Input template param "{\"code\":\"12345\",\"product\":\"云通信\"}"')
args = parser.parse_args()
###################################################################
uf=open(args.input,'r')
for i in uf:
i=i.strip()
tmp=re.split('\t',i)
#print(tmp)
if not tmp[3] == "NULL":
print(send_sms(tmp[0], tmp[3], "组学大讲堂", args.SMS, args.params))
time.sleep(1)
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!