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 = urllib2.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.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 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 = 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><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.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 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 = urllib2.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.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 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 querySubAccount(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 + "/QuerySubAccountByName?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"?><SubAccount><appId>%s</appId>\ <friendlyName>%s</friendlyName>\ </SubAccount>\ '''%(self.AppId, friendlyName) if self.BodyType == 'json': body = '''{"friendlyName": "%s", "appId": "%s"}'''%(friendlyName,self.AppId) data='' 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 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 = urllib2.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.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,alwaysPlay,terminalDtmf,userData,maxCallTime,hangupCdrUrl,needBothCdr,needRecord,countDownTime,countDownPrompt): 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>\ <alwaysPlay>%s</alwaysPlay><terminalDtmf>%s</terminalDtmf><needBothCdr>%s</needBothCdr><needRecord>%s</needRecord><countDownTime>%s</countDownTime><countDownPrompt>%s</countDownPrompt>\ </CallBack>\ '''%(fromPhone,to,customerSerNum,fromSerNum,promptTone,userData,maxCallTime,hangupCdrUrl,alwaysPlay,terminalDtmf,needBothCdr,needRecord,countDownTime,countDownPrompt) if self.BodyType == 'json': body = '''{"from": "%s", "to": "%s","customerSerNum": "%s","fromSerNum": "%s","promptTone": "%s","userData": "%s","maxCallTime": "%s","hangupCdrUrl": "%s","alwaysPlay": "%s","terminalDtmf": "%s","needBothCdr": "%s","needRecord": "%s","countDownTime": "%s","countDownPrompt": "%s"}'''%(fromPhone,to,customerSerNum,fromSerNum,promptTone,userData,maxCallTime,hangupCdrUrl,alwaysPlay,terminalDtmf,needBothCdr,needRecord,countDownTime,countDownPrompt) 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 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 = 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): self.accAuth() nowdate = datetime.datetime.now() self.Batch = nowdate.strftime("%Y%m%d%H%M%S") #生成sig signature = self.AccountSid.encode('utf-8') + self.AccountToken.encode('utf-8') + self.Batch.encode('utf-8'); 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 #生成auth src = self.AccountSid + ":" + self.Batch; auth = base64.encodestring(bytes(src, 'utf-8')).strip() req = 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.add_data(body) req = Request(url, bytes(body, 'utf-8')) self.setHttpHeader(req) req.add_header("Authorization", auth) data='' try: res = urlopen(req); data = res.read() res.close() if self.BodyType=='json': #json格式 locations = json.loads(data.decode('utf-8')) 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) print(error) return {'172001':'网络错误'}