def run(): API = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=' wx_access_token = wxtoken.get_token('neihan_mp') params = { "button": [{ "type": "click", "name": "成为代理", "key": "V1001_PROMO" }, { "type": "click", "name": "推广", "key": "V1001_QRCODE" }, # { # "type": "click", # "name": "小程序", # "key": "V1001_APP" # }, { "type": "media_id", "name": "客服", "media_id": "2GVOdSI8OeOxU9lgcwa_Qt0REBdqJQPMQ01j2c9Q-qg", }] } api = API + wx_access_token['access_token'] resp = requests.post(api, json.dumps(params, ensure_ascii=False)) if resp and resp.status_code == 200: content = resp.json() if content['errcode'] == 0: logging.info('菜单创建成功!') else: logging.info('菜单创建失败'+content['errmsg'].encode('utf8')) else: logging.info('菜单创建失败')
def template_list(): token = get_token() api = 'https://api.weixin.qq.com/cgi-bin/wxopen/template/library/list?access_token='+token['access_token'] params = {'offset': 0, 'count': 5, 'access_token': token['access_token']} resp = requests.post(api, params) if resp and resp.status_code == 200: data = resp.json() print data
def send_msg(arg): global total_send u = arg['u'] video = arg['video'] formids = _mgr.get_user_formid(u['id']) if not formids: logging.info('用户{}-{}无有效的formid'.format(u['id'], u['user_name'].encode('utf8'))) return None if arg['ulevel'] > 0: if len(formids) < 2: return None formid = formids[0] params = { "touser": u['openid'].encode('utf8'), "template_id": TEMPLATE_ID[u['source']], "page": "pages/index/index?video_id={}&from_user_id={}".format(video['group_id'].encode('utf8'), video['from_user_id'].encode('utf8')), "form_id": formid['form_id'].encode('utf8'), "data": { "keyword1": { "value": video['title'].encode('utf8'), "color": "#173177" }, "keyword2": { "value": video['comment'].encode('utf8'), "color": "#173177" } } } _mgr.user_formid_used(formid['id']) access_token = wxtoken.get_token(u['source']) api = WX_MSG_API + access_token['access_token'] resp = requests.post(api, json.dumps(params, ensure_ascii=False)) print resp if resp and resp.status_code == 200: content = resp.json() if content['errcode'] == 0: total_send += 1 mdetail = { 'message_id': 0, 'from_user_id': video['from_user_id'], 'group_id': video['group_id'], 'user_id': u['id'] } _mgr.save_message_send_detail(mdetail) logging.info('用户{}-{}的消息推送成功'.format(u['openid'], u['user_name'].encode('utf8'))) else: logging.info('用户{}的消息推送失败, 失败原因{}'.format(u['openid'], content['errmsg']))
def run(): wx_access_token = wxtoken.get_token('neihan_mp') api = 'https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=' + wx_access_token[ 'access_token'] params = {"type": 'news', "offset": 0, "count": 20} resp = requests.post(api, params) print resp if resp and resp.status_code == 200: content = resp.json() print content if content['errcode'] == 0: logging.info('获取素材列表成功!') else: logging.info('获取素材列表失败') else: logging.info('获取素材列表失败')
def run(filename=''): if not os.path.isfile(filename): logging.info('图片不存在') return None wx_access_token = wxtoken.get_token('neihan_mp') params = {'access_token': wx_access_token['access_token'], 'type': 'image'} api = WX['media_api_p'] + wx_access_token['access_token'] + '&type=image' resp = requests.post(api, params, files={'media': open(filename, 'rb')}) if resp and resp.status_code == 200: content = resp.json() if content.get('media_id', '') != '': logging.info('图片上传成功') print content else: logging.info('图片上传失败' + content['errmsg'].encode('utf8')) else: logging.info('图片上传失败')
def main(): global total_send while True: total_send = 0 msetting = _mgr.get_message_setting() if msetting['status'] != 1: logging.info('消息推送已关闭') sleep(30) continue uparams = {'is_sended': 0, 'send_time': datetime.now()} users = _mgr.get_special_message_tasks(uparams) if users: pools = Pool(WORKER_THREAD_NUM) while len(users): args = [] for x in xrange(WORKER_THREAD_NUM): try: tuser = users.pop() user = _mgr.get_users({'user_id': tuser['user_id']}) access_token = wxtoken.get_token(user[0]['source']) video = _mgr.get_special_video(tuser['user_id']) video['from_user_id'] = 100001 video['title'] = video['content'] args.append({ 'u': user[0], 'video': video, 'access_token': access_token }) except: pass # traceback.print_exc() pools.map(send_msg, args) sleep(3) logging.info('成功发送消息给{}个用户'.format(total_send)) else: logging.info('没有用户,暂停消息推送') sleep(30)
def main(): global total_send while True: total_send = 0 tasks = _mgr.get_message_tasks({ 'is_send': 0, 'send_time': datetime.now() }) if len(tasks) == 0: logging.info('没有消息推送任务') sleep(30) continue for task in tasks: _mgr.update_message_tasks(task['id'], {'is_send': 1}) # video = _mgr.get_videos({'group_id': task['group_id']}) comments = _mgr.get_comment({'group_id': task['group_id']}) tcomment = '' for com in comments: tcomment += "【{}】{}\n".format( com['user_name'].encode('utf8'), com['content'].encode('utf8'), ) uparams = { 'is_active': 1, # 'source': task['app'], 'source': 'neihan_2', 'skip_msg': 0, 'promotion': 0, # 'user_id': 10 } users = _mgr.get_users(uparams) if users: video = { 'from_user_id': task['from_user_id'], 'group_id': task['group_id'], 'title': task['title'], # 'comment': task['comment'] if task['comment'].encode('utf8') else tcomment 'comment': task['comment'] } access_token = wxtoken.get_token(task['app']) pools = Pool(WORKER_THREAD_NUM) while len(users): args = [] for x in xrange(WORKER_THREAD_NUM): try: args.append({ 'message_id': task['id'], 'u': users.pop(), 'video': video, 'ulevel': task['formid_level'], 'access_token': access_token }) except: pass pools.map(send_msg, args) sleep(5) logging.info('成功发送消息给{}个用户'.format(total_send)) _mgr.update_message_tasks(task['id'], {'send_member': total_send}) else: logging.info('没有用户,暂停消息推送') sleep(30)
def send_msg(arg): global total_send u = arg['u'] video = arg['video'] formids = _mgr.get_user_formid(u['id']) if not formids: logging.info('用户{}-{}无有效的formid'.format(u['id'], u['user_name'].encode('utf8'))) return None if arg['ulevel'] > 0: if len(formids) < 2: return None formid = formids[0] params = { "touser": u['openid'].encode('utf8'), "template_id": 'Vpq9PCekMsNMr8zQKC6JporcztCg55RNZUZUizgq5HA', "page": "pages/index/index?video_id={}&from_user_id={}".format( video['group_id'].encode('utf8'), video['from_user_id'].encode('utf8')), "form_id": formid['form_id'].encode('utf8'), "emphasis_keyword": "keyword1.DATA", "data": { "keyword1": { "value": "小编强力推荐!", "color": "#FF0000", }, "keyword2": { "value": video['title'].encode('utf8'), "color": "#FF0000", }, "keyword3": { "value": video['comment'].encode('utf8'), "color": "#173177" } } } _mgr.user_formid_used(formid['id']) access_token = wxtoken.get_token(u['source']) api = WX_MSG_API + access_token['access_token'] resp = requests.post(api, json.dumps(params, ensure_ascii=False), timeout=120) print resp if resp and resp.status_code == 200: content = resp.json() if content['errcode'] == 0: total_send += 1 logging.info('用户{}-{}的消息推送成功'.format( u['openid'], u['user_name'].encode('utf8'))) else: logging.info('用户{}-{}的消息推送失败, 失败原因{}'.format( u['openid'], u['user_name'].encode('utf8'), content['errmsg']))