def _init(): global bot global ice print("小冰AI初始化......") # 设置一个保存登陆账户文件的文件 loacation = get_resourceLocation()+"Manager/TalkWidget/reply/wxpy.pkl" # 初始化机器人,扫码登陆 bot = Bot(cache_path=loacation) ice = bot.mps(update=False).search("小冰") # 在公众号里找到小冰 if(len(ice) == 1): ice = ice[0] else: global ice_reply ice_reply ="尚未关注小冰" # 注册事件,当接收到来自小冰的信息时,执行,将信息保存 @bot.register(chats=ice) def print_group_msg(msg): global ice_reply if(ice_reply is None): ice_reply = msg if(isinstance(ice_reply, str)): ice_reply = ice_reply + msg
class ChatManage: def __init__(self): # 登录缓存 self.bot = Bot(cache_path=True) def send_msg_self(self): # 给机器人自己发送消息 self.bot.self.send('Hello World!') # 给文件传输助手发送消息 self.bot.file_helper.send('Hello World!') def get_friends(self, name=None): """ 获取通讯录对象 :param name: 昵称 :return: 好友对象列表 :rtype: list """ if not name: return self.bot.friends() return self.bot.friends().search(name) @print_def def get_friend_info(self, friends=None): """ 获取朋友详细信息 :param friends: :return: [[姓名,昵称,性别,个性签名,城市]] """ if not friends: friends = self.get_friends() friends_info = [] for friend in friends: data = [ friend.name, # friend.nick_name, # friend.sex, friend.signature, # friend.city, ] friends_info.append(data) return friends_info @print_def def get_friend_nums(self): """返回好友总数""" return len(self.get_friends()) def get_groups(self, name=None): """获取所有群""" if not name: return self.bot.groups() return self.bot.groups.search(name) @print_def def get_group_info(self, groups=None): """ 获取群详细信息 :param groups: :return: """ if not groups: groups = self.get_groups() groups_info = [] for group in groups: data = [ group.members, group.name, group.owner.nick_name, [people.name for people in group.members], ] groups_info.append(data) return groups_info def get_mps(self, name=None): """获取公众号对象""" if not name: return self.bot.mps() return self.bot.mps().search(name) # @print_def def get_mps_info(self, mps_list=None): """ 获取公众号基本信息 :param mps_list: :return: [[名称,昵称,地点,签名]] """ if not mps_list: mps_list = self.get_mps() mps_info = [] for mps in mps_list: data = [ mps.name, mps.nick_name, mps.raw['Province'] + '-' + mps.raw['City'], mps.signature, ] mps_info.append(data) return mps_info def get_mps_nums(self): """获取公众号数量""" return len(self.get_mps()) def send_div(self, friend, msg, sign='text'): """ 发送消息体 :param friend: :param msg: :param sign: :return: """ if sign == 'text': friend.send(msg) elif sign == 'img': # 发送图片 path 'my_picture.png' friend.send_image(msg) elif sign == 'video': # 发送视频 path 'my_video.mov' friend.send_video() elif sign == 'file': # 发送文件 path 'my_file.zip' friend.send_file() else: # 以动态的方式发送图片 '@img@my_picture.png' friend.send(msg) def get_now_msg(self): """ 获取实时消息 :return: """ @self.bot.register() def print_others(msg): print(msg) def reply_msg(self, friend): """ 自动回复 :param friend: 好友对象 :return: """ # 回复 friend 的消息 (优先匹配后注册的函数!) @self.bot.register(friend) def reply_my_friend(msg): return 'received: {} ({})'.format(msg.text, msg.type) # 自动接受新的好友请求 def auto_accept(self): @self.bot.register(msg_types=FRIENDS) def auto_accept_friends(msg): # 接受好友请求 new_friend = msg.card.accept() # 向新的好友发送消息 new_friend.send('哈哈,我接受了你的好友请求')
return 'received: {} ({})'.format(msg.text, msg.type) # 打印出所有群聊中@自己的文本消息,并自动回复相同内容 # 这条注册消息是我们构建群聊机器人的基础 @bot.register(Group, TEXT) def print_group_msg(msg): if msg.is_at: print(msg) msg.reply(msg.text) # 获取所有类型的消息(好友消息、群聊、公众号,不包括任何自己发送的消息) # 并将获得的消息打印到控制台 @bot.register() def print_others(msg): print(msg) # 通过机器人对象 Bot 的 chats(), friends(),groups(), mps() 方法, 可分别获取到当前机器人的 所有聊天对象、好友、群聊,以及公众号列表。 bot.chats() bot.friends() bot.groups() bot.mps() # 堵塞线程 进入Python 命令行 让程序保持运行 embed() # 或者仅仅堵塞线程 # bot.join()
import sys import logging.handlers from wxpy import Bot # 防沉迷程序列表 DISALLOWED_PROCESS_LIST = [ 'chrome.exe', 'YoudaoDict.exe', 'IDMan.exe', 'pythonw.exe' ] # ----------------------windows配置------------------- DELAY_TIME = 120 # 休眠、关机、重启延迟时间 # ---------------------微信模块配置------------------- BOT = Bot(True) MASTER = BOT.file_helper # 自己在微信机器人里面的备注 SMALL_ICE = BOT.mps().search('小冰') MINE = 'Your Majesty' # 通知时对自己的称呼 CONTROLLED = '小老弟' # 通知时对被控者的称呼 MSG_LIST = { 0: '{}, 我已经成功上线╰(●’◡’●)╮'.format(MINE), 1: '测试成功,通讯正常,可以执行命令╭(●`∀´●)╯', 2: '检测到您的{}正在运行以下被禁止的程序(..•˘_˘•..)'.format(CONTROLLED), 3: '没有被禁止的程序在运行,您的{}表现不错๑乛◡乛๑'.format(CONTROLLED), 4: '休眠命令已执行(o-ωq)).oO 困', 5: '关机命令已执行,延迟时间:{}s (_ _)( - . - )(~O~) ……( - . - )'.format(DELAY_TIME), 6: '重启命令已执行,延迟时间:{}s (つω`)~'.format(DELAY_TIME), 7: '取消关机命令已执行,来不来得及我就说不来了 (o≖◡≖)', 100: '目前还不支持此消息类型,请输入文字o( ̄┰ ̄*)ゞ', 101: '输入的命令不存在,无法解析,请输入111获取有效命令(* ̄rǒ ̄)', }
from plugins.GetBiliPic import GetBiliPic bot = Bot(cache_path=True, console_qr=2) allFri = bot.friends() help_msg = """/help 查看帮助信息 /today [城市] 获取今日天气 /random 随机一句 /dui [上联] 对对联 /resou 查看微博热搜 /getbli [B站URL] 获取B站封面 /py [短消息] 查找短消息是否是谣言""" mps = [] for mp in config['mps']: mps.append(bot.mps().search(mp)[0]) # Some global switches for debugging use only isDebug = config['debug'] AutoLaunch(bot=bot, filehelper=bot.file_helper).launchTask() plugins = [ GlobalTextHook({'^/help$': help_msg}), ToolsManager(), SearchGarbage(), GetSentece(), DuiDuilian(), TencentPY(), GetBiliPic() ]