def wechat_auth(): chat = Wechat() if request.method == 'GET': query = request.args signature = query.get('signature') timestamp = query.get('timestamp') nonce = query.get('nonce') echostr = query.get('echostr') return chat.auth(timestamp,nonce,signature,echostr) mc = memcache.Client() chat.parse_data(request.data) if chat.msgtype == 'text': content = chat.msg.content if content == u'音乐': mc.set(chat.msg.user+'_music','music') reply = u'开始收听,列表:1.云中的Angle;2.until you;3.终于等到你。请输入序号。若停止收听,请输入停止,可切换到与机器对话' return chat.resp_text(reply) elif content == u'停止': mc.delete(chat.msg.user+'_music') reply = u'欢迎下次收听' return chat.resp_text(reply) elif content == u'帮助': reply = u'若想收听音乐,请输入音乐,想停止,即输入停止' return chat.resp_text(reply) elif content == u'记忆': articles = [{'title':u'爱你','desc':u'记忆','picurl':'http://hbnn-hbnnstore.stor.sinaapp.com/boat.jpg', 'url':'http://blog.sina.com.cn/s/blog_a2356b8b0102vvq3.html'}, {'title':u'老婆我爱你','desc':u'记忆','picurl':'http://hbnn-hbnnstore.stor.sinaapp.com/europ.jpg', 'url':'http://finance.ifeng.com/a/20150630/13808481_0.shtml'}] return chat.resp_article(articles) else: pass music = mc.get(chat.msg.user+'_music') if music == 'music': song = MusicDict[content] title, desc, musicurl, hqurl = song['title'],song['desc'],song['url'],song['url'] return chat.resp_music(title, desc, musicurl, hqurl) else: reply = xhj.resp(content) return chat.resp_text(reply)
handler = logging.handlers.RotatingFileHandler(WXLOG_FILE, maxBytes = 1024*1024, backupCount = 5) # 实例化handler fmt = '%(asctime)s <%(filename)s><%(lineno)d>: [%(levelname)s] - %(message)s' handler2 = logging.handlers.RotatingFileHandler(MOLOG_FILE, maxBytes = 1024*1024, backupCount = 5) # 实例化handler formatter = logging.Formatter(fmt) # handler.setFormatter(formatter) # handler2.setFormatter(formatter) logger = logging.getLogger('wx') # logger.addHandler(handler) logger.setLevel(logging.DEBUG) logger1 = logging.getLogger('monitor') # logger1.addHandler(handler2) logger1.setLevel(logging.DEBUG) with aiohttp.ClientSession() as client, aiohttp.ClientSession() as rclient: wx = Wechat(client) robot = RobotEngine(rclient, config.apikey) msg = MsgHandler(wx, robot) god = Monitor(wx) tasks = [ wx.sync() , wx.sendmsg() , wx.updategroupinfo() , msg.msgloop() , god.monitor() ] asyncio.get_event_loop().run_until_complete(asyncio.wait(tasks))
return chat.resp_music(title, desc, musicurl, hqurl) else: reply = xhj.resp(content) return chat.resp_text(reply) @app.route('/job') def Job(): pass if __name__ == "__main__": chat = Wechat() xml = """ <xml> <ToUserName><![CDATA[wechat]]></ToUserName> <FromUserName><![CDATA[pycharm]]></FromUserName> <CreateTime>13</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[rhh]]></Content> </xml> """ Music = {'1':{'title':u'云中的Angle','desc':u'爱你', 'url':'http://hbnn-hbnnstore.stor.sinaapp.com/%E5%BC%A0%E6%9D%B0%20-%20%E4%BA%91%E4%B8%AD%E7%9A%84Angel.mp3'}, '2':{'title':'until you','desc':u'爱你,老婆', 'url':'http://hbnn-hbnnstore.stor.sinaapp.com/Shayne%20Ward-Until%20You.wma'}, '3':{'title':u'终于等到你','desc':u'永远在一起', 'url':'http://hbnn-hbnnstore.stor.sinaapp.com/%E5%BC%A0%E9%9D%93%E9%A2%96-%E7%BB%88%E4%BA%8E%E7%AD%89%E5%88%B0%E4%BD%A0(%E7%94%B5%E8%A7%86%E5%89%A7%E3%80%8A%E5%92%B1%E4%BB%AC%E7%BB%93%E5%A9%9A%E5%90%A7%E3%80%8B%E4%B8%BB%E9%A2%98%E6%9B%B2).wma'