def handle_wx(request): global IMG_MEDIA_ID # GET 方式用于微信的公共平台绑定验证 if request.method == "GET": signature = request.GET.get("signature", "") timestamp = request.GET.get("timestamp", "") nonce = request.GET.get("nonce", "") echo_str = request.GET.get("echostr", "") try: check_signature(TOKEN, signature, timestamp, nonce) except InvalidSignatureException: echo_str = "error" response = HttpResponse(echo_str) return response if request.method == "POST": msg = parse_message(request.body) if msg.type == "text": reply = do_text_reply(msg) elif msg.type == 'image': IMG_MEDIA_ID = msg.media_id reply = create_reply("图片已收到" + msg.media_id, msg) elif msg.type == 'event': if msg.key == "func_detail": content = "------功能详情------\n回复关键字即可 如:\n" \ "[天气:郑州]\n[翻译:你好]\n[猜谜语]\n[音乐:天空之城]\n说句语音试一试O(∩_∩)O~~" reply = create_reply(content, message=msg) elif msg.key == "daily_image": reply = ImageReply(message=msg) reply.media_id = IMG_MEDIA_ID elif msg.key == "daily_music": reply = MusicReply(message=msg) reply.title = "偏偏喜欢你" reply.description = "陈百强" reply.thumb_media_id = IMG_MEDIA_ID reply.music_url = "http://bd.kuwo.cn/yinyue/28409674" reply.hq_music_url = "http://bd.kuwo.cn/yinyue/28409674" elif msg.key == "daily_push": reply = ArticlesReply(message=msg) for data in get_news(): reply.add_article({ 'title': data["title"], 'description': data["source"], 'image': data["imgsrc"], 'url': data["url"] }) else: reply = do_event_reply(msg) elif msg.type == 'voice': result = speech_tran(msg) reply = create_reply(result, msg) else: reply = create_reply('你发送的消息已经收到', msg) response = HttpResponse(reply.render(), content_type="application/html") return response
def wechat_back(event): event = re.sub(r'\<xml\>', '<xml>\n', event) xml = event msg = parse_message(xml) if msg.type == 'text': question = turning(msg.content) s = {} ss = {} lib = {} lib_qt = {} lib_qm = {} s_max = '' for i in range(0, len(ques)): lib_qt[ques[i]] = int(token[i]) for i in range(0, len(ques)): lib_qm[ques[i]] = media_id[i] for i in range(0, len(ques)): lib[ques[i]] = answ[i] text = [x.strip() for x in ques if len(x) > 2] if question in lib: s_max = question answer = lib[question] else: for i in text: similarity = sentence_similarity(i, question, tfidf_dict) s[i] = similarity s_max = max(s, key=s.get) #print(s_max) for i in range(0, 10): if jaccard(s_max, question) < 0.20: del s[s_max] s_max = max(s, key=s.get) else: try: ss[s_max] = s[s_max] + jaccard(s_max, question) except: ss[s_max] = 1.5 #print(ss) try: s_max = max(ss, key=ss.get) except: s_max = ' ' #print(s_max) '''try: print(lib_qt[s_max]) except: lib_qt[s_max] = 9 print(ss)''' if lib_qt[s_max] == 1: answer = lib_qm[s_max] reply = ImageReply(content='AAAAAAAAA', media_id=answer, message=msg) xml = reply.render() elif lib_qt[s_max] == 2: answer = lib[s_max] img = lib_qm[s_max].split(' ')[0] url = lib_qm[s_max].split(' ')[1] reply = ArticlesReply(message=msg) reply.add_article({ 'title': question, 'description': answer, 'image': img, 'url': url }) xml = reply.render() else: try: answer = lib[s_max] except: answer = '暂无此问题' answer = answer.encode('utf-8') #print(answer) reply = TextReply(content=answer, message=msg) xml = reply.render() print('问题:' + question) print('匹配结果:' + s_max) print('答案:', answer) resp = apiReply(reply, txt=True, content_type="application/xml") #resp = json.dumps(reply,encoding = 'utf-8') return resp