06 third party SMS package

  Ronglian cloud communications messaging platform

  Register an account

  Management application management module >> >> >> editing application to create applications >> check SMS verification code

  Installation request module

pip install requests

  Request parameter

POST : https://app.cloopen.com:8883/2013-12-26/Accounts/{accountSid}/SMS/TemplateSMS?sig={SigParameter}

  Request Header

parameter Types of Are Required Parameter Description
accountSid String required Account ID
SigParameter String required Signature [Account token Token + ID + time ( 'YmdHMS') MD5 value]

  Request header field

parameter Types of Are Required Parameter Description
Accept String required The client receives the response data format
Content-Type String required Types of
Content-Length String required  
Authorization String required Carry parameters

  Request packet body

parameter Types of Are Required Parameter Description
to String required "Phone 1, Phone 2, Phone 3"
appId String required Application ID
templateId String required  
dates String Optional Content data layer node, if there is no template variables, this parameter is not passed
data String Optional Content data that replaces the template {number}

  In response to this step in response only that the customer's request to send SMS success, does not indicate that the channel has been sent SMS messages successfully. ( Status code )

parameter Types of Are Required Parameter Description
statusCode String required Request status code, ranging 000000 (successful)
smsMessageSid String required SMS unique identifier
dateCreated String required Created SMS

Block

  shortMsg.py

 1 import hashlib, base64, requests, json
 2 from datetime import datetime
 3 
 4 class YunTongXin:
 5   __base_url = 'https://app.cloopen.com:8883/2013-12-26/Accounts/{}/SMS/TemplateSMS?sig={}'
 6 
 7   # 初始化代码
 8   def __init__(self, accid='', appid='', appkey='', temid='', temp_motice='45分钟'):
 9       self.accid = accid
10       self.appid = appid
11       self.appkey = appkey
12       self.temid = temid
13       self.temp_motice = temp_motice
14 
15   # md5编码
16   def __md5(self, raw):
17       md5 = hashlib.md5()
18       md5.update(raw.encode('utf-8'))
19       return md5.hexdigest()
20 
21   # 获取格式化时间
22   def __get_format_time(self):
23       return str(datetime.now().strftime('%Y%m%d%H%M%S'))
24 
25   # base64编码
26   def __get_base64_code(self, raw):
27       st = raw.encode()
28       return base64.b64encode(st).decode('utf-8')
29 
30   # 请求包头
31   def __request_url(self, stamp):
32       sig = self.__md5(self.accid + self.appkey + stamp).upper()  # 生成签名
33       return self.__base_url.format(self.accid, sig)  # https 请求地址
34 
35   # 请求包头字段
36   def __request_header(self, stamp):
37       authorization = self.__get_base64_code(self.accid + ':' + stamp)
38       return {
39           'Authorization': authorization, 'Accept': 'application/json;',
40           'Content-Type': 'application/json;charset=utf-8;'}
41 
42   # 请求包体
43   def __request_body(self, phone, code):
44       return {
45           "to": ','.join(phone),
46           "appId": self.appid,
47           "templateId": self.temid,
48           "datas": [code, self.temp_motice]
49       }
50 
51   # 返回结构化结果
52   def __ajaxReturn(self, code=92000, msg='', data=[]):
53       dict = {"code": code, "msg": msg, "data": data}
54       return json.dumps(dict)
55 
56   # 发送短信
57   def send(self, phone=[], code=''):
58       stamp = self.__get_format_time()  # 获取当前格式化时间
59       url = self.__request_url(stamp)  # 请求包地址
60       headers = self.__request_header(stamp)  # 请求包头字段
61       data = self.__request_body(phone, code)
62       obj = requests.post(url, headers=headers, json=data)
63       dict = json.loads(obj.text)
64       if dict.get('statusCode') == '000000':
65           return self.__ajaxReturn(msg="发送成功", data={})
66       else:
67           return self.__ajaxReturn(code=92001, msg='发送失败', data={"code": dict.get('statusCode')})

  调用

 1 from shortMsg import YunTongXin
 2 accid = '' # 账号id
 3 appid = '' # 应用id
 4 appkey = 'ae007ba417e24833acc709d8e2acdb65' # key
 5 temid = '1' # 默认模版
 6 temp_motice = '30分钟' # 参数
 7 phone = ['电话1','电话2'] # 电话号码 
 8 code = '' # 短信验证码
 9 # 实例化
10 yun = YunTongXin(accid=accid,appid=appid,appkey=appkey,temid=temid,temp_motice=temp_motice)
11 # 发送短信
12 yun.send(phone, code)

 

Guess you like

Origin www.cnblogs.com/a2534786642/p/11040835.html