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 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 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)