def active_send_news_msg(self, openid, news_list): logging.info('ENTRANCE: openid:%s, news list:%s', openid, str(news_list)) datadict = { 'touser':openid, 'msgtype':'news', 'news':{ 'articles':news_list } } postdata = json.dumps(datadict, ensure_ascii=False).encode('utf-8') # encode utf-8 manually try: count = MAX_RETRY_COUNT while count >= 0: url = 'https://api.weixin.qq.com/cgi-bin/message/send?access_token=' + self.__access_token headers = {'Content-Type':'application/json; encoding=utf-8'} post = postdata resp = network_helper.send_url_request(url=url, headers=headers, post=post) logging.info(resp) respdict = json.loads(resp) ret = self.__handle_errcode(respdict) if ret == 0: return 0 elif ret == 1: count -= 1 continue # access token expired, retry else: return ret except Exception, e: logging.error(traceback.format_exc()) return -1
def create_menu(self, button_list): logging.info('ENTRANCE: button list:%s', str(button_list)) create_menu_post_data = { 'button': button_list } postdata = json.dumps(create_menu_post_data, ensure_ascii=False).encode('utf-8') # encode utf-8 manually try: count = MAX_RETRY_COUNT while count >= 0: url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=' + self.__access_token headers = {'Content-Type':'application/json; encoding=utf-8'} post = postdata resp = network_helper.send_url_request(url=url, headers=headers, post=post) logging.info(resp) respdict = json.loads(resp) ret = self.__handle_errcode(respdict) if ret == 0: return 0 elif ret == 1: count -= 1 continue # access token expired, retry else: return ret except Exception, e: logging.error(traceback.format_exc()) return -1
def custom_send_video_msg(self, openid, media_id, title, desc): logging.info('ENTRANCE: openid:%s, media_id:%s, title:%s, desc:%s', openid, media_id, title, desc) datadict = { 'touser':openid, 'msgtype':'video', 'video': { 'media_id':media_id, 'title':title, 'description':desc } } postdata = json.dumps(datadict, ensure_ascii=False).encode('utf-8') # encode utf-8 manually try: count = MAX_RETRY_COUNT while count >= 0: url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' + self.__access_token headers = {'Content-Type':'application/json; encoding=utf-8'} post = postdata resp = network_helper.send_url_request(url=url, headers=headers, post=post) logging.info(resp) respdict = json.loads(resp) ret = self.__handle_errcode(respdict) if ret == 0: return 0 elif ret == 1: count -= 1 continue # access token expired, retry else: return ret except Exception, e: logging.error(traceback.format_exc()) return -1
def get_followers(self): logging.info('ENTRANCE:') try: uinlist = [] next = '' while True: count = MAX_RETRY_COUNT while count >= 0: url = ('https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s&next_openid=%s' % (self.__access_token, next)) headers = {'Content-Type':'application/json; encoding=utf-8'} resp = network_helper.send_url_request(url=url, headers=headers) logging.info(resp) respdict = json.loads(resp) ret = self.__handle_errcode(respdict) if ret == 0: break elif ret == 1: count -= 1 continue # access token expired, retry else: return (ret, []) openids = [] if 'data' in respdict: openids = respdict['data']['openid'] next = respdict['next_openid'] uinlist += openids # Finish all, break while loop if next == '': break return uinlist except Exception, e: logging.error(traceback.format_exc()) return (-1, [])
def show_qrcode(self, ticket): logging.info('ENTRANCE: ticket:%s', ticket) new_ticket = urllib.quote(ticket) # Note: encode ticket here, or will sometimes failed logging.info('urlencode ticket: %s', new_ticket) try: url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' + new_ticket resp = network_helper.send_url_request(url=url) return resp except Exception, e: logging.error(traceback.format_exc()) return None
def __refresh_accesstoken(self): logging.info('ENTRANCE: appid:%s, secretkey:%s', self.__appid, self.__secretkey) try: url = ('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s' % (self.__appid, self.__secretkey)) headers = {'Content-Type':'application/json; encoding=utf-8'} resp = network_helper.send_url_request(url=url, headers=headers) logging.info(resp) # save new access_token respdict = json.loads(resp) if 'access_token' not in respdict: return -2 self.__access_token = respdict['access_token'] self.__save_access_token_to_file() logging.info('new ACCESS_TOKEN:%s', self.__access_token) return 0 except Exception, e: logging.error(traceback.format_exc()) return -1
def delete_menu(self): logging.info('ENTRANCE:') try: count = MAX_RETRY_COUNT while count >= 0: url = 'https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=' + self.__access_token headers = {'Content-Type':'application/json; encoding=utf-8'} resp = network_helper.send_url_request(url=url, headers=headers) logging.info(resp) respdict = json.loads(resp) ret = self.__handle_errcode(respdict) if ret == 0: return 0 elif ret == 1: count -= 1 continue # access token expired, retry else: return ret except Exception, e: logging.error(traceback.format_exc()) return -1
def create_qrcode(self, sceneid): logging.info('ENTRANCE: sceneid:%s', sceneid) datadict = { 'expire_seconds':1800, 'action_name':'QR_SCENE', 'action_info': { 'scene': { 'scene_id':sceneid } } } postdata = json.dumps(datadict, ensure_ascii=False).encode('utf-8') # encode utf-8 manually try: count = MAX_RETRY_COUNT while count >= 0: url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=' + self.__access_token headers = {'Content-Type':'application/json; encoding=utf-8'} post = postdata resp = network_helper.send_url_request(url=url, headers=headers, post=post) logging.info(resp) respdict = json.loads(resp) ret = self.__handle_errcode(respdict) if ret == 0: if 'ticket' in respdict: return (0, respdict['ticket']) else: return (0, '') elif ret == 1: count -= 1 continue # access token expired, retry else: return (ret, '') except Exception, e: logging.error(traceback.format_exc()) return (-1, '')
def get_user_info(self, openid): logging.info('ENTRANCE: openid:%s', openid) try: count = MAX_RETRY_COUNT while count >= 0: url = ('https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s' % (self.__access_token, openid)) headers = {'Content-Type':'application/json; encoding=utf-8'} resp = network_helper.send_url_request(url=url, headers=headers) logging.info(resp) respdict = json.loads(resp) ret = self.__handle_errcode(respdict) if ret == 0: return (0, respdict) elif ret == 1: count -= 1 continue # access token expired, retry else: return (ret, {}) except Exception, e: logging.error(traceback.format_exc()) return (-1, {})