def send_article(wx_appid, wx_app_secret, token, openid, articles): """ 向用户发送文章 """ wx = WX(wx_appid, wx_app_secret, token) for article in articles: if not article.get('title'): article['title'] = '' if not article.get('description'): article['description'] = '' result = wx.send_common_message(openid, msgtype='news', content={'articles': articles}) return _check_error(result)
def send_voice(token, openid, files): """ 发送语音 """ wx = WX(token) if isinstance(files, str) or isinstance(files, unicode): res = requests.get(files) content = res.content else: media = files['media'] content = media.stream filename = random_ascii_string(10) + '.amr' result_1 = wx.add_media('voice', [('voice', (filename, content, 'audio/amr'))]) result_1 = _check_error(result_1) media_id = result_1.get('media_id') result_2 = wx.send_common_message(openid, msgtype='voice', content={'media_id': media_id}) return _check_error(result_2)