<s> import logging <EOL> from tornado import web <EOL> from tornado . options import options <EOL> from wechatpy import parse_message , create_reply <EOL> from wechatpy . utils import check_signature <EOL> from wechatpy . exceptions import InvalidSignatureException <EOL> from ai import AI <EOL> class WechatHandler ( web . RequestHandler ) : <EOL> def get ( self ) : <EOL> echostr = self . get_argument ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> signature = self . get_argument ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> timestamp = self . get_argument ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> nonce = self . get_argument ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> try : <EOL> check_signature ( options . token , signature , timestamp , nonce ) <EOL> except InvalidSignatureException : <EOL> logging . warning ( "<STR_LIT>" ) <EOL> else : <EOL> logging . info ( "<STR_LIT>" ) <EOL> self . write ( echostr ) <EOL> def post ( self ) : <EOL> signature = self . get_argument ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> timestamp = self . get_argument ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> nonce = self . get_argument ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> try : <EOL> check_signature ( options . token , signature , timestamp , nonce ) <EOL> except InvalidSignatureException : <EOL> logging . warning ( "<STR_LIT>" ) <EOL> return <EOL> self . set_header ( "<STR_LIT:Content-Type>" , "<STR_LIT>" ) <EOL> body = self . request . body <EOL> msg = parse_message ( body ) <EOL> if not msg : <EOL> logging . info ( '<STR_LIT>' ) <EOL> return <EOL> bot = AI ( msg ) <EOL> if msg . type == '<STR_LIT:text>' : <EOL> if options . debug : <EOL> logging . info ( '<STR_LIT>' , msg . source ) <EOL> response = bot . respond ( msg . content , msg ) <EOL> reply = create_reply ( response , msg , render = True ) <EOL> self . write ( reply ) <EOL> if options . debug : <EOL> logging . info ( '<STR_LIT>' , msg . source , response ) <EOL> elif msg . type == '<STR_LIT:location>' : <EOL> if options . debug : <EOL> logging . info ( '<STR_LIT>' , msg . source ) <EOL> elif msg . type == '<STR_LIT:image>' : <EOL> if options . debug : <EOL> logging . info ( '<STR_LIT>' , msg . source ) <EOL> else : <EOL> logging . info ( '<STR_LIT>' ) <EOL> handlers = [ <EOL> ( '<STR_LIT:/>' , WechatHandler ) , <EOL> ] </s>
