def handle(self): lib = WeChatLib(WECHAT_TOKEN, WECHAT_APPID, WECHAT_SECRET) wechat_menu = lib.get_wechat_menu() if len(wechat_menu) < 2: return self.reply_text('暂时没有可抢票的活动') elif len(wechat_menu[1]['sub_button']) == 0: return self.reply_text('暂时没有可抢票的活动') else: acti_list = wechat_menu[1]['sub_button'] news_list = [] for acti in acti_list: try: activity = Activity.objects.get(name=acti['name']) news_list.append({ 'Title': activity.name, 'Description': activity.description, 'PicUrl': activity.pic_url, 'Url': self.url_activity_detail(activity.id) }) except: return self.reply_text('数据库内出现了重名活动,请联系管理员-_-!') return self.reply_news(news_list)
def get_current_menu_list(self): lib = WeChatLib(WECHAT_TOKEN, WECHAT_APPID, WECHAT_SECRET) wechat_menu = lib.get_wechat_menu() wechat_menu_activity_list = [] for i in range(0, len(wechat_menu[1]['sub_button'])): wechat_menu_activity_list.append( wechat_menu[1]['sub_button'][i]['name']) return wechat_menu_activity_list
class CustomWeChatView(WeChatView): lib = WeChatLib(WECHAT_TOKEN, WECHAT_APPID, WECHAT_SECRET) handlers = [ recentConferenceHandler, allConferenceHandler, myConferenceHandler, SubscribeHandler, NextPageHandler, SearchWaysHandler, SearchConfHandler, MyRemindHandler, AboutConfPlus ] error_message_handler = ErrorHandler default_handler = DefaultHandler event_keys = { 'my_reminds': 'MY_REMINDS', 'my_conference': 'MY_CONFERENCE', 'recent_conference': 'RECENT_CONFERENCE', 'more_history': 'MORE_HISTORY', 'more_search': 'MORE_SEARCH', 'more_about': 'MORE_ABOUT', } menu = { 'button': [{ "name": "我的", "sub_button": [{ "type": "click", "name": "我的会议", "key": event_keys['my_conference'], }, { "type": "click", "name": "我的提醒", "key": event_keys['my_reminds'], }] }, { "type": "click", "name": "近期会议", "key": event_keys['recent_conference'], }, { "name": "更多服务", "sub_button": [{ "type": "click", "name": "全部会议", "key": event_keys['more_history'], }, { "type": "click", "name": "搜索会议", "key": event_keys['more_search'], }, { "type": "click", "name": "关于会佳", "key": event_keys['more_about'], }] }] }
def get_menu_list(self): lib = WeChatLib(WECHAT_TOKEN, WECHAT_APPID, WECHAT_SECRET) menu_list = lib.get_wechat_menu() menu_book_list = [] for btn in menu_list: if btn['name'] == '抢票': menu_book_list += btn.get('sub_button', list()) menu_id_list = [] for btn in menu_book_list: if 'key' in btn: activity_id = btn['key'] if activity_id.startswith( CustomWeChatView.event_keys['book_header']): activity_id = activity_id[len(CustomWeChatView. event_keys['book_header']):] if activity_id and activity_id.isdigit(): menu_id_list.append(int(activity_id)) return menu_id_list
class CustomWeChatView(WeChatView): lib = WeChatLib(WECHAT_TOKEN, WECHAT_APPID, WECHAT_SECRET) handlers = [ HelpOrSubscribeHandler, UnbindOrUnsubscribeHandler, BindAccountHandler, BookEmptyHandler, GetTicketHandler, BookTicketHandle, BookWhatHandler, CancelTicketHandler, CheckTicketHandler ] error_message_handler = ErrorHandler default_handler = DefaultHandler event_keys = { 'book_what': 'SERVICE_BOOK_WHAT', 'get_ticket': 'SERVICE_GET_TICKET', 'account_bind': 'SERVICE_BIND', 'help': 'SERVICE_HELP', 'book_empty': 'BOOKING_EMPTY', 'book_header': 'BOOKING_ACTIVITY_', } menu = { 'button': [{ "name": "服务", "sub_button": [{ "type": "click", "name": "抢啥", "key": event_keys['book_what'], }, { "type": "click", "name": "查票", "key": event_keys['get_ticket'], }, { "type": "click", "name": "绑定", "key": event_keys['account_bind'], }, { "type": "click", "name": "帮助", "key": event_keys['help'], }] }, { "name": "抢票", "sub_button": [] }] }
def get_menu(cls): from wechat.wrapper import WeChatLib if cls.set_menu_lock: return 'Lock' if cls.updated: print('Cached!') return cls.menus current_menu = WeChatLib(WECHAT_TOKEN, WECHAT_APPID, WECHAT_SECRET).get_wechat_menu() existed_button = list() for btn in current_menu: if btn['name'] == '抢票': existed_button += btn.get('sub_button', list()) activity_ids = list() for btn in existed_button: if 'key' in btn: activity_id = btn['key'] if activity_id.startswith('BOOKING_ACTIVITY_'): activity_id = activity_id[len('BOOKING_ACTIVITY_')] if activity_id and activity_id.isdigit(): activity_ids.append(int(activity_id)) cls.menus = activity_ids cls.updated = True return cls.menus
class CustomWeChatView(WeChatView): lib = WeChatLib(WECHAT_TOKEN, WECHAT_APPID, WECHAT_SECRET) handlers = [ HelpOrSubscribeHandler, BindAccountHandler, UnbindOrUnsubscribeHandler, BookEmptyHandler, BookListHandler, TicketBookHandler, TicketDetailHandler, TicketReturnHandler ] error_message_handler = ErrorHandler default_handler = DefaultHandler event_keys = { 'book_what': 'SERVICE_BOOK_WHAT', 'get_ticket': 'SERVICE_GET_TICKET', 'account_bind': 'SERVICE_BIND', 'help': 'SERVICE_HELP', 'book_empty': 'BOOKING_EMPTY', 'book_header': 'BOOKING_ACTIVITY_', } menu = { 'button': [ { "name": "服务", "sub_button": [ { "type": "click", "name": "抢啥", "key": event_keys['book_what'], }, { "type": "click", "name": "查票", "key": event_keys['get_ticket'], }, { "type": "click", "name": "绑定", "key": event_keys['account_bind'], }, { "type": "click", "name": "帮助", "key": event_keys['help'], } ] }, { "name": "抢票", "sub_button": [] } ] } @classmethod def get_book_btn(cls): return cls.menu['button'][-1] @classmethod def update_book_button(cls, activities): book_btn = cls.get_book_btn() if len(activities) == 0: book_btn['type'] = 'click' book_btn['key'] = cls.event_keys['book_empty'] else: book_btn.pop('type', None) book_btn.pop('key', None) book_btn['sub_button'] = list() for act in activities: book_btn['sub_button'].append({ 'type': 'click', 'name': act['name'], 'key': cls.event_keys['book_header'] + str(act['id']), }) @classmethod def update_menu(cls, activities=None): """ :param activities: list of Activity :return: None """ if activities is not None: if len(activities) > 5: cls.logger.warn('Custom menu with %d activities, keep only 5', len(activities)) cls.update_book_button([{'id': act.id, 'name': act.name} for act in activities[:5]]) else: current_menu = cls.lib.get_wechat_menu() existed_buttons = list() for btn in current_menu: if btn['name'] == '抢票': existed_buttons += btn.get('sub_button', list()) activity_ids = list() for btn in existed_buttons: if 'key' in btn: activity_id = btn['key'] if activity_id.startswith(cls.event_keys['book_header']): activity_id = activity_id[len(cls.event_keys['book_header']):] if activity_id and activity_id.isdigit(): activity_ids.append(int(activity_id)) return cls.update_menu(Activity.objects.filter( id__in=activity_ids, status=Activity.STATUS_PUBLISHED, book_end__gt=timezone.now() ).order_by('book_end')[: 5]) cls.lib.set_wechat_menu(cls.menu)
class CustomWeChatView(WeChatView): lib = WeChatLib(WECHAT_TOKEN, WECHAT_APPID, WECHAT_SECRET) handlers = [ BindAccountHandler, GetConferenceListHandler, GetUpcomingConferenceListHandler, GetMyConferenceListHandler, GetHelpHandler, SearchConferenceHandler, ] error_message_handler = ErrorHandler default_handler = DefaultHandler menu = { "button": [{ "name": "我的账户", "sub_button": [{ "type": "click", "name": "我的会议", "key": "my_conf" }, { "type": "view", "name": "联系我们", "url": "http://www.soso.com/" }, { "type": "click", "name": "帮助", "key": "help" }, { "type": "click", "name": "绑定", "key": "bind_account" }] }, { "name": "会议参与", "sub_button": [{ "type": "click", "name": "即将进行的会议", "key": "upcoming_conf" }, { "type": "click", "name": "全部会议", "key": "all_conf" }, { "type": "view", "name": "会议签到", "url": "http://www.soso.com/" }, { "type": "view", "name": "当前会议评论", "url": "http://www.soso.com/" }, { "type": "view", "name": "会议搜索", "url": "http://www.soso.com/" }] }, { "name": "热门活动", "sub_button": [{ "type": "view", "name": "活动一", "url": "http://www.soso.com/" }, { "type": "view", "name": "活动二", "url": "http://www.soso.com/" }, { "type": "view", "name": "活动三", "url": "http://www.soso.com/" }] }] } @classmethod def update_menu(cls, activities=None): """
class CustomWeChatView(WeChatView): lib = WeChatLib(WECHAT_TOKEN, WECHAT_APPID, WECHAT_SECRET) handlers = [ BookEmptyHandler, BindAccountHandler, GetAllConfListHandler, ShowMoreConfHandler, GetComingConfListHandler, SearchConfListHandler, favoriteConfListHandler, confRemindHandler ] error_message_handler = ErrorHandler default_handler = DefaultHandler event_keys = { 'book_what': 'SERVICE_BOOK_WHAT', 'get_ticket': 'SERVICE_GET_TICKET', 'account_bind': 'SERVICE_BIND', 'remind': 'SERVICE_REMIND', 'book_empty': 'BOOKING_EMPTY', 'book_header': 'BOOKING_ACTIVITY_', 'my_meeting': 'MY_MEETING' } menu = { 'button': [ { "name": "服务", "sub_button": [{ "type": "click", "name": "绑定", "key": event_keys['account_bind'], }, { "type": "click", "name": "提醒", "key": event_keys['remind'], }] }, { "name": "会议", "sub_button": [{ "type": "click", "name": "近期会议", "key": event_keys['book_what'], }, { "type": "click", "name": "所有会议", "key": event_keys['get_ticket'], }, { "type": "click", "name": "我的会议", "key": event_keys['my_meeting'], }] }, # { # "name": "f**k", # "sub_button": [] # } ] } @classmethod def get_book_btn(cls): return cls.menu['button'][-1] @classmethod def update_book_button(cls, activities): book_btn = cls.get_book_btn() # if len(activities) == 0: # book_btn['type'] = 'click' # book_btn['key'] = cls.event_keys['book_empty'] # else: # book_btn.pop('type', None) # book_btn.pop('key', None) # book_btn['sub_button'] = list() # for act in activities: # book_btn['sub_button'].append({ # 'type': 'click', # 'name': act['name'], # 'key': cls.event_keys['book_header'] + str(act['id']), # }) @classmethod def update_menu(cls, activities=None): """ :param activities: list of Activity :return: None """ if activities is not None: if len(activities) > 5: cls.logger.warn('Custom menu with %d activities, keep only 5', len(activities)) cls.update_book_button([{ 'id': act.id, 'name': act.name } for act in activities[:5]]) else: current_menu = cls.lib.get_wechat_menu() existed_buttons = list() for btn in current_menu: if btn['name'] == '抢票': existed_buttons += btn.get('sub_button', list()) activity_ids = list() for btn in existed_buttons: if 'key' in btn: activity_id = btn['key'] if activity_id.startswith(cls.event_keys['book_header']): activity_id = activity_id[ len(cls.event_keys['book_header']):] if activity_id and activity_id.isdigit(): activity_ids.append(int(activity_id)) return cls.update_menu( Activity.objects.filter( id__in=activity_ids, status=Activity.STATUS_PUBLISHED, book_end__gt=timezone.now()).order_by('book_end')[:5]) cls.lib.set_wechat_menu(cls.menu)
class CustomWeChatView(WeChatView): lib = WeChatLib(WECHAT_TOKEN, WECHAT_APPID, WECHAT_SECRET) handlers = [ UnbindOrUnsubscribeHandler, BindAccountHandler, CourseListHandler, BulletScreenHandler, DDLCenterHandler, GetBulletScreenHandler, GetNewNoticeHandler ] error_message_handler = ErrorHandler default_handler = DefaultHandler # SITE_DOMAIN = "http://59.66.250.60/" event_keys = { 'get_curriculum_schedule': 'SERVICE_GET_CURRICULUM_SCHEDULE', 'get_ddl': 'SERVICE_GET_DDL', 'get_exam_info': 'SERVICE_GET_EXAM_INFO', 'get_new_trend': 'SERVICE_GET_NEW_TREND', 'account_bind': 'SERVICE_BIND', 'bullet_screen': 'SERVICE_BULLET_SCREEN', 'lecture_bbs': 'SERVICE_LECTURE_BBS', 'get_library': 'SERVICE_GET_LIBRARY', 'search_lecture': 'SERVICE_SEARCH_LECTURE', 'xuetang_achievement': 'SERVICE_XUETANG_ACHIEVEMENT', } menu = { 'button': [{ "name": "我的课程", "sub_button": [{ "type": "click", "name": "课程表", "key": event_keys['get_curriculum_schedule'] }, { "type": "click", "name": "DDL中心", "key": event_keys['get_ddl'] }, { "type": "click", "name": "新动态", "key": event_keys['get_new_trend'], }, { "type": "click", "name": "考试信息", "key": event_keys['get_exam_info'], }, { "type": "click", "name": "账号绑定", "key": event_keys['account_bind'], }] }, { "name": "课程互动", "sub_button": [{ "type": "click", "name": "课聊", "key": event_keys['bullet_screen'], }, { "type": "click", "name": "课程论坛", "key": event_keys['lecture_bbs'], }] }, { "name": "学习助手", "sub_button": [{ "type": "click", "name": "我要自习", "key": event_keys['get_library'], }, { "type": "click", "name": "我要搜课", "key": event_keys['search_lecture'], }, { "type": "click", "name": "学堂成就", "key": event_keys['xuetang_achievement'], }] }] } @classmethod def update_menu(cls): cls.lib.set_wechat_menu(cls.menu)
def check(self): wechat_lib = WeChatLib(WECHAT_TOKEN, WECHAT_APPID, WECHAT_SECRET) menu = wechat_lib.get_wechat_menu()[-1]['sub_button'] event_keys = [activity['key'] for activity in menu] return self.is_text_command("抢票") or self.is_event_click(*event_keys)
class CustomWeChatView(WeChatView): lib = WeChatLib(WECHAT_TOKEN, WECHAT_APPID, WECHAT_SECRET) handlers = [ HelpOrSubscribeHandler, UnbindOrUnsubscribeHandler, AccountBindHandler, ViewPersonalInformationHandler, CourseSearchHandler, CourseListHandler, CommunicateHandler, NoticePanelHandler, LibraryRemainsHandler, MyCalendarHandler, SchoolCalendarHandler, NavigationHandler, RemindHandler ] error_message_handler = ErrorHandler default_handler = DefaultHandler menu = { 'button': [{ 'name': '欢迎', 'sub_button': [{ 'type': 'view', 'name': '帮助', 'url': get_url(event_urls['help']) }, { 'type': 'view', 'name': '绑定学号', 'url': get_redirect_url(event_urls['account_bind']) }] }, { 'name': '爱学习', 'sub_button': [{ 'type': 'view', 'name': '课程搜索', 'url': get_redirect_url(event_urls['search_course']) }, { 'type': 'view', 'name': '个人课程', 'url': get_redirect_url(event_urls['course_list']) }, { 'type': 'view', 'name': '课程讨论', 'url': get_redirect_url(event_urls['communication_list']) }, { 'type': 'view', 'name': '课程评价', 'url': get_redirect_url(event_urls['comment_list']) }, { 'type': 'view', 'name': '通知面板', 'url': get_redirect_url(event_urls['notice_panel']) }] }, { 'name': '乐生活', 'sub_button': [{ 'type': 'click', 'name': '文图查座', 'key': event_keys['library_remains'] }, { 'type': 'view', 'name': '个人日历', 'url': get_redirect_url(event_urls['calendar']) }, { 'type': 'click', 'name': '校历查询', 'key': event_keys['school_calendar'] }, { 'type': 'view', 'name': '校园导航', 'url': get_redirect_url(event_urls['navigation']) }] }] } @classmethod def get_book_btn(cls): return cls.menu['button'][-1] @classmethod def update_menu(cls): return cls.lib.set_wechat_menu(cls.menu)