def getSubAccounts(self, startNo,offset): 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 + "/GetSubAccounts?sig=" + sig #生成auth src = self.AccountSid + ":" + self.Batch; auth = base64.encodestring(src).strip() # req = urllib2.Request(url) # self.setHttpHeader(req) # req.add_header("Authorization", auth) headers = {'Authorization':auth} #xml格式 body ='''<?xml version="1.0" encoding="utf-8"?><SubAccount><appId>%s</appId>\ <startNo>%s</startNo><offset>%s</offset>\ </SubAccount>\ '''%(self.AppId, startNo, offset) if self.BodyType == 'json': #json格式 body = '''{"appId": "%s", "startNo": "%s", "offset": "%s"}'''%(self.AppId,startNo,offset) data='' # print(body) # req.add_data(body) try: # res = urllib2.urlopen(req); # data = res.read() # res.close() res = requests.post(url,data=body,headers=headers) s = res.text # print(s) 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 = md5.new(signature).hexdigest().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 = urllib2.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='' # print(body,'bodybodybodybody') req.add_data(body) try: res = urllib2.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 = urllib2.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.add_data(body) data='' try: res = urllib2.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): 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 = urllib2.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></VoiceVerify>\ '''%(self.AppId,verifyCode,playTimes,to,respUrl,displayNum) 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"}'''%(self.AppId,verifyCode,playTimes,to,respUrl,displayNum) req.add_data(body) data='' try: res = urllib2.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 callBack(self,fromPhone,to,customerSerNum,fromSerNum,promptTone,userData,maxCallTime,hangupCdrUrl): self.subAuth() nowdate = datetime.datetime.now() self.Batch = nowdate.strftime("%Y%m%d%H%M%S") #生成sig signature = self.SubAccountSid + self.SubAccountToken + self.Batch; sig = md5.new(signature).hexdigest().upper() #拼接URL url = "https://"+self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/SubAccounts/" + self.SubAccountSid + "/Calls/Callback?sig=" + sig #生成auth src = self.SubAccountSid + ":" + self.Batch; auth = base64.encodestring(src).strip() req = urllib2.Request(url) self.setHttpHeader(req) req.add_header("Authorization", auth) #创建包体 body ='''<?xml version="1.0" encoding="utf-8"?><CallBack>\ <from>%s</from><to>%s</to><customerSerNum>%s</customerSerNum><fromSerNum>%s</fromSerNum><promptTone>%s</promptTone><userData>%s</userData><maxCallTime>%s</maxCallTime><hangupCdrUrl>%s</hangupCdrUrl>\ </CallBack>\ '''%(fromPhone,to,customerSerNum,fromSerNum,promptTone,userData,maxCallTime,hangupCdrUrl) if self.BodyType == 'json': body = '''{"from": "%s", "to": "%s","customerSerNum": "%s","fromSerNum": "%s","promptTone": "%s","userData": "%s","maxCallTime": "%s","hangupCdrUrl": "%s"}'''%(fromPhone,to,customerSerNum,fromSerNum,promptTone,userData,maxCallTime,hangupCdrUrl) req.add_data(body) data='' try: res = urllib2.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 = urllib2.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.add_data(body) data='' try: res = urllib2.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 queryAccountInfo(self): 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 + "/AccountInfo?sig=" + sig #生成auth src = self.AccountSid + ":" + self.Batch; auth = base64.encodestring(src).strip() req = urllib2.Request(url) self.setHttpHeader(req) body='' req.add_header("Authorization", auth) data='' try: res = urllib2.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 sendTemplateSMS(self, to,datas,tempId): # print ('login in sendTemplateSMS') self.accAuth() nowdate = datetime.datetime.now() self.Batch = nowdate.strftime("%Y%m%d%H%M%S") #生成sig signature = self.AccountSid + self.AccountToken + self.Batch; signature = signature.encode('utf-8') # print(signature) m = md5() m.update(signature) sig = m.hexdigest().upper() #拼接URL url = "https://"+self.ServerIP + ":" + self.ServerPort + "/" + self.SoftVersion + "/Accounts/" + self.AccountSid + "/SMS/TemplateSMS?sig=" + sig # print (url) #生成auth src = self.AccountSid + ":" + self.Batch; src = src.encode('utf-8') auth = base64.encodestring(src).strip() req = urllib2.Request(url) self.setHttpHeader(req) req.add_header("Authorization", auth) headers = {"Authorization":auth,"Accept":'application/json',"Content-Type":"application/json;charset=utf-8","Content-Length":256} #创建包体 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": to, "datas": b, "templateId": tempId, "appId": self.AppId} req.data = body # print(type(body),'heeeeeeeeeeeee') data='' try: # print('login') # res = urllib2.urlopen(req); # print(res) # data = res.read() # print(data) # res.close() # print('url=',url,'\n',body,headers) res = requests.post(url,data =json.dumps(body),headers=headers) data = res.text # print(res.text) # print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~',res.status_code) 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':'您的网络暂时不通畅,请稍候再试'}