-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsms.py
206 lines (189 loc) · 7.42 KB
/
sms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# -*- coding: utf-8 -*-
from qiniu import http
import json
class Sms(object):
def __init__(self, auth):
self.auth = auth
self.server = 'https://sms.qiniuapi.com'
def createSignature(self, signature, source, pics=None):
"""
*创建签名
*signature: string类型,必填,【长度限制8个字符内】超过长度会报错
*source: string类型,必填,申请签名时必须指定签名来源。取值范围为:
enterprises_and_institutions 企事业单位的全称或简称
website 工信部备案网站的全称或简称
app APP应用的全称或简称
public_number_or_small_program 公众号或小程序的全称或简称
store_name 电商平台店铺名的全称或简称
trade_name 商标名的全称或简称
*pics: 签名对应的资质证明图片进行 base64 编码格式转换后的字符串
* @ return: 类型array
{
"signature_id": < signature_id >
}
"""
req = {}
req['signature'] = signature
req['source'] = source
if pics:
req['pics'] = pics
body = json.dumps(req)
url = '{0}/v1/signature'.format(self.server)
return self.__post(url, body)
def querySignature(self, audit_status=None, page=1, page_size=20):
"""
查询签名
* audit_status: 审核状态 string 类型,可选,取值范围为: "passed"(通过), "rejected"(未通过), "reviewing"(审核中)
* page:页码 int 类型,
* page_size: 分页大小 int 类型,可选, 默认为20
*@return: 类型array {
"items": [{
"id": string,
"signature": string,
"source": string,
"audit_status": string,
"reject_reason": string,
"created_at": int64,
"updated_at": int64
}...],
"total": int,
"page": int,
"page_size": int,
}
"""
url = '{}/v1/signature'.format(self.server)
if audit_status:
url = '{}?audit_status={}&page={}&page_size={}'.format(url, audit_status, page, page_size)
else:
url = '{}?page={}&page_size={}'.format(url, page, page_size)
return self.__get(url)
def updateSignature(self, id, signature):
"""
编辑签名
* id 签名id : string 类型,必填,
* signature: string 类型,必填,
request 类型array {
"signature": string
}
:return:
"""
url = '{}/v1/signature/{}'.format(self.server, id)
req = {}
req['signature'] = signature
body = json.dumps(req)
return self.__put(url, body)
def deleteSignature(self, id):
"""
删除辑签名
* id 签名id : string 类型,必填,
* @retrun : 请求成功 HTTP 状态码为 200
"""
url = '{}/v1/signature/{}'.format(self.server, id)
return self.__delete(url)
def createTemplate(self, name, template, type, description, signature_id):
"""
创建模版
:param name: 模板名称 string 类型 ,必填
:param template: 模板内容 string 类型,必填
:param type: 模板类型 string 类型,必填,
取值范围为: notification (通知类短信), verification (验证码短信), marketing (营销类短信)
:param description: 申请理由简述 string 类型,必填
:param signature_id: 已经审核通过的签名 string 类型,必填
:return: 类型 array {
"template_id": string
}
"""
url = '{}/v1/template'.format(self.server)
req = {}
req['name'] = name
req['template'] = template
req['type'] = type
req['description'] = description
req['signature_id'] = signature_id
body = json.dumps(req)
return self.__post(url, body)
def queryTemplate(self, audit_status, page=1, page_size=20):
"""
查询模版
:param audit_status: 审核状态, 取值范围为: passed (通过), rejected (未通过), reviewing (审核中)
:param page: 页码。默认为 1
:param page_size: 分页大小。默认为 20
:return:{
"items": [{
"id": string,
"name": string,
"template": string,
"audit_status": string,
"reject_reason": string,
"type": string,
"signature_id": string, // 模版绑定的签名ID
"signature_text": string, // 模版绑定的签名内容
"created_at": int64,
"updated_at": int64
}...],
"total": int,
"page": int,
"page_size": int
}
"""
url = '{}/v1/template'.format(self.server)
if audit_status:
url = '{}?audit_status={}&page={}&page_size={}'.format(url, audit_status, page, page_size)
else:
url = '{}?page={}&page_size={}'.format(url, page, page_size)
return self.__get(url)
def updateTemplate(self, id, name, template, description, signature_id):
"""
更新模版
:param id: template_id
:param name: 模板名称 string 类型 ,必填
:param template: 模板内容 string 类型,必填
:param description: 申请理由简述 string 类型,必填
:param signature_id: 已经审核通过的签名 string 类型,必填
:return: 请求成功 HTTP 状态码为 200
"""
url = '{}/v1/template/{}'.format(self.server, id)
req = {}
req['name'] = name
req['template'] = template
req['description'] = description
req['signature_id'] = signature_id
body = json.dumps(req)
return self.__put(url, body)
def deleteTemplate(self, id):
"""
删除模版
:param id: template_id
:return: 请求成功 HTTP 状态码为 200
"""
url = '{}/v1/template/{}'.format(self.server, id)
return self.__delete(url)
def sendMessage(self, template_id, mobiles, parameters):
"""
发送短信
:param template_id: 模板 ID
:param mobiles: 手机号
:param parameters: 自定义魔法变量,变量设置在创建模板时,参数template指定
:return:{
"job_id": string
}
"""
url = '{}/v1/message'.format(self.server)
req = {}
req['template_id'] = template_id
req['mobiles'] = mobiles
req['parameters'] = parameters
body = json.dumps(req)
return self.__post(url, body)
def __post(self, url, data=None):
headers = {'Content-Type': 'application/json'}
return http._post_with_qiniu_mac_and_headers(url, data, self.auth, headers)
def __get(self, url, params=None):
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
return http._get_with_qiniu_mac_and_headers(url, params, self.auth, headers)
def __put(self, url, data=None):
headers = {'Content-Type': 'application/json'}
return http._put_with_qiniu_mac_and_headers(url, data, self.auth, headers)
def __delete(self, url, data=None):
headers = {'Content-Type': 'application/json'}
return http._delete_with_qiniu_mac_and_headers(url, data, self.auth, headers)