def test_multi_article_reply(self): from wechatpy.replies import ArticlesReply article = {'title': 'test', 'url': 'http://www.qq.com'} r1 = ArticlesReply() r1.add_article(article) r2 = ArticlesReply() self.assertTrue(r1.render() != r2.render())
def test_multi_article_reply(self): from wechatpy.replies import ArticlesReply article = {"title": "test", "url": "http://www.qq.com"} r1 = ArticlesReply() r1.add_article(article) r2 = ArticlesReply() self.assertTrue(r1.render() != r2.render())
def wechat(club_name): logger.debug(club_name) query = request.args logger.debug(query) signature = query.get("signature", "") timestamp = query.get("timestamp", "") nonce = query.get("nonce", "") logger.debug(request.args) try: check_signature(TOKEN, signature, timestamp, nonce) except Exception as e: logger.debug("invalid request!") abort(403) if request.method == "GET": return make_response(request.args.get("echostr", "")) else: logger.debug("start make response") encrypt_type = request.args.get("encrypt_type", "raw") xml = request.data msg = None if encrypt_type == "raw": # plain mode logger.debug("plain mode") msg = parse_message(xml) else: try: # encrypt mode crypto = WeChatCrypto(TOKEN, AES_KEY, APP_ID) msg = parse_message( crypto.decrypt_message(xml, signature, timestamp, nonce)) except Exception as e: abort(403) reply_xml = None if msg.type == "text": key_words = [item.strip() for item in str(msg.content).split(" ")] articles = app_controller.search_club_service_article( club_name, key_words) for article in articles: article["image"] = "{}{}".format(get_host(), article["image"]) article["url"] = "{}{}".format(get_host(), article["url"]) reply = ArticlesReply(articles=articles, message=msg) reply_xml = reply.render() else: reply = TextReply(content="Not supported!", message=msg) reply_xml = reply.render() logger.debug("xml:" + reply_xml) if encrypt_type == "raw": return reply_xml else: return crypto.encrypt_message(reply_xml, nonce, timestamp)
def handle_msg(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') check_signature(TOKEN, signature, timestamp, nonce) return HttpResponse(echo_str) elif request.method == 'POST': body = request.body msg = parse_message(body) rep = TextReply() rep.source = msg.target rep.target = msg.source if msg.type == 'event': if msg.event == 'click': if msg.key == 'sports_advice': rep.content = recommend_plan(msg.source) elif msg.key == 'view_info': rep.content = get_info(msg.source) elif msg.key == 'add_bong': rep.content = add_bong(msg.source) elif msg.key == 'add_test': rep.content = add_test(msg.source) elif msg.key == 'add_test_new': rep.content = add_test_new(msg.source) elif msg.key == 'show_today': rep.content = get_datatoday(msg.source) elif msg.key == 'change_remind': rep.content = set_remind(msg.source) elif msg.key == 'build_match': article_rep = ArticlesReply() article_rep.source = msg.target article_rep.target = msg.source article_rep.add_article({ 'title': '创建比赛', 'description': '点此链接以创建比赛', 'image': serverIP+'static/img/run02.jpg', 'url': build_match(msg.source) }) repxml = article_rep.render() return HttpResponse(repxml) elif msg.event == 'subscribe': rep.content = create_newuser(msg.source) else: rep.content = '!!!' else: rep.content = '你好' repxml = rep.render() return HttpResponse(repxml)
def wechat(): if request.method=='GET': token='****' data=request.args signature=data.get('signature','') timestamp=data.get('timestamp','') nonce=data.get('nonce','') echostr=data.get('echostr','') try: check_signature(token,signature,timestamp,nonce) except InvalidSignatureException: return "" return echostr else: try: msg=parse_message(request.data) except InvalidSignatureException: return "" if msg.type=='text': retmsg=[{"title": "检索结果","image": "http://*.*.*.*:*/*.jpg", "url": u"http://*.*.*.*:*/*.php?title="+msg.content},] reply = ArticlesReply(message=msg, articles=retmsg) return reply.render() if msg.type=='image': image_content='图片.jpg' reply=create_reply(image_content,msg) return reply.render() if msg.type=='voice': voice_content='喂喂喂?' reply=create_reply(voice_content,msg) return reply.render() if msg.type=='event': welcome_content='欢迎关注我的微信公众号~~ 直接输入关键字可检索有关Github项目' reply=create_reply(welcome_content,msg) return reply.render() else : return ""
def test_handler(recv_msg, *args, **kwargs): reply = ArticlesReply() reply.source = recv_msg.to_user_name reply.target = recv_msg.from_user_name reply.add_article({ 'title': '测试图文消息', 'description': '图文消息描述', 'image': 'http://pic1.win4000.com/pic/b/6e/5aee949474.jpg', 'url': 'http://www.baidu.com' }) reply.add_article({ 'title': '测试图文消息', 'description': '图文消息描述', 'image': 'http://pic1.win4000.com/pic/b/6e/5aee949474.jpg', 'url': 'http://www.baidu.com' }) xml = reply.render() return HttpResponse(xml)
def send_message(request): if request.method == 'GET': # 验证URL signature = request.GET.get('signature', '') timestamp = request.GET.get('timestamp', '') nonce = request.GET.get('nonce', '') echo_str = request.GET.get('echostr', '') try: check_signature(AUTH_TOKEN, signature, timestamp, nonce) except InvalidSignatureException: echo_str = 'error' response = HttpResponse(echo_str, content_type='text/plain') return response elif request.method == 'POST': # 接收来微信服务器信息 msg = parse_message(request.body) if msg.type == 'text': reply = create_reply("<a href='https://articuly.com'>清心涟漪博客</a>", msg) elif msg.type == 'event': reply = ArticlesReply(message=msg) reply.add_article({ 'title': '清心涟漪博客', 'description': '因缘相见,整合图谱,心理占星,Python全栈', 'image': 'https://articuly.com/wp-content/uploads/2014/07/articuly.jpg', 'url': 'https://articuly.com' }) elif msg.type == 'image': reply = create_reply('你刚才发给我的是一张图片', msg) elif msg.type == 'voice': reply = create_reply('你刚才发给我的是语音', msg) else: reply = create_reply('这是其它类型消息', msg) response = HttpResponse(reply.render(), content_type='application/xml') return response else: print('-' * 50)
def send_article(toUser, fromUser): reply = ArticlesReply() reply.source = fromUser reply.target = toUser reply.add_article({"title":"圣光会制裁你!", "description":"The Light Shall Bring Victory!", "image":LIGHT, "url":"http://baike.baidu.com/link?url=QBxKSNA79HbeeaSwsbkcPHdhhVI5ZM9HePZrUpQCwtwKx9xShsZ1vOCd35UuNYP0SxaedrA2UIaPLMvFVW5wD_"}) return HttpResponse(reply.render())
def wechat(request): """ 此地址为响应微信发送的Token验证,验证服务器配置是否正确 :param request: :return: """ signature = request.GET.get('signature', 'c58469c4151fac046efe180b277c51b1e5b563d3') timestamp = request.GET.get('timestamp', '1451138472') nonce = request.GET.get('nonce', '1432579014') echo_str = request.GET.get('echostr', '2691756735856574460') encrypt_type = request.args.get('encrypt_type', '') msg_signature = request.args.get('msg_signature', '') print('signature:', signature) print('timestamp: ', timestamp) print('nonce:', nonce) print('echo_str:', echo_str) print('encrypt_type:', encrypt_type) print('msg_signature:', msg_signature) try: check_signature(settings.WECHAT_TOKEN, signature, timestamp, nonce) except InvalidSignatureException: return HttpResponseForbidden() if request.method == 'GET': return echo_str else: print('Raw message: \n%s' % request.data) crypto = WeChatCrypto(settings.WECHAT_TOKEN, settings.EncodingAESKey, settings.WECHAT_APPID) try: msg = crypto.decrypt_message( request.data, msg_signature, timestamp, nonce ) print('Descypted message: \n%s' % msg) except (InvalidSignatureException, InvalidAppIdException): return HttpResponseForbidden() msg = parse_message(msg) if msg.type == 'text': reply = create_reply(msg.content, msg) elif msg.type == 'image': reply = ArticlesReply(message=msg) # simply use dict as article reply.add_article({ 'title': 'test', 'description': 'test', 'image': 'image url', 'url': 'url' }) # or you can use ObjectDict article = ObjectDict() article.title = 'test' article.description = 'test' article.image = 'image url' article.url = 'url' reply.add_article(article) # reply = create_reply([article], msg) else: reply = create_reply('Sorry, can not handle this for now', msg) msg= crypto.encrypt_message( reply.render(), nonce, timestamp) print(msg) return HttpResponse(msg)
def home(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(WECHAT_TOKEN, signature, timestamp, nonce) except InvalidSignatureException: echo_str = 'error' response = HttpResponse(echo_str, content_type="text/plain") return response elif request.method == 'POST': msg = parse_message(request.body) if msg.type == 'text': muser = MagicUser.objects.get_or_create(openid=msg.source) print(muser[0].snumber) if '成绩' in msg.content and '更新' not in msg.content: reply = create_reply( str(muser[0].uuid) + str(msg.content), msg) print(str(muser[0].uuid)) if None == muser[0].snumber: url = BASEURL + reverse('index') + '?uuid=' + str( muser[0].uuid) + "&type=chengji&create=" + str( muser[1]) else: url = BASEURL + reverse('chengji') + '?uuid=' + str( muser[0].uuid) reply = ArticlesReply(message=msg) reply.add_article({ 'title': '成绩', 'description': '成绩', 'image': BASEURL + '/static/mags/imgs/weixinmsggrade.png', 'url': url }) elif '课表' in msg.content and '更新' not in msg.content: reply = create_reply( str(muser[0].uuid) + str(msg.content), msg) if None == muser[0].snumber: url = BASEURL + reverse('index') + '?uuid=' + str( muser[0].uuid) + "&type=kebiao&create=" + str(muser[1]) else: url = BASEURL + reverse('kebiao') + '?uuid=' + str( muser[0].uuid) reply = ArticlesReply(message=msg) reply.add_article({ 'title': '课表', 'description': '课表', 'image': 'http://pic1.sc.chinaz.com/files/pic/pic9/201803/zzpic10731.jpg', 'url': url }) elif '更新成绩' in msg.content or '更新课表' in msg.content: if None == muser[0].snumber: url = BASEURL + reverse('index') + '?uuid=' + str( muser[0].uuid) + "&type=chengji&create=" + str( muser[1]) else: url = BASEURL + reverse('gengxin') + '?uuid=' + str( muser[0].uuid) reply = ArticlesReply(message=msg) reply.add_article({ 'title': '更新成绩和课表', 'description': '更新成绩和课表点击前往更新', 'image': 'http://pic1.sc.chinaz.com/files/pic/pic9/201803/zzpic10731.jpg', 'url': url }) else: flag = False buildings = [ '致高楼A幢', '管理楼', '致用楼', '科学会堂', '致高楼B幢', '水利馆', '闻天馆', '博学楼', '工程馆', '北教楼', '致远楼', '江宁体育场', '研究生综合楼', '水文楼', '励学楼', '勤学楼' ] for i in buildings: if msg.content in i: flag = True break if flag: d = datetime.datetime.now() index = d.weekday() #false空闲 true有课 #西康路校区 江宁校区 江宁西校区 常州校区 todayres = test_curl_fetch( 'http://map.hhu.edu.cn/mapi/api/v2.0/classRoom.json?day=' + str(index)) tomorrowres = test_curl_fetch( 'http://map.hhu.edu.cn/mapi/api/v2.0/classRoom.json?day=' + str((index + 1) % 7)) todayjisoshi = json.loads(todayres)['data'] tomorrowjisoshi = json.loads(tomorrowres)['data'] today = '您查询的教学楼为: {}\n今天空闲的教室有:'.format(msg.content) tomorrow = '\n明天空闲的教室有:'.format(msg.content) for i in todayjisoshi: if msg.content in i['buildName']: today += i['code'] + ' ' for index, j in enumerate(i['lessons']): if not j: today += str(index + 1) + ' ' today += '小节|' for i in tomorrowjisoshi: if msg.content in i['buildName']: tomorrow += i['code'] + ' ' for index, j in enumerate(i['lessons']): if not j: today += str(index + 1) + ' ' tomorrow += '小节|' reply = create_reply(today, msg) else: reply = create_reply('暂不支持相关信息的查询', msg) elif msg.type == 'image': reply = create_reply('这是条图片消息', msg) elif msg.type == 'voice': reply = create_reply('这是条语音消息', msg) else: reply = create_reply('这是条其他类型消息', msg) response = HttpResponse(reply.render(), content_type="application/xml") return response
def Reply_Article(msg, reply_dic): reply = ArticlesReply(message=msg) # simply use dict as article reply.add_article(reply_dic) return reply.render()