def MainHandler(): try: s = request.args.get('signature') t = request.args.get('timestamp') n = request.args.get('nonce') if not validateSource(t, n, s): return 'Invalid Request!' if request.method == 'GET': return request.args.get('echostr') else: x = request.data ToUserName, FromUserName, CreateTime, MsgType, Content = parseTextXml(x) ToUserName, FromUserName = FromUserName, ToUserName res = '' if pattern.validate(Content): res = pattern.answer(ToUserName, FromUserName, CreateTime, MsgType, Content) elif topten.validate(Content): res = topten.answer(ToUserName, FromUserName, CreateTime, MsgType, Content) elif activity.validate(Content): res = activity.answer(ToUserName, FromUserName, CreateTime, MsgType, Content) return res except: return render_template('index.html')
def post(self): x = self.request.body ToUserName, FromUserName, CreateTime, MsgType, Content = parseTextXml(x) ToUserName, FromUserName = FromUserName, ToUserName logging.info('Received message "{}" from "{}"'.format(Content, ToUserName)) Content = normalizeContent(Content) if pattern.validate(Content): res = pattern.answer(ToUserName, FromUserName, CreateTime, MsgType, Content) elif topten.validate(Content): res = topten.answer(ToUserName, FromUserName, CreateTime, MsgType, Content) elif library.validate(Content): res = library.answer(ToUserName, FromUserName, CreateTime, MsgType, Content) elif weather.validate(Content): res = weather.answer(ToUserName, FromUserName, CreateTime, MsgType, Content) elif translate.validate(Content): res = translate.answer(ToUserName, FromUserName, CreateTime, MsgType, Content) elif classroom.validate(Content): res = classroom.answer(ToUserName, FromUserName, CreateTime, MsgType, Content) elif movie.validate(Content): res = movie.answer(ToUserName, FromUserName, CreateTime, MsgType, Content) else: res = defaultAnswer(ToUserName, FromUserName, CreateTime, MsgType, Content) saveMsgLog(ToUserName, Content) self.response.write(res)