#NEWLINE# @ app . route ( '/wechat' , methods = [ 'GET' , 'POST' ] ) #NEWLINE# def wechat( ) : #NEWLINE# #INDENT# signature = request . args . get ( 'signature' , '' ) #NEWLINE# timestamp = request . args . get ( 'timestamp' , '' ) #NEWLINE# nonce = request . args . get ( 'nonce' , '' ) #NEWLINE# echo_str = request . args . get ( 'echostr' , '' ) #NEWLINE# encrypt_type = request . args . get ( 'encrypt_type' , '' ) #NEWLINE# msg_signature = request . args . get ( 'msg_signature' , '' ) #NEWLINE# print ( 'signature:' , signature ) #NEWLINE# print ( 'timestamp: ' , timestamp ) #NEWLINE# print ( 'nonce:' , nonce ) #NEWLINE# print ( 'echo_str:' , echo_str ) #NEWLINE# print ( 'encrypt_type:' , encrypt_type ) #NEWLINE# print ( 'msg_signature:' , msg_signature ) #NEWLINE# try : #NEWLINE# #INDENT# check_signature ( TOKEN , signature , timestamp , nonce ) #NEWLINE# #UNINDENT# except InvalidSignatureException : #NEWLINE# #INDENT# abort ( 403 ) #NEWLINE# #UNINDENT# if ( request . method == 'GET' ) : #NEWLINE# #INDENT# return echo_str #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# print ( ( 'Raw message: \n%s' % request . data ) ) #NEWLINE# crypto = WeChatCrypto ( TOKEN , EncodingAESKey , AppId ) #NEWLINE# try : #NEWLINE# #INDENT# msg = crypto . decrypt_message ( request . data , msg_signature , timestamp , nonce ) #NEWLINE# print ( ( 'Descypted message: \n%s' % msg ) ) #NEWLINE# #UNINDENT# except ( InvalidSignatureException , InvalidAppIdException ) : #NEWLINE# #INDENT# abort ( 403 ) #NEWLINE# #UNINDENT# msg = parse_message ( msg ) #NEWLINE# if ( msg . type == 'text' ) : #NEWLINE# #INDENT# reply = create_reply ( msg . content , msg ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# reply = create_reply ( 'Sorry, can not handle this for now' , msg ) #NEWLINE# #UNINDENT# return crypto . encrypt_message ( reply . render ( ) , nonce , timestamp )