def get_exam_cal(self, msg, userId): self.class_database_op.step_back(userId) jwxt = self.class_database_op.get_jwxt_info(userId) if not jwxt: return "您还没有保存您的信息,选3来输入登录信息吧~" itchatmp.send("正在查询,请稍候。。。", msg['FromUserName']) request_utils_ins = request_utils.request_utils() exams = request_utils_ins.jwxt_get_exam_list(userId) print(exams) cal = utils.exams_list_to_cal(exams) file_name = 'cache/' + str(userId) + '.ics' file_show_name = utils.random_string(32) + '.ics' with open(file_name, 'wb') as f: f.write(cal.to_ical()) f.close() file_url = utils.upload_file(file_name, file_show_name) import os os.remove(file_name) itchatmp.send("请接收文件~由于微信限制,机器人不能给您直接发送文件,请将以下链接复制到外部浏览器下载~", msg['FromUserName']) return file_url
def baidu_wenku_download(self, msg, userId): if not self.class_database_op.get_redis_kv(userId, 'inputing'): self.class_database_op.set_redis_kv(userId, 'inputing', 1) return '请输入您要下载的百度文库文档地址:' else: self.class_database_op.delete_redis_kv(userId, 'inputing') current_url = msg['Content'] self.class_database_op.step_back(userId) current_url = current_url.replace('wk.baidu.com', 'wenku.baidu.com') current_url = current_url.replace('www.baidu.com/sf_edu_wenku', 'wenku.baidu.com') current_url = current_url.replace('m.baidu.com/sf_edu_wenku', 'wenku.baidu.com') if current_url.find('https://wenku.baidu.com') != 0 and current_url.find('http://wenku.baidu.com') != 0: return "非百度文库文档地址,无法下载。" itchatmp.send("正在为您下载该文档,请稍候。", msg['FromUserName']) request_utils_ins = request_utils.request_utils() file_info = request_utils_ins.baidu_wenku_download(current_url) file_url = utils.upload_file(file_info[0], file_info[1]) import os os.remove(file_info[0]) itchatmp.send("请接收文件~由于微信限制,机器人不能给您直接发送文件,请将以下链接复制到外部浏览器下载~", msg['FromUserName']) return file_url
def balance_check(self, userId, task, is_retry=False): loop = utils.init_asyncio() try: userName = self.class_database_op.get_username_by_id(userId) card = self.class_database_op.get_card_info(userId) if not card: return request_utils_ins = request_utils.request_utils() balance = request_utils_ins.card_balance_check(userId)['current'] value = float(task.function_option) if value > balance: if not self.class_database_op.get_redis_kv( userId, 'balance-notice-sent'): itchatmp.send("您的校园卡余额不足,目前是 %.2f 元,请及时充值。" % balance, userName) self.class_database_op.set_redis_kv( userId, 'balance-notice-sent', 1) else: self.class_database_op.delete_redis_kv(userId, 'balance-notice-sent') except Exception: if not is_retry: self.thread_pool.submit(self.balance_check, task.user.id, task, True) utils.close_asyncio(loop)
def book_check(self, userId, task, is_retry=False): loop = utils.init_asyncio() try: userName = self.class_database_op.get_username_by_id(userId) lib = self.class_database_op.get_lib_info(userId) if not lib: return request_utils_ins = request_utils.request_utils() books = request_utils_ins.lib_lend_book_list(userId) value = int(task.function_option) for book in books: if book['remain_days'] <= value: itchatmp.send( \ "您的这本书该还了哦~\n" \ "书名: %s:\n" \ "图书条码: %s\n" \ "流通部门: %s\n" \ "流通状态: %s\n" \ "归还日期: %s\n" \ "剩余天数: %d\n" \ % (book['name'], book['barcode'], book['department'], book['state'], book['back_date'], book['remain_days']), userName) except Exception: if not is_retry: self.thread_pool.submit(self.book_check, task.user.id, task, True) utils.close_asyncio(loop)
def daily_consume_notice(self, userId, task, is_retry=False): loop = utils.init_asyncio() try: userName = self.class_database_op.get_username_by_id(userId) card = self.class_database_op.get_card_info(userId) if not card: return today = datetime.date.today().strftime('%Y%m%d') month = datetime.date.today().strftime('%Y%m') request_utils_ins = request_utils.request_utils() ret_list = request_utils_ins.card_get_today_log(userId) consume_amount = request_utils_ins.card_get_total_consume( userId, month + '01', month + '30') today_consume_amount = request_utils_ins.card_get_today_total_consume( userId) bash_count = 0 bash_amount = 0.0 rest_amount = 0.0 buy_count = 0 buy_amount = 0.0 eat_count = 0 eat_amount = 0.0 for item in ret_list: if item['sub_system_name'] == '本部水控': bash_count += 1 bash_amount += abs(item['amount']) elif item['sub_system_name'] == '本部商贸硬网关': buy_count += 1 buy_amount += abs(item['amount']) elif item['sub_system_name'] == '本部北院硬网关' or item[ 'sub_system_name'] == '本部南院硬网关': eat_count += 1 eat_amount += abs(item['amount']) else: rest_amount += abs(item['amount']) itchatmp.send("晚上好,您的今天消费状况如下:\n" \ "共洗了 %d 次澡,花了 %.2f 元。\n" \ "共吃了 %d 次饭,花了 %.2f 元。\n" \ "共买了 %d 次东西,花了 %.2f 元。\n" \ "其他共花了 %.2f 元。\n" \ "本月总共共花了 %.2f 元。\n" \ % (bash_count, bash_amount, \ eat_count, eat_amount, \ buy_count, buy_amount, rest_amount, abs(consume_amount + today_consume_amount)), userName) except Exception: if not is_retry: self.thread_pool.submit(self.daily_consume_notice, task.user.id, task, True) utils.close_asyncio(loop)
def msg_handle_task(self, msg, msg_opt): user_id = self.class_database_op.put_user(msg['FromUserName']) if buu_config.config.verbose: print('%s: %s' % (msg['FromUserName'], msg['Content'])) return_str = self.command_dict_class.step_process( msg, user_id, msg_opt) if return_str: itchatmp.send(return_str, msg['FromUserName'])
def balance_check(self, msg, userId): self.class_database_op.step_back(userId) itchatmp.send("正在查询,请稍候。。。", msg['FromUserName']) card = self.class_database_op.get_card_info(userId) if not card: return "您还没有保存您的信息,选1来输入登录信息吧~" request_utils_ins = request_utils.request_utils() balance = request_utils_ins.card_balance_check(userId) return "当前余额:%.2f 元\n当前过渡余额:%.2f 元\n上次过渡余额:%.2f 元" % ( balance['current'], balance['current_pending'], balance['pre_pending'])
def renew_lend_book(self, msg, userId): lib = self.class_database_op.get_lib_info(userId) if not lib: self.class_database_op.step_back(userId) return "您还没有保存您的信息,选2来输入登录信息吧~" request_utils_ins = request_utils.request_utils() if not self.class_database_op.get_redis_kv(userId, 'inputing'): itchatmp.send("正在查询,请稍候。。。", msg['FromUserName']) books = request_utils_ins.lib_lend_book_list(userId) i = 0 for book in books: i = i + 1 remark = '' if book['remain_days'] < 0: remark = '-已超期,无法续借' itchatmp.send( \ "[%d%s]书名: %s\n" \ "图书条码: %s\n" \ "流通部门: %s\n" \ "流通状态: %s\n" \ "归还日期: %s\n" \ "剩余天数: %d\n" \ % (i, remark, book['name'], book['barcode'], book['department'], book['state'], book['back_date'], book['remain_days']), msg['FromUserName']) itchatmp.send( "输入书名前括号内数字则续借这本书(请注意续借是将还书时间重置为从当前日期起往后十天,并不是在原先的日期上加十天),输入0则返回上一级菜单", msg['FromUserName']) self.class_database_op.set_redis_kv(userId, 'inputing', 1) else: self.class_database_op.delete_redis_kv(userId, 'inputing') if msg['Content'] == '0': self.class_database_op.step_back(userId) return "已经返回上级菜单。" else: if request_utils_ins.lib_lend_book_renew( userId, int(msg['Content'])): itchatmp.send("续借成功,请在十天之后还书哦~", msg['FromUserName']) else: itchatmp.send("续借失败,您的输入有误或者您可能已经续借过这本书了。", msg['FromUserName']) self.class_database_op.step_back(userId)
def card_get_log(self, msg, userId): if not self.class_database_op.get_redis_kv(userId, 'inputing'): self.class_database_op.set_redis_kv(userId, 'inputing', 1) return '请输入您要查询的月份,比如2017年8月则输入 201708:' else: self.class_database_op.delete_redis_kv(userId, 'inputing') month = msg['Content'] self.class_database_op.step_back(userId) itchatmp.send("正在查询,请稍候。。。", msg['FromUserName']) card = self.class_database_op.get_card_info(userId) if not card: return "您还没有保存您的信息,选1来输入登录信息吧~" request_utils_ins = request_utils.request_utils() ret_list = request_utils_ins.card_get_history_log( userId, month + '01', month + '30') consume_amount = request_utils_ins.card_get_total_consume( userId, month + '01', month + '30') bash_count = 0 bash_amount = 0.0 rest_amount = 0.0 buy_count = 0 buy_amount = 0.0 eat_count = 0 eat_amount = 0.0 for item in ret_list: if item['sub_system_name'] == '本部水控': bash_count += 1 bash_amount += abs(item['amount']) elif item['sub_system_name'] == '本部商贸硬网关': buy_count += 1 buy_amount += abs(item['amount']) elif item['sub_system_name'] == '本部北院硬网关' or item[ 'sub_system_name'] == '本部南院硬网关': eat_count += 1 eat_amount += abs(item['amount']) else: rest_amount += abs(item['amount']) return "查询完毕, 您的消费状况如下:\n" \ "共洗了 %d 次澡,花了 %.2f 元。\n" \ "共吃了 %d 次饭,花了 %.2f 元。\n" \ "共买了 %d 次东西,花了 %.2f 元。\n" \ "其他共花了 %.2f 元。\n" \ "总共共花了 %.2f 元。\n" \ % (bash_count, bash_amount, \ eat_count, eat_amount, \ buy_count, buy_amount, rest_amount, abs(consume_amount))
def consume_check(self, userId, task, is_retry=False): loop = utils.init_asyncio() try: userName = self.class_database_op.get_username_by_id(userId) card = self.class_database_op.get_card_info(userId) if not card: return request_utils_ins = request_utils.request_utils() ret_list = request_utils_ins.card_get_today_log(userId) last_order = self.class_database_op.get_redis_kv( userId, 'consume-notice-count') if last_order: last_order = int(last_order) put_order_count = 0 for item in ret_list: if last_order: if item['count'] > last_order: # 开始提醒 intent = '其他' if item['sub_system_name'] == '本部水控': intent = '洗澡' elif item['sub_system_name'] == '本部商贸硬网关': intent = '购物' elif item['sub_system_name'] == '本部北院硬网关' or item[ 'sub_system_name'] == '本部南院硬网关': intent = '吃饭' itchatmp.send("校园卡消费提醒:\n" \ "时间:%s\n" \ "金额:%.2f 元\n" \ "余额:%.2f 元\n" \ "消费用途:%s" \ % (item['time'], abs(item['amount']), item['current_balance'], intent) \ , userName) if put_order_count < item['count']: put_order_count = item['count'] if put_order_count: self.class_database_op.set_redis_kv(userId, 'consume-notice-count', put_order_count) except Exception: if not is_retry: self.thread_pool.submit(self.consume_check, task.user.id, task, True) utils.close_asyncio(loop)
def construct_reply(reply, user_id, active=False): content = reply['content'] if reply['type'] == 'article': print('Reply article') title = content['title'] description = content['description'] picture_url = content['cover-url'] article_url = content['content-url'] article = { 'Title': title, 'Description': description, 'PicUrl': picture_url, 'Url': article_url } if active: article = { 'title': title, 'description': description, 'picurl': picture_url, 'url': article_url } return {'MsgType': itchatmp.content.NEWS, 'Articles': [article]} elif reply['type'] == 'image': print('Reply image') pic_tag = content['tag'] pic_url = content['url'] logger.error("content is " + str(content)) if ('payment' in content) and (content['payment'] == 'wx'): itchatmp.send("请您使用微信扫描下面二维码支付,谢谢", user_id) return generate_qr_code_url(user_id) image = fetch_image(pic_tag, pic_url) print('image = ' + image) if not image: raise Exception('fetch image from {} failed'.format(pic_url)) return {'MsgType': itchatmp.content.IMAGE, 'FileDir': image} elif reply['type'] == 'qrcode': print('Reply qrcode') orderId = content['orderId'] return generate_qr_code_url(orderId) elif reply['type'] == 'custom-service': print('Reply custom-service') return generate_reply_by_custom_service(user_id) print('Reply text') return content
def export_to_ical(self, msg, userId): self.class_database_op.step_back(userId) itchatmp.send("正在查询,请稍候。。。", msg['FromUserName']) card = self.class_database_op.get_jwxt_info(userId) if not card: return "您还没有保存您的信息,选 3 来输入登录信息吧~" request_utils_ins = request_utils.request_utils() results = request_utils_ins.jwxt_total_login(userId) if not results[0]: return "登录教务系统失败!" lessons = request_utils_ins.jwxt_get_lesson_table(userId) lessons_list = [] for key in lessons: lesson = lessons[key] if lesson['time'] != '': time_list = lesson['time'].split(';') room_list = lesson['room'].split(';') for i in range(0, len(time_list)): ret_dict = {} ret_dict['name'] = lesson['name'] ret_dict['teacher'] = lesson['teacher_name'] key_list = re.compile(r"(.*){(.*)}", flags=re.M).findall(time_list[i]) ret_dict['time'] = key_list[0][0] ret_dict['week'] = key_list[0][1] ret_dict['room'] = room_list[i] lessons_list.append(ret_dict) cal = utils.lessons_list_to_cal(lessons_list) file_name = 'cache/' + str(userId) + '.ics' file_show_name = utils.random_string(32) + '.ics' with open(file_name, 'wb') as f: f.write(cal.to_ical()) f.close() file_url = utils.upload_file(file_name, file_show_name) import os os.remove(file_name) itchatmp.send("请接收文件~由于微信限制,机器人不能给您直接发送文件,请将以下链接复制到外部浏览器下载~", msg['FromUserName']) return file_url
def card_get_today_log(self, msg, userId): self.class_database_op.step_back(userId) itchatmp.send("正在查询,请稍候。。。", msg['FromUserName']) card = self.class_database_op.get_card_info(userId) if not card: return "您还没有保存您的信息,选1来输入登录信息吧~" request_utils_ins = request_utils.request_utils() ret_list = request_utils_ins.card_get_today_log(userId) consume_amount = request_utils_ins.card_get_today_total_consume(userId) bash_count = 0 bash_amount = 0.0 rest_amount = 0.0 buy_count = 0 buy_amount = 0.0 eat_count = 0 eat_amount = 0.0 for item in ret_list: if item['sub_system_name'] == '本部水控': bash_count += 1 bash_amount += abs(item['amount']) elif item['sub_system_name'] == '本部商贸硬网关': buy_count += 1 buy_amount += abs(item['amount']) elif item['sub_system_name'] == '本部北院硬网关' or item[ 'sub_system_name'] == '本部南院硬网关': eat_count += 1 eat_amount += abs(item['amount']) else: rest_amount += abs(item['amount']) return "查询完毕, 您的今天消费状况如下:\n" \ "共洗了 %d 次澡,花了 %.2f 元。\n" \ "共吃了 %d 次饭,花了 %.2f 元。\n" \ "共买了 %d 次东西,花了 %.2f 元。\n" \ "其他共花了 %.2f 元。\n" \ "总共共花了 %.2f 元。\n" \ % (bash_count, bash_amount, \ eat_count, eat_amount, \ buy_count, buy_amount, rest_amount, abs(consume_amount))
def keep_alive(self, is_retry = False): loop = utils.init_asyncio() try: request_utils_ins = request_utils.request_utils() try: request_utils_ins.www_keepalive() except Exception: import traceback traceback.print_exc() userName = self.class_database_op.get_username_by_id(self.config.admin_id) itchatmp.send("网络掉线,请检查~", userName) except Exception: if not is_retry: self.thread_pool.submit(self.keep_alive, True) utils.close_asyncio(loop)
def book_renew(self, userId, task, is_retry=False): utils.init_asyncio() try: userName = self.class_database_op.get_username_by_id(userId) lib = self.class_database_op.get_lib_info(userId) if not lib: return request_utils_ins = request_utils.request_utils() books = request_utils_ins.lib_lend_book_list(userId) i = 0 for book in books: i = i + 1 if book['remain_days'] <= 1: if request_utils_ins.lib_lend_book_renew(userId, i): itchatmp.send("续借成功,请在十天之后还书哦~", userName) else: itchatmp.send("续借失败,您的输入有误或者您可能已经续借过这本书了。", userName) itchatmp.send( \ "书名: %s:\n" \ "图书条码: %s\n" \ "流通部门: %s\n" \ "流通状态: %s\n" \ "归还日期: %s\n" \ "剩余天数: %d\n" \ % (book['name'], book['barcode'], book['department'], book['state'], book['back_date'], book['remain_days']), userName) except Exception: if not is_retry: self.thread_pool.submit(self.book_renew, task.user.id, task, True) utils.close_asyncio(loop)
def test(self, msg, userId): self.class_database_op.step_back(userId) itchatmp.send("正在查询,请稍候。。。", msg['FromUserName']) card = self.class_database_op.get_card_info(userId) if not card: return "您还没有保存您的信息,请到校园卡菜单来输入登录信息吧~" request_utils_ins = request_utils.request_utils() results = request_utils_ins.jwxt_total_login(userId) if not results[0]: return "登录教务系统失败!" lessons = request_utils_ins.jwxt_get_lesson_table(userId) for key in lessons: lesson = lessons[key] if lesson['time'] != '': itchatmp.send( \ "课程 %s:\n" \ "教师: %s\n" \ "教室: %s\n" \ "上课时间: %s" \ % (lesson['name'], lesson['teacher_name'], lesson['room'], lesson['time']), msg['FromUserName'])
def auto_rank_lesson(self, msg, userId): self.class_database_op.step_back(userId) jwxt = self.class_database_op.get_jwxt_info(userId) if not jwxt: return "您还没有保存您的信息,选 3 来输入登录信息吧~" itchatmp.send("正在操作,请稍候。。。", msg['FromUserName']) request_utils_ins = request_utils.request_utils() lessons = request_utils_ins.jwxt_get_rank_list(userId) for lesson in lessons: itchatmp.send("正在评价" + lesson['lesson_name'], msg['FromUserName']) request_utils_ins.jwxt_rank_lesson(userId, lesson['lesson_url']) if len(lessons) > 1: request_utils_ins.jwxt_rank_lessons_submit(userId, lessons[-1]['lesson_url']) itchatmp.send("评价完毕!", msg['FromUserName'])
def get_exam_list(self, msg, userId): self.class_database_op.step_back(userId) jwxt = self.class_database_op.get_jwxt_info(userId) if not jwxt: return "您还没有保存您的信息,选 3 来输入登录信息吧~" itchatmp.send("正在查询,请稍候。。。", msg['FromUserName']) request_utils_ins = request_utils.request_utils() exams = request_utils_ins.jwxt_get_exam_list(userId) for exam in exams: itchatmp.send( \ "考试号: %s\n" \ "考试名: %s\n" \ "时间: %s\n" \ "考场: %s\n" \ "座位号: %s\n" \ "校区: %s\n" \ % (exam['exam_no'], exam['exam_name'], exam['datetime'], exam['exam_room'], exam['exam_sit_no'], exam['room_area']), msg['FromUserName']) itchatmp.send("考试信息输出完毕!", msg['FromUserName'])
def get_lend_book_info(self, msg, userId): self.class_database_op.step_back(userId) lib = self.class_database_op.get_lib_info(userId) if not lib: return "您还没有保存您的信息,选2来输入登录信息吧~" itchatmp.send("正在查询,请稍候。。。", msg['FromUserName']) request_utils_ins = request_utils.request_utils() books = request_utils_ins.lib_lend_book_list(userId) for book in books: itchatmp.send( \ "书名: %s\n" \ "图书条码: %s\n" \ "流通部门: %s\n" \ "流通状态: %s\n" \ "归还日期: %s\n" \ "剩余天数: %d\n" \ % (book['name'], book['barcode'], book['department'], book['state'], book['back_date'], book['remain_days']), msg['FromUserName']) itchatmp.send("借阅信息输出完毕!", msg['FromUserName'])
def test(self, msg, userId): word = None if not self.class_database_op.get_redis_kv(userId, 'operating'): if not self.class_database_op.get_redis_kv(userId, 'inputing'): self.class_database_op.set_redis_kv(userId, 'inputing', 1) return '请输入您要搜索的关键字~' else: self.class_database_op.delete_redis_kv(userId, 'inputing') word = msg['Content'] self.class_database_op.set_redis_kv(userId, 'word', word) self.class_database_op.set_redis_kv(userId, 'operating', 1) if msg['Content'] == '0' and not word: return self.back(msg, userId) elif msg['Content'] == '1' and not word: itchatmp.send("正在查询,请稍候。。。", msg['FromUserName']) total_page = int( self.class_database_op.get_redis_kv(userId, 'total_page')) current_page = int( self.class_database_op.get_redis_kv(userId, 'current_page')) + 1 word = self.class_database_op.get_redis_kv(userId, 'word').decode('utf-8') if total_page + 1 > current_page: self.class_database_op.set_redis_kv(userId, 'current_page', current_page) else: #翻页到头 return self.back(msg, userId) else: if not self.class_database_op.get_redis_kv(userId, 'current_page'): itchatmp.send("正在查询,请稍候。。。", msg['FromUserName']) self.class_database_op.set_redis_kv(userId, 'current_page', 1) current_page = 1 else: #异常输入 return self.back(msg, userId) request_utils_ins = request_utils.request_utils() books = request_utils_ins.lib_search_books(word, current_page) total_page = books[1] if not self.class_database_op.get_redis_kv(userId, 'total_page'): if total_page == 0: return self.back(msg, userId) self.class_database_op.set_redis_kv(userId, 'total_page', total_page) books = books[0] i = 0 while len(books) < 5 and total_page >= current_page + i: i += 1 books += request_utils_ins.lib_search_books( word, current_page + i)[0] if i > 0: current_page += i self.class_database_op.set_redis_kv(userId, 'current_page', current_page) for book in books: if book['publisher'] != '' and book['author'] != '': itchatmp.send( \ "书名:%s\n" \ "作者:%s\n" \ "出版社:%s\n" \ "ISBN:%s\n" \ "出版年份:%s\n" \ "索书号:%s\n" \ "可借/总数:%d/%d" \ % \ ( \ book['name'], \ book['author'], \ book['publisher'], \ book['isbn'], \ book['publish_year'], \ book['search_book_no'], \ book['av_number'], book['have_number'], \ ) \ , msg['FromUserName']) if current_page == total_page: itchatmp.send( "页面到头,已返回上级菜单。\n当前第 %d 页,总共 %d 页。" % (current_page, total_page), msg['FromUserName']) itchatmp.send(self.back(msg, userId), msg['FromUserName']) else: itchatmp.send( "输入0退回到上级菜单,输入1翻页。\n当前第 %d 页,总共 %d 页。" % (current_page, total_page), msg['FromUserName'])
def generate_reply_by_custom_service(user_id): if (has_online_customer()): itchatmp.send("正在帮您接通人工客服,请稍等...", user_id) return {'MsgType': itchatmp.content.TRANSFER} return "抱歉,当前客服座席忙,稍后会主动联系您..."
'output': 'json', 'pois': '1', 'ak': config['baidu']['ak'] } try: r = session.get('http://api.map.baidu.com/geocoder/v2/', params=params) if r.ok: print(r.text) data = json.loads(r.text) if data['status'] == 0: # success # 将位置保存到数据库 city_name = data['result']['addressComponent']['city'][:-1] city = db.query(models.City).filter_by(name=city_name).first() if city: user.city_id = city.id return f'成功将您的城市更新为{city_name}' return '不支持的城市' else: # todo save into log return '解析地理位置失败' except: # todo save into log return ' 链接 GPS 解析失败' if __name__ == '__main__': # models.Base.metadata.create_all(models.engine) itchatmp.run(port=8000) print(itchatmp.send('123', config['wechat']['admin']))
def text_reply(msg): itchatmp.send("hello") return msg['Content']
def step_process(self, msg, userId, msg_opt): loop = utils.init_asyncio() try: is_back = False if msg['Content'] in ['#返回']: self.class_database_op.step_back(userId) is_back = True cur_step = self.class_database_op.get_user_current_step(userId) is_head = False if not cur_step or cur_step == '0' and not is_back: cur_step = str(msg['Content']) next_step = int(msg['Content']) is_head = True steps = cur_step.split('-') cur_step_action = self.command_dict for step in steps: next_step = int(step) cur_step_action = (cur_step_action[0])[int(step)] if type(cur_step_action[0]) is not type(self.step_process): if not is_head and not is_back: next_step = int(msg['Content']) if next_step == 0: self.class_database_op.step_back(userId) return self.get_current_step_tips(userId) if cur_step == '0': cur_step = str(next_step) else: cur_step = cur_step + "-" + str(next_step) cur_step_action = (cur_step_action[0])[next_step] self.class_database_op.set_user_current_step(userId, cur_step) if type(cur_step_action[0]) == type([]): # 菜单处理 self.class_database_op.delete_redis_kv(userId, 'inputing') return cur_step_action[1](msg, userId) else: #动作处理 if msg_opt: return cur_step_action[0](msg, userId, msg_opt) else: if not cur_step_action[0]: self.class_database_op.step_back(userId) return self.get_current_step_tips(userId) return cur_step_action[0](msg, userId) except (ValueError, IndexError): import traceback traceback.print_exc() self.class_database_op.step_back(userId) return self.get_current_step_tips(userId) except Exception: import traceback traceback.print_exc() self.class_database_op.set_user_current_step(userId, '0') for single_key in self.config.fail_clean: self.class_database_op.delete_redis_kv(userId, single_key) r = itchatmp.send( "似乎。。。出现了一个错误。。。\n这一般是因为:\n[1]您的输入有误\n[2]系统在处理时发生了一个错误\n[3]您在系统中输入的校园卡信息不正确,mybuu 账号无法登陆。\n\n综上,已返回到首级菜单。", msg['FromUserName']) return self.step_0_tips(msg, userId) utils.close_asyncio(loop)
def print_job_add(self, msg, userId, msg_opt=None): printer_id = self.class_database_op.get_redis_kv( userId, 'current_printer_id') if not printer_id: if not self.class_database_op.get_redis_kv(userId, 'inputing'): self.class_database_op.set_redis_kv(userId, 'inputing', 1) return_str = '以下是您可访问的打印机。\nID 名称\n' results = self.class_database_op.get_all_printers(userId) for printer in results: return_str += str( printer.id) + " " + printer.printer_name + "\n" return_str += "输入0返回上级,输入ID则使用该台打印机。" return return_str else: self.class_database_op.delete_redis_kv(userId, 'inputing') if msg['Content'] == '0': self.class_database_op.step_back(userId) return "已经返回上级菜单。" else: printer_id = int(msg['Content']) if not self.class_database_op.get_printer(printer_id): self.class_database_op.step_back(userId) return "打印机不存在,已经返回上级菜单。" self.class_database_op.set_redis_kv( userId, 'current_printer_id', printer_id) return "请输入打印份数~" amount = self.class_database_op.get_redis_kv(userId, 'current_print_amount') if not amount: self.class_database_op.delete_redis_kv(userId, 'inputing') if msg['Content'] == '0': self.class_database_op.step_back(userId) return "已经返回上级菜单。" else: amount = int(msg['Content']) self.class_database_op.set_redis_kv(userId, 'current_print_amount', amount) return "请将您要打印的文件发给我,目前支持 .doc, .docx, .pdf \n推荐 pdf(务必解密) ,doc 和 docx 可能出现页数统计错误等情况。" amount = int(amount) print_file_name = self.class_database_op.get_redis_kv( userId, 'current_print_file_name') print_page_count = self.class_database_op.get_redis_kv( userId, 'current_print_file_page_count') print_price = self.class_database_op.get_redis_kv( userId, 'current_print_file_print_price') create_time = self.class_database_op.get_redis_kv( userId, 'current_print_create_time') printer = self.class_database_op.get_printer(int(printer_id)) if not print_file_name or not print_page_count or not print_price or not create_time: if not msg_opt: self.class_database_op.step_back(userId) return '操作错误,请发送文件!已经取消当前操作。' if 'file_name' not in msg_opt: self.class_database_op.step_back(userId) return '操作错误,请发送文件!已经取消当前操作。' itchatmp.send("已收到文件,正在处理...", msg['FromUserName']) results = utils.doc_get_page_num( "cache/files/" + msg_opt['file_name'], msg_opt['file_ext']) print_price = round(self.config.print_price * results[0] * amount, 2) itchatmp.send( "每份页数:%d,共打印 %d 份,以下是打印预览。继续打印请转账 %.2f 元,不打印请回复0。" % (results[0], amount, print_price), msg['FromUserName']) itchatmp.send_file(results[1], msg['FromUserName']) self.class_database_op.set_redis_kv(userId, 'current_print_file_name', results[1]) self.class_database_op.set_redis_kv( userId, 'current_print_file_page_count', results[0]) self.class_database_op.set_redis_kv( userId, 'current_print_file_print_price', print_price) self.class_database_op.set_redis_kv(userId, 'current_print_create_time', int(time.time())) return print_price = float(print_price) create_time = int(create_time) if msg['Content'] == '0': self.class_database_op.step_back(userId) return "已经取消。" else: if 'money' not in msg_opt: self.class_database_op.step_back(userId) return "请转账。已经取消当前操作。" if create_time >= msg_opt['create_time']: self.class_database_op.step_back(userId) return "检测到交易重放,已返回上级菜单,请勿重发转账消息。" itchatmp.send( "收到转账:%.2f元。时间戳:%d" % (msg_opt['money'], msg_opt['create_time']), msg['FromUserName']) print_price = print_price - float(msg_opt['money']) create_time = int(time.time()) self.class_database_op.set_redis_kv(userId, 'current_print_file_print_price', print_price) self.class_database_op.set_redis_kv(userId, 'current_print_create_time', int(time.time())) if print_price > 0: return "金额不足,请继续转账 %.2f 元。" % (print_price) self.class_database_op.step_back(userId) request_utils_ins = request_utils.request_utils() for i in range(0, amount): request_utils_ins.send_email(print_file_name, printer) print_file_name = print_file_name.decode('utf-8') user = self.class_database_op.get_user_by_id(userId) self.class_database_op.add_print_job(print_page_count, print_file_name, user, printer) self.class_database_op.delete_redis_prefix_kv(userId, 'current_print') return "任务创建成功,请您到打印机那取打印好的资料。"
def active_reply(replies, user_id): for reply in replies: try: itchatmp.send(construct_reply(reply, user_id, True), user_id) except Exception as e: print('Exception occured when active send!')
openId_list = user_list['data']['openid'] for openId in openId_list: # loop over users content = '点击蓝色链接可打开事件详细列表\n\n' tagId_list = itchatmp.users.get_tags_of_user(openId)['tagid_list'] has_subscription = False # construct message content, which contains hyperlinks to event list for tagId in tagId_list: # loop over user's tag ids if tagId in tag_dict_subscription: # put tag id if it's subscription has_subscription = True event_name_chinese = tag_dict_subscription[tagId] event_name = EVENT_NAME_C2E.get(event_name_chinese) # prepare content html_path = 'file/event/{}/{}.html' \ .format(today_str, event_name) url = "http://139.224.234.82:8653/event/acquire?file={}" \ .format(html_path) # hyper_link = "<a href=\"{}\">{}</a>"\ # .format(url, event_name_chinese) link = "{}事件链接:\n{}"\ .format(event_name_chinese, url) content += '{}\n\n'.format(link) if has_subscription: itchatmp.send(content, openId) # send to user else: if datetime.datetime.today().weekday == 0: # 0 means Monday itchatmp.send('您还没有订阅事件,请点击\"订阅事件\"按钮查看详情', openId)
def send_leo(message): r = itchatmp.send(message, leoId) print(r)