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 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 test_image_reply_properties(self): from wechatpy.replies import ImageReply reply = ImageReply(image='7890') self.assertEqual('7890', reply.image) self.assertEqual('7890', reply.media_id) reply.media_id = '123456' self.assertEqual('123456', reply.image) self.assertEqual('123456', reply.media_id)
def test_image_reply_properties(self): from wechatpy.replies import ImageReply reply = ImageReply(image="7890") self.assertEqual("7890", reply.image) self.assertEqual("7890", reply.media_id) reply.media_id = "123456" self.assertEqual("123456", reply.image) self.assertEqual("123456", reply.media_id)
def test_image_reply_properties(self): from wechatpy.replies import ImageReply reply = ImageReply(image='7890') self.assertEqual('7890', reply.image) self.assertEqual('7890', reply.media_id) reply.media_id = '123456' self.assertEqual('123456', reply.image) self.assertEqual('123456', reply.media_id)
def subscribeReply(self, msg, content=None, media_id=None): if content == None and media_id == None: raise Exception("no info be send!") elif content != None and media_id != None: raise Exception("content and media_id args only one != None") elif content != None: reply = create_reply(content, message=msg) xml = reply.render() return xml else: reply = ImageReply(message=msg) reply.media_id = media_id xml = reply.render() return xml
def image_reply(msg): reply = ImageReply(message=msg) id = random.randint(0, 7) if id == 0: reply.media_id = "3nZjDfLSZGG6pM1moOgVpiIxl77Ii501riZPHS7NdOY" elif id == 1: reply.media_id = "3nZjDfLSZGG6pM1moOgVpm4_XftO2zaJxsCO9KHOpZE" elif id == 2: reply.media_id = "3nZjDfLSZGG6pM1moOgVphls0UeRqcAHael9a_KTrM4" elif id == 3: reply.media_id = "3nZjDfLSZGG6pM1moOgVpteYUBCK3Evkc3YYnQcDf3E" elif id == 4: reply.media_id = "3nZjDfLSZGG6pM1moOgVppGMwQTVoaNRb5nQMIPYMmE" elif id == 5: reply.media_id = "3nZjDfLSZGG6pM1moOgVpp21QXwC-r14DremI0PdZjQ" elif id == 6: reply.media_id = "3nZjDfLSZGG6pM1moOgVpgWdPB8IBUo57Avk3Med_4k" else: k = msg.media_id reply.media_id = "%s" % k return reply.render()
def post(self, request): msg = parse_message(request.body) print(msg) if msg.type == 'text': reply = create_reply('这是条文字消息', message=msg) elif msg.type == 'image': reply = create_reply('这是条图片消息', message=msg) elif msg.type == 'voice': reply = create_reply('这是条语音消息', message=msg) elif msg.type == 'event': openid = request.GET.get('openid', None) print(openid) if msg.event == 'subscribe': reply = create_reply('感谢你的关注', message=msg) inf = client.user.get('o-Njg0grnFp0LNHidvhKNek6_H88') # print(user["nickname"]) Users.objects.create(nickname=inf["nickname"], headimgurl=inf["headimgurl"], sex=inf["sex"], country=inf["country"], province=inf["province"], city=inf["city"], subscribe_time=inf["subscribe_time"], openid=inf["openid"]) elif msg.event == 'unsubscribe': Users.objects.get(openid=openid).delete() elif msg.event == 'click': reply = ImageReply(message=msg) media_id = '9kT9-alo_ph3g2I45zACW5X59Dqxbf45k-0Z89XXOSta_H_gqfnAhJvG557pqOEM' reply.media_id = media_id Menu_click_count.picture_click_count += 1 elif msg.event == 'view': Menu_click_count.url_click_count += 1 else: reply = create_reply('这是条其他类型消息', message=msg) return HttpResponse(reply.render(), content_type=" ")
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"
def wechat(): signature = request.args.get("signature", "") timestamp = request.args.get("timestamp", "") nonce = request.args.get("nonce", "") encrypt_type = request.args.get("encrypt_type", "raw") msg_signature = request.args.get("msg_signature", "") try: check_signature(TOKEN, signature, timestamp, nonce) except InvalidSignatureException: abort(403) if request.method == "GET": echo_str = request.args.get("echostr", "") return echo_str # POST request if encrypt_type == "raw": # plaintext mode date = (datetime.datetime.now() + datetime.timedelta(days=-1)).strftime("%Y-%m-%d") msg = parse_message(request.data) # if msg.type == "text": # reply = create_reply(msg.content, msg) code = msg.content if code[0] == '6': code += ".XSHG" strPePercent = JqDataSDKOperate.get_stock_pe_and_pb( code, date, 2500) strReply = code + "; " + strPePercent reply = create_reply(strReply, msg) return reply.render() elif code[0] == '0' or code[0] == '3': code += ".XSHE" strPePercent = JqDataSDKOperate.get_stock_pe_and_pb( code, date, 2500) strReply = code + "; " + strPePercent reply = create_reply(strReply, msg) return reply.render() # elif code == '行业': elif code == '行业': reply = ImageReply(message=msg) reply.media_id = get_media_id(client, msg) return reply.render() # else: # reply = create_reply("Sorry, can not handle this for now", msg) # return reply.render() else: # encryption mode from wechatpy.crypto import WeChatCrypto crypto = WeChatCrypto(TOKEN, AES_KEY, APPID) try: msg = crypto.decrypt_message(request.data, msg_signature, timestamp, nonce) except (InvalidSignatureException, InvalidAppIdException): abort(403) else: msg = parse_message(msg) if msg.type == "text": reply = create_reply(msg.content, msg) else: reply = create_reply("Sorry, can not handle this for now", msg) return crypto.encrypt_message(reply.render(), nonce, timestamp)
def imgreply(msg,id): reply = ImageReply(message=msg) reply.media_id = id xml = reply.render() return xml