def sendTemplateSMS(self, to,datas,tempId): self.accAuth() nowdate = datetime.datetime.now() self.Batch = nowdate.strftime("%Y%m%d%H%M%S") #生成sig signature = self.AccountSid + self.AccountToken + self.Batch; sig = hashlib.md5(signature.encode('utf-8')).hexdigest() sig = sig.upper() #拼接URL url = "https://"+self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/Accounts/" + self.AccountSid + "/SMS/TemplateSMS?sig=" + sig #生成auth src = self.AccountSid + ":" + self.Batch; auth = base64.encodestring(src.encode('utf-8')).strip() req = request.Request(url) self.setHttpHeader(req) req.add_header("Authorization", auth) #创建包体 b='' for a in datas: b+='<data>%s</data>'%(a) body ='<?xml version="1.0" encoding="utf-8"?><SubAccount><datas>'+b+'</datas><to>%s</to><templateId>%s</templateId><appId>%s</appId>\ </SubAccount>\ '%(to, tempId,self.AppId) if self.BodyType == 'json': # if this model is Json ..then do next code b='[' for a in datas: b+='"%s",'%(a) b+=']' body = '''{"to": "%s", "datas": %s, "templateId": "%s", "appId": "%s"}'''%(to,b,tempId,self.AppId) req.data = body.encode() data = "" try: res = request.urlopen(req); data = res.read() res.close() if self.BodyType=='json': #json格式 locations = json.loads(data) else: #xml格式 xtj=xmltojson() locations=xtj.main(data) if self.Iflog: self.log(url,body,data) return locations except Exception as error: if self.Iflog: self.log(url,body,data) return {'172001':'网络错误'}
def MediaFileUpload (self,filename,body): self.accAuth() nowdate = datetime.datetime.now() self.Batch = nowdate.strftime("%Y%m%d%H%M%S") #生成sig signature = self.AccountSid + self.AccountToken + self.Batch; sig = md5.new(signature).hexdigest().upper() #拼接URL url = "https://"+self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/Accounts/" + self.AccountSid + "/Calls/MediaFileUpload?sig=" + sig + "&appid=" + self.AppId + "&filename=" + filename #生成auth src = self.AccountSid + ":" + self.Batch; auth = base64.encodestring(src).strip() req = request.Request(url) req.add_header("Authorization", auth) if self.BodyType == 'json': req.add_header("Accept", "application/json") req.add_header("Content-Type", "application/octet-stream") else: req.add_header("Accept", "application/xml") req.add_header("Content-Type", "application/octet-stream") #创建包体 req.data(body) try: res = request.urlopen(req); data = res.read() res.close() if self.BodyType=='json': #json格式 locations = json.loads(data) else: #xml格式 xtj=xmltojson() locations=xtj.main(data) if self.Iflog: self.log(url,body,data) return locations except Exception as error: if self.Iflog: self.log(url,body,data) return {'172001':'网络错误'}
def CreateSubAccount(self, friendlyName): self.accAuth() nowdate = datetime.datetime.now() self.Batch = nowdate.strftime("%Y%m%d%H%M%S") #生成sig signature = self.AccountSid + self.AccountToken + self.Batch; sig = hashlib.md5(signature.encode('utf-8')).hexdigest() sig = sig.upper() #拼接URL url = "https://"+self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/Accounts/" + self.AccountSid + "/SubAccounts?sig=" + sig #生成auth src = self.AccountSid + ":" + self.Batch; auth = base64.encodestring(src).strip() req = request.Request(url) self.setHttpHeader(req) req.add_header("Authorization", auth) #xml格式 body ='''<?xml version="1.0" encoding="utf-8"?><SubAccount><appId>%s</appId>\ <friendlyName>%s</friendlyName>\ </SubAccount>\ '''%(self.AppId, friendlyName) if self.BodyType == 'json': #json格式 body = '''{"friendlyName": "%s", "appId": "%s"}'''%(friendlyName,self.AppId) data='' req.data(body) try: res = request.urlopen(req); data = res.read() res.close() if self.BodyType=='json': #json格式 locations = json.loads(data) else: #xml格式 xtj=xmltojson() locations=xtj.main(data) if self.Iflog: self.log(url,body,data) return locations except Exception as error: if self.Iflog: self.log(url,body,data) return {'172001':'网络错误'}
def QueryCallState (self,callid,action): self.accAuth() nowdate = datetime.datetime.now() self.Batch = nowdate.strftime("%Y%m%d%H%M%S") #生成sig signature = self.AccountSid + self.AccountToken + self.Batch; sig = md5.new(signature).hexdigest().upper() #拼接URL url = "https://"+self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/Accounts/" + self.AccountSid + "/ivr/call?sig=" + sig + "&callid=" + callid #生成auth src = self.AccountSid + ":" + self.Batch; auth = base64.encodestring(src).strip() req = request.Request(url) self.setHttpHeader(req) req.add_header("Authorization", auth) #创建包体 body ='''<?xml version="1.0" encoding="utf-8"?><Request>\ <Appid>%s</Appid><QueryCallState callid="%s" action="%s"/>\ </Request>\ '''%(self.AppId,callid,action) if self.BodyType == 'json': # if this model is Json ..then do next code body = '''{"Appid":"%s","QueryCallState":{"callid":"%s","action":"%s"}}'''%(self.AppId,callid,action) req.data(body) data='' try: res = request.urlopen(req); data = res.read() res.close() if self.BodyType=='json': #json格式 locations = json.loads(data) else: #xml格式 xtj=xmltojson() locations=xtj.main(data) if self.Iflog: self.log(url,body,data) return locations except Exception as error: if self.Iflog: self.log(url,body,data) return {'172001':'网络错误'}
def billRecords(self,date,keywords): self.accAuth() nowdate = datetime.datetime.now() self.Batch = nowdate.strftime("%Y%m%d%H%M%S") #生成sig signature = self.AccountSid + self.AccountToken + self.Batch; sig = md5.new(signature).hexdigest().upper() #拼接URL url = "https://"+self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/Accounts/" + self.AccountSid + "/BillRecords?sig=" + sig #生成auth src = self.AccountSid + ":" + self.Batch; auth = base64.encodestring(src).strip() req = request.Request(url) self.setHttpHeader(req) req.add_header("Authorization", auth) #创建包体 body ='''<?xml version="1.0" encoding="utf-8"?><BillRecords>\ <appId>%s</appId><date>%s</date><keywords>%s</keywords>\ </BillRecords>\ '''%(self.AppId,date,keywords) if self.BodyType == 'json': # if this model is Json ..then do next code body = '''{"appId": "%s", "date": "%s","keywords": "%s"}'''%(self.AppId,date,keywords) req.data(body) data='' try: res = request.urlopen(req); data = res.read() res.close() if self.BodyType=='json': #json格式 locations = json.loads(data) else: #xml格式 xtj=xmltojson() locations=xtj.main(data) if self.Iflog: self.log(url,body,data) return locations except Exception as error: if self.Iflog: self.log(url,body,data) return {'172001':'网络错误'}
def voiceVerify(self,verifyCode,playTimes,to,displayNum,respUrl,lang,userData): self.accAuth() nowdate = datetime.datetime.now() self.Batch = nowdate.strftime("%Y%m%d%H%M%S") #生成sig signature = self.AccountSid + self.AccountToken + self.Batch; sig = md5.new(signature).hexdigest().upper() #拼接URL url = "https://"+self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/Accounts/" + self.AccountSid + "/Calls/VoiceVerify?sig=" + sig #生成auth src = self.AccountSid + ":" + self.Batch; auth = base64.encodestring(src).strip() req = request.Request(url) self.setHttpHeader(req) req.add_header("Authorization", auth) #创建包体 body ='''<?xml version="1.0" encoding="utf-8"?><VoiceVerify>\ <appId>%s</appId><verifyCode>%s</verifyCode><playTimes>%s</playTimes><to>%s</to><respUrl>%s</respUrl>\ <displayNum>%s</displayNum><lang>%s</lang><userData>%s</userData></VoiceVerify>\ '''%(self.AppId,verifyCode,playTimes,to,respUrl,displayNum,lang,userData) if self.BodyType == 'json': # if this model is Json ..then do next code body = '''{"appId": "%s", "verifyCode": "%s","playTimes": "%s","to": "%s","respUrl": "%s","displayNum": "%s","lang": "%s","userData": "%s"}'''%(self.AppId,verifyCode,playTimes,to,respUrl,displayNum,lang,userData) req.data(body) data='' try: res = request.urlopen(req); data = res.read() res.close() if self.BodyType=='json': #json格式 locations = json.loads(data) else: #xml格式 xtj=xmltojson() locations=xtj.main(data) if self.Iflog: self.log(url,body,data) return locations except Exception as error: if self.Iflog: self.log(url,body,data) return {'172001':'网络错误'}
def landingCall(self,to,mediaName,mediaTxt,displayNum,playTimes,respUrl,userData,maxCallTime,speed,volume,pitch,bgsound): self.accAuth() nowdate = datetime.datetime.now() self.Batch = nowdate.strftime("%Y%m%d%H%M%S") #生成sig signature = self.AccountSid + self.AccountToken + self.Batch; sig = md5.new(signature).hexdigest().upper() #拼接URL url = "https://"+self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/Accounts/" + self.AccountSid + "/Calls/LandingCalls?sig=" + sig #生成auth src = self.AccountSid + ":" + self.Batch; auth = base64.encodestring(src).strip() req = request.Request(url) self.setHttpHeader(req) req.add_header("Authorization", auth) #创建包体 body ='''<?xml version="1.0" encoding="utf-8"?><LandingCall>\ <to>%s</to><mediaName>%s</mediaName><mediaTxt>%s</mediaTxt><appId>%s</appId><displayNum>%s</displayNum>\ <playTimes>%s</playTimes><respUrl>%s</respUrl><userData>%s</userData><maxCallTime>%s</maxCallTime><speed>%s</speed> <volume>%s</volume><pitch>%s</pitch><bgsound>%s</bgsound></LandingCall>\ '''%(to, mediaName,mediaTxt,self.AppId,displayNum,playTimes,respUrl,userData,maxCallTime,speed,volume,pitch,bgsound) if self.BodyType == 'json': body = '''{"to": "%s", "mediaName": "%s","mediaTxt": "%s","appId": "%s","displayNum": "%s","playTimes": "%s","respUrl": "%s","userData": "%s","maxCallTime": "%s","speed": "%s","volume": "%s","pitch": "%s","bgsound": "%s"}'''%(to, mediaName,mediaTxt,self.AppId,displayNum,playTimes,respUrl,userData,maxCallTime,speed,volume,pitch,bgsound) req.data(body) data='' try: res = request.urlopen(req); data = res.read() res.close() if self.BodyType=='json': #json格式 locations = json.loads(data) else: #xml格式 xtj=xmltojson() locations=xtj.main(data) if self.Iflog: self.log(url,body,data) return locations except Exception as error: if self.Iflog: self.log(url,body,data) return {'172001':'网络错误'}
def ivrDial(self,number,userdata,record): self.accAuth() nowdate = datetime.datetime.now() self.Batch = nowdate.strftime("%Y%m%d%H%M%S") #生成sig signature = self.AccountSid + self.AccountToken + self.Batch; sig = md5.new(signature).hexdigest().upper() #拼接URL url = "https://"+self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/Accounts/" + self.AccountSid + "/ivr/dial?sig=" + sig #生成auth src = self.AccountSid + ":" + self.Batch; auth = base64.encodestring(src).strip() req = request.Request(url) req.add_header("Accept", "application/xml") req.add_header("Content-Type", "application/xml;charset=utf-8") req.add_header("Authorization", auth) #创建包体 body ='''<?xml version="1.0" encoding="utf-8"?> <Request> <Appid>%s</Appid> <Dial number="%s" userdata="%s" record="%s"></Dial> </Request> '''%(self.AppId,number,userdata,record) req.data(body) data='' try: res = request.urlopen(req); data = res.read() res.close() xtj=xmltojson() locations=xtj.main(data) if self.Iflog: self.log(url,body,data) return locations except Exception as error: if self.Iflog: self.log(url,body,data) return {'172001':'网络错误'}
def CallResult(self,callSid): self.accAuth() nowdate = datetime.datetime.now() self.Batch = nowdate.strftime("%Y%m%d%H%M%S") #生成sig signature = self.AccountSid + self.AccountToken + self.Batch; sig = md5.new(signature).hexdigest().upper() #拼接URL url = "https://"+self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/Accounts/" + self.AccountSid + "/CallResult?sig=" + sig + "&callsid=" + callSid #生成auth src = self.AccountSid + ":" + self.Batch; auth = base64.encodestring(src).strip() req = request.Request(url) self.setHttpHeader(req) body='' req.add_header("Authorization", auth) data='' try: res = request.urlopen(req); data = res.read() res.close() if self.BodyType=='json': #json格式 locations = json.loads(data) else: #xml格式 xtj=xmltojson() locations=xtj.main(data) if self.Iflog: self.log(url,body,data) return locations except Exception as error: if self.Iflog: self.log(url,body,data) return {'172001':'网络错误'}