def test(self, request, message, wechat): """ 发布中测试 """ logger = getLogger('django.request.test_ProcessServerEventView') logger.info(message) if message.get('MsgType').lower() == 'event': reply = TextReply() reply.target = message['FromUserName'] reply.source = message['ToUserName'] reply.content = message['Event'] + 'from_callback' xml_str = reply.render() headers = {'CONTENT_TYPE': request.META['CONTENT_TYPE']} return Response(xml_str, headers=headers) elif message.get('MsgType').lower() in consts.MESSAGE_TYPES: if message.get('Content') == 'TESTCOMPONENT_MSG_TYPE_TEXT': reply = TextReply() reply.target = message['FromUserName'] reply.source = message['ToUserName'] reply.content = 'TESTCOMPONENT_MSG_TYPE_TEXT_callback' xml_str = reply.render() headers = {'CONTENT_TYPE': request.META['CONTENT_TYPE']} return Response(xml_str, headers=headers) elif message.get('Content').startswith('QUERY_AUTH_CODE'): from datetime import timedelta now = datetime.utcnow() + timedelta(seconds=2) query_auth_code = message.get('Content').split(':')[1] process_wechat_query_auth_code_test.apply_async( (message['FromUserName'], query_auth_code), eta=now) return Response('')
def serve(request): # GET 方式用于微信公众平台绑定验证 if request.method == 'GET': signature = request.GET.get('signature', '') timestamp = request.GET.get('timestamp', '') nonce = request.GET.get('nonce', '') echo_str = request.GET.get('echostr', '') try: check_signature(settings.Token, signature, timestamp, nonce) except InvalidSignatureException: echo_str = '错误的请求' response = HttpResponse(echo_str) else: msg = parse_message(request.body) msg_dict = msg.__dict__['_data'] # print(msg.id, msg.source, msg.create_time, msg.type, msg.target, msg.time, msg.__dict__['_data']['Event'], '====') if msg.type == 'text': print(msg) # print(msg.source) # print(msg.target) # transResult = youdaoTranslate(msg.content) # transResult = googleTranslate(msg.content) transResult = getAns(msg.content) transResult = transResult if transResult != '' else '我好像不明白' reply = TextReply(content= transResult, messsage=msg) reply.source = msg.target reply.target = msg.source xml = reply.render() print(reply) # pass elif msg.type == 'event': if msg_dict['Event'] == 'subscribe': # 关注后 将获取的用户的信息保存到数据库 # wx_wechat.subscribe(getWxUserInfo(msg.source)) print('subscribe') elif msg_dict['Event'] == 'unsubscribe': # 取关后,将用户的关注状态更改为 未关注 # wx_wechat.unsubscribe(msg.source) print('subscribe') elif msg.type == 'image': print(msg) faceResult = getResultFromUrl(msg.image) if faceResult['ret']!=0: reply = TextReply(content= '未检测到人脸', messsage=msg) else: content = numTranslate(faceResult['data']['face_list'][0]) reply = TextReply(content= content, messsage=msg) reply.source = msg.target reply.target = msg.source xml = reply.render() else: pass response = HttpResponse(xml, content_type="application/xml") return response
def handle_msg(request): if request.method == 'GET': signature = request.GET.get('signature') timestamp = request.GET.get('timestamp') nonce = request.GET.get('nonce') echo_str = request.GET.get('echostr') check_signature(TOKEN, signature, timestamp, nonce) return HttpResponse(echo_str) elif request.method == 'POST': body = request.body msg = parse_message(body) rep = TextReply() rep.source = msg.target rep.target = msg.source if msg.type == 'event': if msg.event == 'click': print(msg.key) if msg.key == 'sports_advice': rep.content = recommend_plan(msg.source) elif msg.key == 'view_info': rep.content = get_info(msg.source) elif msg.key == 'add_test': rep.content = add_test(msg.source) elif msg.key == 'show_today': rep.content = get_datatoday(msg.source) elif msg.event == 'subscribe': rep.content = create_newuser(msg.source) else: rep.content = '!!!' else: rep.content = '<a href="http://learn.tsinghua.edu.cn">你好</a>' repxml = rep.render() return HttpResponse(repxml)
def reply_message(self, message, content): """ 回复公众号消息 """ reply = TextReply() reply.target = message.FromUserName reply.source = message.ToUserName reply.content = content xml_str = reply.render() headers = {'CONTENT_TYPE': self.request.META['CONTENT_TYPE']} return Response(xml_str, headers=headers)
def handle_msg(request): if request.method == 'GET': signature = request.GET.get('signature') timestamp = request.GET.get('timestamp') nonce = request.GET.get('nonce') echo_str = request.GET.get('echostr') check_signature(TOKEN, signature, timestamp, nonce) return HttpResponse(echo_str) elif request.method == 'POST': body = request.body msg = parse_message(body) rep = TextReply() rep.source = msg.target rep.target = msg.source if msg.type == 'event': if msg.event == 'click': if msg.key == 'sports_advice': rep.content = recommend_plan(msg.source) elif msg.key == 'view_info': rep.content = get_info(msg.source) elif msg.key == 'add_bong': rep.content = add_bong(msg.source) elif msg.key == 'add_test': rep.content = add_test(msg.source) elif msg.key == 'add_test_new': rep.content = add_test_new(msg.source) elif msg.key == 'show_today': rep.content = get_datatoday(msg.source) elif msg.key == 'change_remind': rep.content = set_remind(msg.source) elif msg.key == 'build_match': article_rep = ArticlesReply() article_rep.source = msg.target article_rep.target = msg.source article_rep.add_article({ 'title': '创建比赛', 'description': '点此链接以创建比赛', 'image': serverIP+'static/img/run02.jpg', 'url': build_match(msg.source) }) repxml = article_rep.render() return HttpResponse(repxml) elif msg.event == 'subscribe': rep.content = create_newuser(msg.source) else: rep.content = '!!!' else: rep.content = '你好' repxml = rep.render() return HttpResponse(repxml)
def test1(request): print('!!!!!!') if request.method == 'GET': signature = request.GET.get('signature') timestamp = request.GET.get('timestamp') nonce = request.GET.get('nonce') echo_str = request.GET.get('echostr') check_signature(TOKEN, signature, timestamp, nonce) print('!!!') return HttpResponse(echo_str) elif request.method == 'POST': body = request.body msg = parse_message(body) print(msg.content) print(msg.source) print(msg.target) rep = TextReply() rep.source = msg.target rep.target = msg.source rep.content = '<a href="http://www.baidu.com">草泥马</a>' repxml = rep.render() return HttpResponse(repxml)
def handleMsg(request): if request.method == 'GET': signature = request.GET.get('signature') timestamp = request.GET.get('timestamp') nonce = request.GET.get('nonce') echo_str = request.GET.get('echostr') check_signature(TOKEN, signature, timestamp, nonce) return HttpResponse(echo_str) elif request.method == 'POST': body = request.body msg = parse_message(body) rep = TextReply() rep.source = msg.target rep.target = msg.source print(msg.type) if msg.type == 'event': if msg.event == 'click': if msg.key == 'change_info': rep.content = '请点击下面的链接修改信息\n<a href="http://59.66.139.224/showinfo?openID= '+ msg.source + '">修改信息</a>' else: rep.content = '<a href="http://learn.tsinghua.edu.cn">草泥马</a>' repxml = rep.render() return HttpResponse(repxml)
def weixin(): if request.method == 'GET': _g = lambda k: request.args.get(k) try: print(TOKEN, _g('signature'), _g('timestamp'), _g('nonce')) check_signature(TOKEN, _g('signature'), _g('timestamp'), _g('nonce')) return _g('echostr') except InvalidSignatureException: pass elif request.method == 'POST': msg = parse_message(request.data) if isinstance(msg, ClickEvent): ev = msg if ev.key == WX_BTN_BIND_BX: reply = TextReply() reply.source = ev.target reply.target = ev.source reply.content = '请访问以下链接登陆到BX邮箱\n%s' % ( '%s?%s' % (urljoin(DOMAIN, 'mslogin'), urlencode({'wx': ev.source})) ) return reply.render() elif ev.key == WX_BTN_BEGIN_CHECKIN: reply = TextReply() reply.source = msg.target reply.target = msg.source reply.content = '请访问以下链接选择开始哪个会议\n%s' % ( '%s/check-in-meetings?openid=%s' % (DOMAIN, msg.source) ) return reply.render() else: reply = TextReply() reply.source = msg.target reply.target = msg.source reply.content = '哦' return reply.render() elif isinstance(msg, TextMessage): if msg.content.lower().startswith('e'): reply = TextReply() reply.source = msg.target reply.target = msg.source reply.content = '请访问以下链接选择开始哪个会议\n%s' % ( '%s/check-in-meetings?openid=%s' % (DOMAIN, msg.source) ) return reply.render() else: return '哦' elif isinstance(msg, ScanEvent) or isinstance(msg, SubscribeScanEvent): openid = msg.source meetingid = msg.scene_id meeting = mongo.db.meeting.find_one({'meetingid': meetingid}) checkin_time, ps = emit_checked_in(openid=openid, meeting=meeting) r = check_in(openid=openid, meetingid=meetingid, punish_str=ps, checkin_time=checkin_time) meeting = mongo.db.meeting.find_one({'meetingid': meetingid}) print(meeting) if all_attendees_checked(meeting['attendee']): s = '签到汇总\n%s' ra = [] for u in meeting['attendee']: print(u) full_u = mongo.db.users.find_one({'openid': u['openid']}) ra.append([full_u['outlook']['name'], u['punish_str']]) s %= '\n'.join(map(lambda ss: ': '.join(ss), ra)) tmp = list(map(lambda u: u['openid'], meeting['attendee'])) tmp.append(meeting['organizer']['openid']) if len(tmp) > 1: wx.message.send_mass_text(tmp, s, is_to_all=True) else: wx.message.send_text(tmp[0], s) # return r wx.message.send_text(openid, r) reply = TextReply() reply.source = msg.target reply.target = msg.source reply.content = '请访问以下链接选择查看签到状况\n%s' % ( '%s/check-in-members?meetingid=%s' % (DOMAIN, meetingid) ) return reply.render()