def msg_handle(): """ 处理来自用户的消息 """ if not verify(request): # 如果消息不是来自微信服务器, 拒绝服务. raise Exception('message processing fail') msg = parse_msg(request.data) user_id = msg['FromUserName'] Rp = Responser(msg) IRp = IndividualRepoter(msg, D, PATH.INDIVIDUAL_PATH) if is_subscribe_event(msg): # 新用户关注 return Rp.rp_text_msg(WELCOME_MESSAGE) elif is_click_event(msg): # 用户点击了微信菜单上的click事件按钮 if msg['EventKey'] == WECHAT.INDIVIDUAL_TOKEN: user_rm(user_id) user_individuality_new(user_id) return Rp.rp_text_msg(INDIVIDUAL_RESPONSE) elif is_text_msg(msg): if user_in_indivduality(user_id): # 请求个性化告知服务 result = IRp.individual_response() return Rp.rp_text_msg(result) else: return Rp.make_rp() raise Exception('xxx')
def access_verify(): """ 微信认证接口 """ echostr = request.args.get('echostr') if verify(request) and echostr: return echostr raise Exception('access verification fail')