def test_music_reply_properties(self): from wechatpy.replies import MusicReply reply = MusicReply() reply.thumb_media_id = '123456' reply.title = 'test' reply.description = 'test' reply.music_url = 'http://www.qq.com' reply.hq_music_url = None self.assertEqual('123456', reply.thumb_media_id) self.assertEqual('test', reply.title) self.assertEqual('test', reply.description) self.assertEqual('http://www.qq.com', reply.music_url) self.assertTrue(reply.hq_music_url is None) reply = MusicReply(thumb_media_id='123456', title='test', description='test', music_url='http://www.qq.com', hq_music_url=None) self.assertEqual('123456', reply.thumb_media_id) self.assertEqual('test', reply.title) self.assertEqual('test', reply.description) self.assertEqual('http://www.qq.com', reply.music_url) self.assertTrue(reply.hq_music_url is None)
def test_music_reply_properties(self): from wechatpy.replies import MusicReply reply = MusicReply() reply.thumb_media_id = "123456" reply.title = "test" reply.description = "test" reply.music_url = "http://www.qq.com" reply.hq_music_url = None self.assertEqual("123456", reply.thumb_media_id) self.assertEqual("test", reply.title) self.assertEqual("test", reply.description) self.assertEqual("http://www.qq.com", reply.music_url) self.assertTrue(reply.hq_music_url is None) reply = MusicReply( thumb_media_id="123456", title="test", description="test", music_url="http://www.qq.com", hq_music_url=None, ) self.assertEqual("123456", reply.thumb_media_id) self.assertEqual("test", reply.title) self.assertEqual("test", reply.description) self.assertEqual("http://www.qq.com", reply.music_url) self.assertTrue(reply.hq_music_url is None)
def test_music_reply_properties(self): from wechatpy.replies import MusicReply reply = MusicReply() reply.thumb_media_id = '123456' reply.title = 'test' reply.description = 'test' reply.music_url = 'http://www.qq.com' reply.hq_music_url = None self.assertEqual('123456', reply.thumb_media_id) self.assertEqual('test', reply.title) self.assertEqual('test', reply.description) self.assertEqual('http://www.qq.com', reply.music_url) self.assertTrue(reply.hq_music_url is None) reply = MusicReply( thumb_media_id='123456', title='test', description='test', music_url='http://www.qq.com', hq_music_url=None ) self.assertEqual('123456', reply.thumb_media_id) self.assertEqual('test', reply.title) self.assertEqual('test', reply.description) self.assertEqual('http://www.qq.com', reply.music_url) self.assertTrue(reply.hq_music_url is None)
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 my_main_api(msg): kuan = Kuan() if msg.key == 'kuan_update': reply = kuan.get_update(msg) elif msg.key == 'kuan_app_push': reply = kuan.get_app_push(msg) elif msg.key == 'kuan_game_push': reply = kuan.get_game_push(msg) elif msg.key == 'day_bing': bing = DayBing() reply = bing.get_image_url(msg) elif msg.key == 'day_music': # http://music.163.com/song/522510615/?userid=311948152 reply = MusicReply( message=msg, title='千禧', thumb_media_id= 'EpZ4EJF9njZOorkr2TnVAquSxXF2eoQW2Bk6DdKS07kycz-hwYnRSLAmfubPNvzh', description='徐秉龙', music_url='http://www.ytmp3.cn/down/54100.mp3', hq_music_url='http://www.ytmp3.cn/down/54100.mp3') elif msg.key == 'm_review': ren = RenRen() news = ren.get_info() reply = ArticlesReply(message=msg) print(news) for new in news: reply.add_article({ 'title': new['title'], 'description': new['description'], 'image': new['image'], 'url': new['url'] }) elif msg.key == 'recom': ren = RenRen() ren.url = 'http://www.zimuzu.tv/article?type=recom' news = ren.get_info() reply = ArticlesReply(message=msg) # print(news) articles = [{ 'thumb_media_id': 'Pp8iDnCrxFlb8CUfmmpGOYuoj3EisK_t4xtHvBA_blg', 'title': '哈哈哈哈', 'content': '红红火火恍恍惚惚红火火恍恍惚惚', 'author': '张宇', 'content_source_url': 'http://mp.weixin.qq.com/s?__biz=MzUxNzk3MTQ5NA==&mid=100000003&idx=1&sn=85ac89b7f597ebae2ea6373d3a2e922b&chksm=79914a4a4ee6c35c3c9c4147ea169692df3a84feeb992d442848f2c67ccd5de1e90e905d7af7#rd', 'digest': '你好', 'show_cover_pic': '1', }] # 上传永久素材 # client = WeChatClient('wx892238d68ae6f860', 'dcb0a16bc13e183f3e20aa0fa30b23e6') # material = WeChatMaterial(client) # 图片 # obj_file = open('test.jpg','rb') # resp = material.add('thumb',obj_file,'缩略图.test') # 图文 # resp = material.add_articles(articles) # 获取素材 # resp = material.get('Pp8iDnCrxFlb8CUfmmpGOaF0tYuqXkQKNn0qufc5HsA') # print(resp) for new in news: reply.add_article({ 'title': new['title'], 'description': new['description'], 'image': new['image'], 'url': 'http://mp.weixin.qq.com/s?__biz=MzUxNzk3MTQ5NA==&mid=100000003&idx=1&sn=85ac89b7f597ebae2ea6373d3a2e922b&chksm=79914a4a4ee6c35c3c9c4147ea169692df3a84feeb992d442848f2c67ccd5de1e90e905d7af7#rd' }) else: reply = '' return reply
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 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)
def do_event(msg): if msg.event == 'subscribe': reply = create_reply('欢迎您关注我的公众号:请输入天气查看当前天气!', msg) elif msg.event == 'unsubscribe': pass elif msg.event == "click": if msg.key == "V1001_TODAY_MUSIC": # 回复音乐信息 reply = MusicReply(message=msg) reply.title = '音乐' reply.description = '今日音乐 ' thumb = MaterialModel.objects.get(id=5) reply.thumb_media_id = thumb.media_id reply.music_url = "http://www.xiami.com/song/1792541433" reply.hq_music_url = "http://www.xiami.com/song/1792541433" elif msg.key == "V1001_TODAY_SINGER": reply = ArticlesReply(message=msg) reply.add_article({ "title": "阿信", "description": "陈信宏(Ashin),昵称阿信,1975年12月6日生于台北市北投区。中国台湾男歌手、词曲创作者,中国台湾摇滚乐团五月天的主唱,创意潮牌STAYREAL总裁。", "image": "https://gss1.bdstatic.com/9vo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=f8e67f4769061d95694b3f6a1a9d61b4/e4dde71190ef76c6f608fd549a16fdfaae5167f2.jpg", 'url': 'https://mp.weixin.qq.com/s?__biz=MzU1OTc0NTQ1OQ==&tempkey=OTgwX2tBU25rdGFHcjRuU0s2THJZRTVRNE5MQ0NMbTg1TmNrU2tRLWRvZWFxQ2ZGd0djSmdtazJoNC1NZXBKdEdlSmFuLU85ZHRJV2JkUlVHeTRienhCSVdQeGZpQ3BzajdBWUJJWC1zSWhfNlJWekc4ZEd5NGVrV2xkckZMTTUwX0FCdlNKN1BHc21JdldHSG1rZF9UT3g3V1NFQUpKcWFxcmNvLUQ3QVF%2Bfg%3D%3D&chksm=7c13d1c14b6458d7daa04b4a5e2737c2293aa2c678abfc3bad271bf63f11a961067f83183d63#rd' }) elif msg.key == 'V1001_ABOUT_ME': reply = ArticlesReply(message=msg) reply.add_article({ "title": "关于我", "description": "也没啥说的", 'image': 'http://i.dyt6.cc/0f/4d/04/cf/ea/c8/b2/f6/b2/79/7f/7f/0c/f5/db/0e.jpg', "url": "https://mp.weixin.qq.com/s/3u6BlqYLx8r9DiLBXbFU5g" }) else: # 回复图片信息 reply = ArticlesReply(message=msg) reply.add_article({ "title": "阿信", "description": "陈信宏(Ashin)", "image": "http://img5.imgtn.bdimg.com/it/u=1430011000,1084796449&fm=26&gp=0.jpg", "url": "https://baike.baidu.com/item/%E9%99%88%E4%BF%A1%E5%AE%8F/334?fr=aladdin" }) reply.add_article({ "title": "百度图库", "description": "图片", 'image': 'http://img3.imgtn.bdimg.com/it/u=1241905237,194043770&fm=26&gp=0.jpg', "url": "http://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=%E9%98%BF%E4%BF%A1" }) reply.add_article({ "title": "百度图库", "description": "图片", 'image': 'http://img1.imgtn.bdimg.com/it/u=2285845240,3067239871&fm=26&gp=0.jpg', }) else: reply = create_reply('我们不知道这个时间类型是什么', msg) return reply