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 POST(self): try: xml = web.data() msg = parse_message(xml) if msg.type == 'text': content = msg.content if content.startswith(('表情包:', '表情包:')): words = ''.join(content.replace(':', ':').split(':')[1:]) wechat_client = WeChatClient( appid='wx19a2591b2a719add', secret='c46fa65dbc2803b90431fbf9c803cbd4', ) access_token = wechat_client.access_token json_data = pics.upload_pic(pics.get_random_pic(words), access_token) # print(json_data) if 'errcode' not in json_data: media_id = json_data['media_id'] reply = ImageReply(message=msg) reply.media_id = media_id # print(web.url() + ' get_pic. words: ' + words + ' return: ' + str(json_data)) logger.info(web.url() + ' get_pic. words: ' + words + ' return: ' + str(json_data)) else: reply = TextReply(message=msg) reply.content = json_data['link'] logger.warning(web.url() + ' get_pic faild,return link. words:' + words + ' return: ' + json_data['link']) elif content.startswith(('影视:', '影视:')): words = ''.join(content.replace(':', ':').split(':')[1:]) if words == '': string = '没有输入要搜索的名字!' logger.info(web.url() + ' get_movie without words') else: data = search(words) data1 = data[0] data2 = data[1] if len(data1) != 0: string = '' if len(data1) <= 12: for each in data1: string += '%s %s %s %s\n' % (each[0], each[1], each[3], each[4]) else: for each in data1[:12]: string += '%s %s %s %s\n' % (each[0], each[1], each[3], each[4]) elif len(data2) != 0: string = '' if len(data2) <= 12: for each in data2: string += '%s %s %s %s\n' % (each[0], each[1], each[3], each[4]) else: for each in data2[:12]: string += '%s %s %s %s\n' % (each[0], each[1], each[3], each[4]) else: string = '竟然没有搜索到!!!\n请检查名称输入的是否正确,请尽量使用中文哦' # print(web.url() + ' get_pic. words: ' + words + ' data: ' + str(data) + ' return: ' + string) logger.info(web.url() + ' get_movie. words: ' + words + ' data: ' + str(data) + ' return: ' + string) reply = TextReply(message=msg) reply.content = string elif content.startswith(('在线:', '在线:')): words = ''.join(content.replace(':', ':').split(':')[1:]) if words == '': string = '没有输入要搜索的名字!' logger.info(web.url() + ' see_movie without words') else: data = get_link(words) if data: string = data else: string = '竟然没有搜索到!!!\n请检查名称输入的是否正确,请试试英文名哦' # print(web.url() + ' see_pic. words: ' + words + ' data: ' + str(data) + ' return: ' + string) logger.info(web.url() + ' see_movie. words: ' + words + ' data: ' + str(data) + ' return: ' + string) reply = TextReply(message=msg) reply.content = string else: reply = TextReply(message=msg) response = get_response(content) logger.info(web.url() + ' turing. words: ' + content + ' response: ' + response) reply.content = response elif msg.type == 'event' and msg.event: mscontent = msg.event if mscontent == "subscribe": string = '终于等到你!欢迎关注Snoopy同学~\n' \ '输入"表情包:xxx"获取自定义文字的表情\n' \ '输入"影视:xxx"获取电影的网盘链接\n' \ '输入"在线:xxx"获取在线观看视频的链接,需要复制到浏览器使用哦~' reply = TextReply(message=msg) reply.content = string elif mscontent == "unsubscribe": string = '有什么不足之处还请谅解,我会慢慢改进,欢迎您以后再来' reply = TextReply(message=msg) reply.content = string # 转换成 XML reply_xml = reply.render() return reply_xml except Exception as e: logger.error(e) # print(e) return "success"