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 = md5.new(signature).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(src).strip() req = urllib2.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) 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, 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, 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, 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, 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, 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, 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, error: if self.Iflog: self.log(url,body,data) return {'172001':'网络错误'}
def sendVerifyCode(request): response = {} response['data'] = {} response['errorMsg'] = "" _telephone = request.REQUEST.get('telephone') _method = request.REQUEST.get('method','0') #method为0,短信验证码,为1,语音验证码. #print('get the telephone with POST :'+_telephone) if _telephone == None: response['code'] = -1 response['errorMsg'] = '请输入手机号' return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json") if _telephone == '': response['code'] = -1 response['errorMsg'] = '请输入手机号' return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json") if len(_telephone) != 11: response['code'] = -1 response['errorMsg'] = '请输入11位手机号' return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json") # 手机号码: # 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[6, 7, 8], 18[0-9], 170[0-9] # 移动号段: 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705 # 联通号段: 130,131,132,155,156,185,186,145,176,1709 # 电信号段: 133,153,180,181,189,177,1700 MOBILE_prog = re.compile(r"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\d{8}$") # 中国移动:China Mobile # 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705 CM_prog = re.compile(r"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\d{8}$)|(^1705\d{7}$)") # 中国联通:China Unicom # 130,131,132,155,156,185,186,145,176,1709 CU_prog = re.compile(r"(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\d{8}$)|(^1709\d{7}$)") # 中国电信:China Telecom # 133,153,180,181,189,177,1700 CT_prog = re.compile(r"(^1(33|53|77|8[019])\d{8}$)|(^1700\d{7}$)") telephone_match_MOBILE = MOBILE_prog.match(str(_telephone)) telephone_match_CM = CM_prog.match(str(_telephone)) print telephone_match_CM telephone_match_CU = CU_prog.match(str(_telephone)) telephone_match_CT = CT_prog.match(str(_telephone)) if not telephone_match_MOBILE and not telephone_match_CM and not telephone_match_CT and not telephone_match_CU: response['code'] = -1 response['errorMsg'] = '请输入有效的手机号' return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json") saved_verify_code = cache.get(str(_telephone)) verify_code = 0 if saved_verify_code: verify_code = saved_verify_code else: verify_code = random.randrange(1000,9999,4) if _method =='0': base64_password = base64.encodestring('123456') content_msg = u'【后厨帮】登陆验证码:'+str(verify_code)+u',请您妥善保管。' params = urllib.urlencode({'action':'send','userid':'695','account':u'快点餐'.encode('utf-8'),'password':'******', 'mobile':_telephone,'content':content_msg}) url_req = "http://115.28.50.135:8888/sms.aspx" sms_req = urllib2.Request(url = url_req, data = params) sms_response = urllib2.urlopen(sms_req) sms_response=sms_response.read() xmltojsonModel = xmltojson() sms_response_json = xmltojsonModel.main(sms_response) print(sms_response) print sms_response_json if sms_response_json['returnstatus'] != 'Success': response['code'] = -1 response['errorMsg'] = sms_response_json['message'] return HttpResponse(json.dumps(response),content_type="application/json") cache.set(str(_telephone),str(verify_code),10*60) print('send_verify_code:'+cache.get(str(_telephone))) response['code'] = 0 return HttpResponse(json.dumps(response),content_type="application/json") else: voice_response = voiceVerify(str(verify_code),2,_telephone,'400-100-111','123.57.134.241/') if voice_response['statusCode'] == '000000': response['code'] = 0 return HttpResponse(json.dumps(response),content_type="application/json") else: response['code'] = -1 response['errorMsg'] = '请重新发送语音验证码~' return HttpResponse(json.dumps(response),content_type="application/json")