def receive_message(request): """ 收取微信消息 """ signature = request.GET.get('signature') timestamp = request.GET.get('timestamp') nonce = request.GET.get('nonce') echostr = request.GET.get('echostr') wechat = WechatBasic(token=WECHAT_TOKEN) """ 用于在微信配置响应服务器时的验证 {u'nonce': [u'280474307'], u'timestamp': [u'1438015570'],\ u'echostr': [u'3904558954066704850'],\ u'signature': [u'cfbd4c33549370f85424415310449f44e962c5d7']} """ if wechat.check_signature(signature=signature, timestamp=timestamp,\ nonce=nonce): if request.method == 'GET': if echostr: return Response(int(echostr)) elif request.method == 'POST': body = request.body try: wechat.parse_data(body) message = wechat.get_message() response = _reply_message(message, wechat) return Response(response) except Exception, e: logger.error(e)
def receive_message(request): """ 收取微信消息 """ signature = request.GET.get('signature') timestamp = request.GET.get('timestamp') nonce = request.GET.get('nonce') echostr = request.GET.get('echostr') wechat = WechatBasic(token=WECHAT_TOKEN) """ 用于在微信配置响应服务器时的验证 {u'nonce': [u'280474307'], u'timestamp': [u'1438015570'],\ u'echostr': [u'3904558954066704850'],\ u'signature': [u'cfbd4c33549370f85424415310449f44e962c5d7']} """ if wechat.check_signature(signature=signature, timestamp=timestamp,\ nonce=nonce): if request.method == 'GET': if echostr: return Response(int(echostr)) elif request.method == 'POST': body = request.body try: wechat.parse_data(body) message = wechat.get_message() response = _reply_message(message, wechat) return Response(response) except Exception, e: logger.error(e)
def getToken(request): token = 'xiangeqwd' wechat = WechatBasic(token=token) if wechat.check_signature(signature=request.GET.get('signature',''), timestamp=request.GET.get('timestamp',''), nonce=request.GET.get('nonce','')): # return HttpResponse(request.GET.get('echostr', 'error')) print('request method in getToken:',request.method) if request.method == 'GET': response = request.GET.get('echostr', 'error') else: xml2dict = wechat.parse_data(request.body) for k,v in xml2dict.items(): print(k,v) if xml2dict['Event'] == 'LOCATION' or 'VIEW': # if 'FromUserName' not in xml2dict or not xml2dict['FromUserName']: # xml2dict['FromUserName'] = request.META.get('REMOTE_ADDR') if 'Latitude' not in xml2dict.keys() or 'Longitude' not in xml2dict.keys(): latitude = float(22.5414180756) longitude = float(114.0480804443) else: latitude = float(xml2dict['Latitude']) longitude = float(xml2dict['Longitude']) # if not TblUser.objects.filter(openid=xml2dict['FromUserName']): # user = TblUser.objects.create( # id=uuid4(), # add_time=time.strftime(SERVER_TIME_FORMAT, time.localtime(time.time())), # openid=xml2dict['FromUserName'] # ) if TblUser.objects.filter(openid=xml2dict['FromUserName']).count() == 0: tbl_user = TblUser( id=uuid4(), add_time=time.strftime(SERVER_TIME_FORMAT, time.localtime(time.time())), openid=xml2dict['FromUserName'], latitude=latitude, longitude=longitude, default=user_service.SET_DEFAULT, access=user_service.ALLOW) tbl_user.save() # else: # TblUser.objects.filter(openid=xml2dict['FromUserName']).update( # latitude=latitude, # longitude=longitude) request.session['openid'] = xml2dict['FromUserName'] # message = wechat.get_message() # response = wechat.response_text(u'消息类型: {}'.format(message.type)) response = 'success' else: return HttpResponseBadRequest('Verify Failed') return HttpResponse(response)
def weixin(request): signature = request.GET.get('signature') timestamp = request.GET.get('timestamp') nonce = request.GET.get('nonce') xml = request.body wechat_instance = WechatBasic(token='custom_service', appid=weixin_appid, appsecret=weixin_secret) if not wechat_instance.check_signature( signature=signature, timestamp=timestamp, nonce=nonce): return HttpResponseBadRequest('Verify Failed') else: if request.method == 'GET': return HttpResponse(request.GET.get('echostr')) try: wechat_instance.parse_data(data=xml) except ParseError: return HttpResponseBadRequest('Invalid XML Data') message = wechat_instance.get_message() # try: if message.type == 'text': # response = wechat_instance.response_text(u'文字') if do_commands_table.has_key(message.content): response = do_commands_table[message.content](wechat_instance, request) else: response = wechat_instance.response_text(message.content) elif message.type == 'image': content = message.media_id response = wechat_instance.response_text(u'图片') elif message.type == 'voice': response = wechat_instance.response_text(u'声音') elif message.type == 'video' or message.type == 'shortvideo': response = wechat_instance.response_text(u'视频') elif message.type == 'location': response = wechat_instance.response_text(u'地理位置') elif message.type == 'link': response = wechat_instance.response_text(u'链接') elif message.type == 'event': response = wechat_instance.response_text(u'事件') elif message.type == 'click' or message.type == 'view': if do_commands_table.has_key(message.key): response = do_commands_table[message.key](wechat_instance, request) else: response = wechat_instance.response_text(message.key) else: response = wechat_instance.response_text(u'未知' + message.type) # except: # response = wechat_instance.response_text(u'服务器错误') return HttpResponse(response)
def weixinapi(request): wechat = WechatBasic(token=TOKEN) if 'echostr' in request.GET: result = wechat.check_signature(signature=request.GET['signature'], timestamp=request.GET['timestamp'], nonce=request.GET['nonce']) if result: return HttpResponse(request.GET['echostr']) else: return HttpResponse() message = request.body response = process_msg(wechat, message) return HttpResponse(response)
def weixin(request): signature = request.GET.get('signature') timestamp = request.GET.get('timestamp') nonce = request.GET.get('nonce') xml = request.body wechat_instance = WechatBasic(token='custom_service', appid=weixin_appid, appsecret=weixin_secret) if not wechat_instance.check_signature(signature=signature, timestamp=timestamp, nonce=nonce): return HttpResponseBadRequest('Verify Failed') else: if request.method == 'GET': return HttpResponse(request.GET.get('echostr')) try: wechat_instance.parse_data(data=xml) except ParseError: return HttpResponseBadRequest('Invalid XML Data') message = wechat_instance.get_message() # try: if message.type == 'text': # response = wechat_instance.response_text(u'文字') if do_commands_table.has_key(message.content): response = do_commands_table[message.content](wechat_instance, request) else: response = wechat_instance.response_text(message.content) elif message.type == 'image': content = message.media_id response = wechat_instance.response_text(u'图片') elif message.type == 'voice': response = wechat_instance.response_text(u'声音') elif message.type == 'video' or message.type == 'shortvideo': response = wechat_instance.response_text(u'视频') elif message.type == 'location': response = wechat_instance.response_text(u'地理位置') elif message.type == 'link': response = wechat_instance.response_text(u'链接') elif message.type == 'event': response = wechat_instance.response_text(u'事件') elif message.type == 'click' or message.type == 'view': if do_commands_table.has_key(message.key): response = do_commands_table[message.key](wechat_instance, request) else: response = wechat_instance.response_text(message.key) else: response = wechat_instance.response_text(u'未知' + message.type) # except: # response = wechat_instance.response_text(u'服务器错误') return HttpResponse(response)
def wechat_auth(request): try: wechat = WechatBasic(appid="wx85ddc4617e4400b0", appsecret="237dc3cc551b6bac043f660cbab093ca", token="feynman") signature = request.GET['signature'] timestamp = request.GET['timestamp'] nonce = request.GET['nonce'] if wechat.check_signature(signature=signature, timestamp=timestamp, nonce=nonce): if request.method == 'GET': return HttpResponse(request.GET.get('echostr', ''), content_type="text/plain") else: wechat.parse_data(request.body) return HttpResponse(wechat_kernel(wechat, request), content_type="application/xml") else: return HttpResponse("Valid Auth") except Exception, e: print e.message return HttpResponse(e.message)