def test_voice_reply_properties(self): from wechatpy.replies import VoiceReply reply = VoiceReply(voice='7890') self.assertEqual('7890', reply.voice) self.assertEqual('7890', reply.media_id) reply.media_id = '123456' self.assertEqual('123456', reply.voice) self.assertEqual('123456', reply.media_id)
def index(): if request.method == 'GET': return request.args.get('echostr') xml = request.get_data().decode() print(xml) msg = parse_message(xml) if msg.type == 'text': media_id = upload(msg.content) reply = VoiceReply(media_id=media_id, message=msg) return reply.render() return ''
def test_voice_reply_properties(self): from wechatpy.replies import VoiceReply reply = VoiceReply(voice="7890") self.assertEqual("7890", reply.voice) self.assertEqual("7890", reply.media_id) reply.media_id = "123456" self.assertEqual("123456", reply.voice) self.assertEqual("123456", reply.media_id)
def test_voice_reply_properties(self): from wechatpy.replies import VoiceReply reply = VoiceReply(voice='7890') self.assertEqual('7890', reply.voice) self.assertEqual('7890', reply.media_id) reply.media_id = '123456' self.assertEqual('123456', reply.voice) self.assertEqual('123456', reply.media_id)
def echo_server(): error = None if request.method == 'GET': # The WeChat server will issue a GET request in order to verify the chatbot backend server upon configuration. return verify_backend(request) elif request.method == 'POST': # Messages will be POSTed from the WeChat server to the chatbot backend server, message = parse_message(request.data) print(message) if message.type == 'text': reply = TextReply(content=message.content, message=message) elif message.type == 'image': reply = VoiceReply(media_id=message.media_id, message=message) elif message.type == 'voice': reply = VoiceReply(media_id=message.media_id, message=message) return reply.render()
def wechat(request): if request.method == 'GET': signature = request.GET.get('signature', None) timestamp = request.GET.get('timestamp', None) nonce = request.GET.get('nonce', None) echostr = request.GET.get('echostr', None) try: check_signature(WECHAT_TOKEN, signature, timestamp, nonce) except InvalidSignatureException: echostr = 'error' return HttpResponse(echostr) elif request.method == 'POST': msg = parse_message(request.body) print(msg) # if msg.type == 'text': # if msg.content == '民事案件': # reply = getDogLossList(request, msg) # elif msg.content == '刑事案件': # reply = getDogOwnerList(request, msg) # else: # reply = TransferCustomerServiceReply(message=msg) # code = request.POST.get('code',None) # print(code,1111111111) if msg.type == 'image': reply = ImageReply(message=msg) reply.media_id = msg.media_id elif msg.type == 'voice': reply = VoiceReply(message=msg) reply.media_id = msg.media_id reply.content = '语音信息' elif msg.type == 'event': print('eventkey=', msg.event) if msg.event == 'subscribe': print(333333333333) saveWxLaw(msg.source) reply = create_reply('感谢您关注【辽宁大潮律师事务所】', msg) elif msg.event == 'unsubscribe': reply = create_reply('取消关注公众号', msg) unSubUserinfo(msg.source) elif msg.event == 'subscribe_scan': reply = create_reply('感谢您关注【辽宁大潮律师事务所】', msg) saveWxLaw(msg.source, msg.scene_id) print('scene_id=', msg.scene_id) elif msg.event == 'scan': print('scan====', msg.scene_id) # setUserToMember(msg.source, msg.scene_id) reply = create_reply('', msg) else: reply = create_reply('view', msg) response = HttpResponse(reply.render(), content_type="application/xml") return response
def wechat(request): if request.method == 'GET': signature = request.GET.get('signature', None) timestamp = request.GET.get('timestamp', None) nonce = request.GET.get('nonce', None) echostr = request.GET.get('echostr', None) try: check_signature(settings.WECHAT_TOKEN, signature, timestamp, nonce) except InvalidSignatureException: echostr = 'error' return HttpResponse(echostr) elif request.method == 'POST': msg = parse_message(request.body) if msg.type == 'text': reply = TransferCustomerServiceReply(message=msg) elif msg.type == 'image': reply = ImageReply(message=msg) reply.media_id = msg.media_id elif msg.type == 'voice': reply = VoiceReply(message=msg) reply.media_id = msg.media_id reply.content = '语音信息' elif msg.type == 'event': print('eventkey=', msg.event) if msg.event == 'subscribe': saveUserinfo(msg.source) reply = create_reply('你好,欢迎关注亚电新能源', msg) elif msg.event == 'unsubscribe': reply = create_reply('取消关注公众号', msg) unSubUserinfo(msg.source) elif msg.event == 'subscribe_scan': reply = create_reply('你好,欢迎关注亚电新能源', msg) saveUserinfo(msg.source, msg.scene_id) elif msg.event == 'scan': reply = create_reply('', msg) else: reply = create_reply('view', msg) else: reply = create_reply('', msg) response = HttpResponse(reply.render(), content_type="application/xml") return response
def on_post(self, req, resp): # 猜测是打开数据流 xml = req.stream.read() # 解析xml格式数据 msg = parse_message(xml) if msg.type == 'text': reply = TextReply(content='露露我爱你', message=msg) # content=msg.content xml = reply.render() resp.body = (xml) resp.status = falcon.HTTP_200 elif msg.type == 'image': reply = ImageReply(media_id=msg.media_id, message=msg) xml = reply.render() resp.body = (xml) resp.status = falcon.HTTP_200 elif msg.type == 'voice': reply = VoiceReply(media_id=msg.media_id, message=msg) xml = reply.render() resp.body = (xml) resp.status = falcon.HTTP_200
def post(self, request): xml = request.body msg = parse_message(xml) if msg.type == 'text': # 获取文本内容 try: content = msg.content # print(res) # print(json.dump(res)) reply = TextReply(content=content, message=msg) r_xml = reply.render() # 获取唯一标记用户的openid,下文介绍获取用户信息会用到 openid = msg.source # print(openid) return HttpResponse(r_xml) except Exception as e: # 自行处理 return HttpResponse('success') # return HttpResponse('') elif msg.type == 'image': print(msg.image, msg.media_id) reply = ArticlesReply(message=msg) reply.add_article({ 'title': 'Welcome tom my channel!', 'description': 'Finally, we meet you here,\nYou must know you are very important for us!', 'image': msg.image, 'url': 'https://baidu.com' }) r_xml = reply.render() return HttpResponse(r_xml) elif msg.type == 'voice': print(msg.format, msg.recognition) reply = VoiceReply(media_id=msg.media_id, message=msg) return HttpResponse(reply.render()) elif msg.type == 'video': print(msg) print(msg.media_id, msg.thumb_media_id) reply = VideoReply(media_id=msg.media_id, title='Test video', description='Description', message=msg) return HttpResponse(reply.render()) elif msg.type == 'location': print(msg.location_x, msg.location_y, msg.scale, msg.label, msg.location) return HttpResponse('Success') elif msg.type == 'event': # print(msg.type) if msg.event == 'subscribe': try: reply = TextReply(content='Welcome to my channel', message=msg) r_xml = reply.render() return HttpResponse(r_xml) except: return HttpResponse('Success') elif msg.event == 'unsubscribe': try: reply = TextReply(content='See you again!', message=msg) r_xml = reply.render() return HttpResponse(r_xml) except: return HttpResponse('Success') elif msg.event == 'subscribe_scan': try: print(msg.scene_id, msg.ticket) reply = TextReply(content='See you again!', message=msg) r_xml = reply.render() return HttpResponse(r_xml) except: return HttpResponse('Success') elif msg.event == 'scan': try: print(msg.scene_id, msg.ticket) reply = TextReply(content='See you again!', message=msg) r_xml = reply.render() return HttpResponse(r_xml) except: return HttpResponse('Success') elif msg.event == 'location': try: print(msg.latitude, msg.longitude, msg.precision) reply = TextReply(content='See you again!', message=msg) r_xml = reply.render() return HttpResponse(r_xml) except: return HttpResponse('Success') elif msg.event == 'click': try: print(msg.key) reply = TextReply(content=msg.key, message=msg) r_xml = reply.render() return HttpResponse(r_xml) except: return HttpResponse('Success') elif msg.event == 'view': try: print(f'{msg.url}\n=========') reply = TextReply(content=msg.url, message=msg) r_xml = reply.render() return HttpResponse(r_xml) except: return HttpResponse('Success')
def handle_wx(request): 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 = '错误的请求' response = HttpResponse(echo_str) # client = WeChatClient("wx058a1e6adf42dede", "d4631adb81c598c1f9acddfb4bc2ba10") # menu = client.menu.get() # print(menu) return response elif request.method == 'POST': xml = request.body msg = parse_message(xml) openid = msg.source try: if msg.type == 'text': reply = TextReply(content=msg.content, message=msg) print(msg.content) return HttpResponse(reply.render()) elif msg.type == 'image': print(msg.media_id) reply = ImageReply(media_id=msg.media_id, message=msg) return HttpResponse(reply.render()) elif msg.type == 'voice': print(msg.media_id) reply = VoiceReply(media_id=msg.media_id, message=msg) return HttpResponse(reply.render()) elif msg.type == 'video': print(msg) reply = VideoReply(media_id=msg.media_id, thumb_media_id=msg.thumb_media_id, title='title', description='description', message=msg) print(reply) print(msg.media_id) return HttpResponse(reply.render()) elif msg.type == 'music': print(msg.thumb_media_id, msg.title, msg.description) reply = MusicReply(thumb_media_id=msg.thumb_media_id, title='title', description='description', music_url=msg.music_url, hq_music_url=msg.hq_music_url, message=msg) return HttpResponse(reply.render()) # elif msg.type == 'news': # reply = ArticlesReply(message=msg, articles=[ # { # 'title': u'标题1', # 'description': u'描述1', # 'url': u'http://www.qq.com', # }, # { # 'title': u'标题2', # 'description': u'描述2', # 'url': u'http://www.qq.com', # 'image': 'http://img.qq.com/1.png', # }, # ]) # # 继续添加 # reply.add_article({ # 'title': u'标题3', # 'description': u'描述3', # 'url': u'http://www.qq.com', # }) # r_xml = reply.render() # return HttpResponse(r_xml) elif msg.type == 'event': push = ScanCodeWaitMsgEvent(msg) #获取二维码信息,字符串 content = msg.scan_result print(content) # 如何处理,自行处理,回复一段文本或者图文 reply = TextReply(content="Someting", message=msg) r_xml = reply.render() return HttpResponse(r_xml) #pass except Exception as e: print("Exception:", e) return "success" return HttpResponse('ok')
def on_post(self,req,resp): #后台接受数据进行xml解析 xml=req.stream.read() msg=parse_message(xml) print(msg) dict={} for name in dir(msg): value = getattr(msg, name) if not name.startswith('__') and not callable(value) and not name.startswith('_'): dict[name] = value print(dict) #print(type(dict)) #对文本的处理 if dict['type']=='text': ser=search() result,spaceCount,isChinese=ser.main(dict['content']) print(result) #print(spaceCount) # src = result_text['translateResult'][0][0]['src'] tgt = result['translateResult'][0][0]['tgt'] type=result['type'] print("\ntgt:"+tgt) print(type) if spaceCount==0 and not isChinese: print("te") re_str='' if type == 'en2zh-CHS': if "smartResult" in result: re = result['smartResult']['entries'] while '' in re: re.remove('') while ' \r\n' in re: re.remove(' \r\n') re_str = ''.join(re[0:]) #print(re_str) ret="基本解释\n"+tgt+"\n更多解释\n"+re_str else: ret=tgt if type == 'fr2zh-CHS': ret = "法语,翻译--->" + tgt elif type == 'de2zh-CHS': ret = "德语,翻译--->" + tgt else: ret=tgt if type=='fr2zh-CHS': ret="法语,翻译--->"+tgt elif type=='de2zh-CHS': ret="德语,翻译--->"+tgt print(ret) reply=TextReply(content=ret,message=msg) xml=reply.render() resp.body=(xml) resp.status=falcon.HTTP_200 #对图片的处理 elif dict['type']=='image': print ("hello world5") reply = ImageReply(media_id=msg.media_id, message=msg) xml = reply.render() resp.body = (xml) resp.status = falcon.HTTP_200 #对事件的处理 elif dict['type']=='event': if dict['event']=='subscribe': print("欢迎关注") reply = TextReply(content="欢迎关注,这是一个翻译小平台,",message=msg) xml = reply.render() resp.body = (xml) resp.status = falcon.HTTP_200 #对音频的处理 elif dict['type']=='voice': reply = VoiceReply(media_id=msg.media_id,message=msg) xml = reply.render() resp.body = (xml) resp.status = falcon.HTTP_200
def wechatHome(request): # get signature, timestamp and nonce signature = request.GET.get('signature') timestamp = request.GET.get('timestamp') nonce = request.GET.get('nonce') encrypt_type = request.GET.get('encrypt_type') msg_signature = request.GET.get('msg_signature') try: check_signature(TOKEN, signature, timestamp, nonce) if request.method == 'GET': response = request.GET.get('echostr') return HttpResponse(response, content_type="application/json") # POST request if encrypt_type != None: # encryption mode print("request is encrypt") from wechatpy.crypto import WeChatCrypto crypto = WeChatCrypto(TOKEN, AES_KEY, APPID) try: # now msg is xml msg = crypto.decrypt_message(request.body, msg_signature, timestamp, nonce) except (InvalidSignatureException, InvalidAppIdException): HttpResponseBadRequest("decrypt message error") else: msg = request.body # plaintext mode print("before parse_message") pprint.pprint(msg) msg = wechatpy.parse_message(msg) print("after parse_message") pprint.pprint(msg) if msg.type == 'text': # text message reply = TextReply(message=msg) reply.content = 'text reply' elif msg.type == 'image': reply = ImageReply(message=msg) reply.media_id = msg.media_id elif msg.type == 'voice': reply = VoiceReply(message=msg) reply.media_id = 'voice media id' elif msg.type == 'video': reply = VideoReply(message=msg) reply.media_id = 'video media id' reply.title = 'video title' reply.description = 'video description' elif msg.type == 'music': reply = MusicReply(message=msg) reply.thumb_media_id = 'thumb media id' reply.title = 'music title' reply.description = 'music description' reply.music_url = 'music url' reply.hq_music_url = 'hq music url' elif msg.type == 'news': reply = ArticlesReply(message=msg, articles=[ { 'title': u'标题1', 'description': u'描述1', 'url': u'http://www.qq.com', }, ]) else: reply = create_reply('Sorry, can not handle this for now', msg) return HttpResponse(reply.render(), content_type="application/json") except InvalidSignatureException as e: print("check_signature failed") HttpResponseBadRequest(e)