def group_msg_handle(message): msg_dict = { "msg_type": "group", "number": get_number(message), "msg": "我听不懂" } #检测 general_detect = group_msg_general_detection(message) db_detect = group_msg_db_detection(message) if general_detect != '': msg_dict["msg"] = general_detect elif db_detect != '': msg_dict["msg"] = db_detect else: logging_put("群聊中用户请求没有对应的信息:" + str(get_user_id(message)) + "-" + get_raw_message(message)) msg_dict["msg"] = group_msg_error(message) #没有at,则有四分之一的概率回复消息;at了则一定回复 if at_robot(message): send_msg(msg_dict) else: if 1 == random.randint(1, 12): send_msg(msg_dict) return
def exist_number(table, number): #连接数据库 db = pymysql.connect(host=host, port=port, user=user, password=password, database=database) #创建一个游标 cursor = db.cursor() #sql语句 sql = "select * from " + table + " WHERE num=%s" db.begin() #开启事务 #接收查找结果 res = () try: #执行sql语句 (第二个参数即把占位符解释) cursor.execute(sql, number) res = cursor.fetchall() db.commit() #提交事务 except pymysql.Error as e: logging_put(e) db.rollback() #报错,则回滚 db.close() #将查询结果(元组)返回 if len(res) > 0: return True else: return False
def get_numbers(table): #连接数据库 db = pymysql.connect(host=host, port=port, user=user, password=password, database=database) #创建一个游标 cursor = db.cursor() #sql语句 sql = "select * from " + table db.begin() #开启事务 #接收查找结果 res = None try: #执行sql语句 (第二个参数即把占位符解释) cursor.execute(sql) res = cursor.fetchall() db.commit() #提交事务 except pymysql.Error as e: logging_put(e) db.rollback() #报错,则回滚 db.close() result = [] for item in res: result.append(item[1]) return result
def findInfo(quest): #连接数据库 db = pymysql.connect(host=host, port=port, user=user, password=password, database=database) #创建一个游标 cursor = db.cursor() #sql语句 sql = "select * from words WHERE quest=%s" db.begin() #开启事务 #接收查找结果 res = None try: #执行sql语句 (第二个参数即把占位符解释) cursor.execute(sql, quest) res = cursor.fetchall() db.commit() #提交事务 except pymysql.Error as e: logging_put(e) db.rollback() #报错,则回滚 db.close() #将查询结果(元组)返回 return res
def message_handle(message): logging_put("收到消息" + get_raw_message(message) + "来自" + str(get_number(message))) if get_message_type(message) == 'private': private_msg_handle(message) elif get_message_type(message) == 'group': group_msg_handle(message) return 0
def command_study_handle(message): if len(get_raw_message(message).split(' ')) == 3: new_quest = get_raw_message(message).split(' ')[1] new_reply = get_raw_message(message).split(' ')[2] if study_info(new_quest, new_reply): logging_put("用户" + str(get_user_id(message)) + "教我新内容:" + new_quest + "--" + new_reply) return '学习成功,收获新知识[CQ:face,id=63]' else: return '学习失败' else: return '命令格式有误,发送“#帮助”获得更多信息'
def send_subscribe(msg_type,num,subscribe): logging_put("给账号"+str(num)+"发送"+subscribe+"的订阅") msg_dict={ "msg_type":msg_type, "number":num, "msg":"" } #订阅内容分发 if subscribe=='famousremark': msg_dict["msg"]=ss_content.famousremark_sub() if msg_dict["msg"]=="": msg_dict["msg"]="【每日英语】订阅频道出现问题[CQ:face,id=37]" elif subscribe=="subwallpaper": msg_dict["msg"]=ss_content.wallpaper_sub() if msg_dict["msg"]=="": msg_dict["msg"]="【每日必应壁纸】订阅频道出现问题[CQ:face,id=37],请联系蝙蝠侠" elif subscribe=="submusic": msg_dict["msg"]=ss_content.random_music_sub() if msg_dict["msg"]=="": msg_dict["msg"]="【网易云每日推荐】订阅频道出现问题[CQ:face,id=37],请联系蝙蝠侠" elif subscribe=="subsexpic": msg_dict["msg"]=ss_content.sex_picture_sub() if msg_dict["msg"]=="": msg_dict["msg"]="【每日美女】订阅频道出现问题[CQ:face,id=37],请联系蝙蝠侠" elif subscribe=="subweibohot": msg_dict["msg"]=ss_content.weibo_hot_sub() if msg_dict["msg"]=="": msg_dict["msg"]="【每日微博热搜】订阅频道出现问题[CQ:face,id=37],请联系蝙蝠侠" elif subscribe=="sublifeskill": msg_dict["msg"]=ss_content.life_skill_sub() if msg_dict["msg"]=="": msg_dict["msg"]="【每日生活小窍门】订阅频道出现问题[CQ:face,id=37],请联系蝙蝠侠" elif subscribe=="subkaoyan": msg_dict["msg"]=ss_content.kaoyan_sub() if msg_dict["msg"]=="": msg_dict["msg"]="【考研倒计时】订阅频道出现问题[CQ:face,id=37],请联系蝙蝠侠" elif subscribe=="love_word": msg_dict["msg"]=ss_content.love_word_sub() if msg_dict["msg"]=="": msg_dict["msg"]="【每日情话】订阅频道出现问题[CQ:face,id=37],请联系蝙蝠侠" elif subscribe=="fuck_word": msg_dict["msg"]=ss_content.fuck_word_sub() if msg_dict["msg"]=="": msg_dict["msg"]="【周三骚话】订阅频道出现问题[CQ:face,id=37],请联系蝙蝠侠" send_msg(msg_dict) return
def private_msg_handle(message): msg_dict = { "msg_type": "private", "number": get_number(message), "msg": "我听不懂" } #检测 general_detect = private_msg_general_detection(message) db_detect = private_msg_db_detection(message) if general_detect != '': msg_dict["msg"] = general_detect elif db_detect != '': msg_dict["msg"] = db_detect else: logging_put("私聊中用户请求没有对应的信息:" + str(get_user_id(message)) + "-" + get_raw_message(message)) msg_dict["msg"] = private_msg_error() send_msg(msg_dict) return
def del_number(table, number): #连接数据库 db = pymysql.connect(host=host, port=port, user=user, password=password, database=database) #创建一个游标 cursor = db.cursor() #sql语句 sql = "delete from " + table + " where num=%s" db.begin() #开启事务 try: #执行sql语句 cursor.execute(sql, number) db.commit() #提交事务 except pymysql.Error as e: logging_put(e) db.rollback() #报错,则回滚 return False db.close() return True
def add_number(table, number): #连接数据库 db = pymysql.connect(host=host, port=port, user=user, password=password, database=database) #创建一个游标 cursor = db.cursor() #sql语句 sql = "insert into " + table + "(id,num) VALUES(NULL,%s)" db.begin() #开启事务 try: #执行sql语句 cursor.execute(sql, number) #提交事务 db.commit() except pymysql.Error as e: logging_put(e) db.rollback() #报错,则回滚 return False db.close() return True
def check_pass(message): if pick.get_post_type(message) == "message": if pick.get_message_type(message) == "private": command = pick.get_raw_message(message).split(" ")[0] number = str(pick.get_number(message)) msg_dict = { "msg_type": "private", "number": pick.get_number(message), "msg": "" } #消息来源在黑名单中,则只识别是不是“#开机” if exist_number(number): #当前发消息者面前,我们是关机的 if command == "#开机": logging_put(number + "开启了我,执行者:" + str(pick.get_user_id(message))) #从黑名单中移除该number del_number(number) msg_dict['msg'] = "我们又见面了" send_msg(msg_dict) #这次消息都不予通过 return False else: #当前发消息者面前,我们是开机的 if command == "#关机": logging_put(number + "关闭了我,执行者:" + str(pick.get_user_id(message))) #向黑名单中加人该number add_number(number) msg_dict['msg'] = "使用“#开机”就可以再次开启我" send_msg(msg_dict) return False elif command == "#开机": msg_dict['msg'] = "我现在就是开机状态[CQ:face,id=22]" send_msg(msg_dict) return False else: return True elif pick.get_message_type(message) == "group": if pick.get_raw_message(message).split( ' ')[0] == "[CQ:at,qq=1750133059]": #方便还原message ori = pick.get_raw_message(message) #更新原数据的原始信息 pick.set_raw_message( message, pick.get_raw_message(message).split(' ', 1)[1]) command = pick.get_raw_message(message).split(" ")[0] number = str(pick.get_number(message)) msg_dict = { "msg_type": "group", "number": pick.get_number(message), "msg": "" } #消息来源在黑名单中,则只识别是不是“#开机” if exist_number(number): #当前发消息者面前,我们是关机的 if command == "#开机": logging_put(number + "开启了我,执行者:" + str(pick.get_user_id(message))) #从黑名单中移除该number del_number(number) msg_dict['msg'] = "我们又见面了" send_msg(msg_dict) #这次消息都不予通过 return False else: #当前发消息者面前,我们是开机的 if command == "#关机": logging_put(number + "关闭了我,执行者:" + str(pick.get_user_id(message))) #向黑名单中加人该number add_number(number) msg_dict['msg'] = "使用“#开机”就可以再次开启我" send_msg(msg_dict) return False elif command == "#开机": msg_dict['msg'] = "我现在就是开机状态[CQ:face,id=22]" send_msg(msg_dict) return False else: #还原消息 pick.set_raw_message(message, ori) return True else: if exist_number(number): return False else: return True elif pick.get_post_type(message) == "notice" or pick.get_post_type( message) == "request": try: user_id = message['user_id'] except BaseException as e: #如果user_id都没有,则直接拦截 return False #再尝试得到群号 try: group_id = message['group_id'] except BaseException as e: #有user_id,没有群id if exist_number(message['user_id']): return False else: return True #有群id if exist_number(message['group_id']): return False else: return True else: #三大消息以外应该是心跳消息,通行 return True