async def on_message(self, msg: Message): """ listen for message event """ from_contact = msg.talker() text = msg.text() room = msg.room() if text == 'gif': conversation: Union[ Room, Contact] = from_contact if room is None else room await conversation.ready() await conversation.say('dong') file_box = FileBox.from_file('./bot.gif') await conversation.say(file_box)
async def on_message(self, msg: Message): """ listen for message event """ from_contact = msg.talker() text = msg.text() type = msg.type() room = msg.room() # 不处理群消息 if room is None: # 识别主人 if text == '你好我的机器人' and self.host_contact is None: # 记录主人的contact self.host_contact = from_contact # 列举所有好友的contact friend_contacts = await self.Contact.find_all() # 列举所有群 self.rooms = await self.Room.find_all() # 过滤一些contact self.friend_contacts = [ contact for contact in friend_contacts if len(contact.contact_id) > 50 ] # 初始化好友权限 self.friend_allow = { contact: [False, None] for contact in self.friend_contacts } # 初始化群聊权限 self.room_allow = {room: [False, None] for room in self.rooms} # 对主人开启权限 self.friend_allow[self.host_contact] = [True, None] # 给主人发消息 conversation = self.host_contact await conversation.ready() await conversation.say( '你好亲爱的主人,机器人目前的功能有:\n1 将图片转换为动漫风格\n2 对好友开启机器人\n3 对好友关闭机器人\n4 对群聊开启机器人\n5 对群聊关闭机器人\n主人回复相应数字即可开启对应功能' ) # 如果是主人的消息 if from_contact == self.host_contact: conversation = self.host_contact await conversation.ready() if self.friend_allow[self.host_contact][1] == 2: # 关闭功能 self.friend_allow[self.host_contact][1] = None # 获取好友备注或昵称 friend_name = text # 记录是否找到好友 is_find = False # 查找好友 for contact in self.friend_contacts: if friend_name == contact.payload.alias or friend_name == contact.name: # 找到 is_find = True # 对好友开启权限 self.friend_allow[contact] = [True, None] # 给好友发消息 conversation_friend = contact await conversation_friend.ready() await conversation_friend.say( '这是自动回复:你好,我是机器人,我的主人是Lovely-Pig,主人对你开启了机器人的功能' ) await conversation_friend.say( '这是自动回复:机器人目前的功能有:\n1 将图片转换为动漫风格\n回复相应数字即可开启对应功能' ) break # 给主人反馈 if is_find: await conversation.say(f'亲爱的主人,已对{friend_name}开启机器人功能') if not is_find: await conversation.say( f'亲爱的主人,{friend_name}不在您的好友列表里,“对好友开启机器人”功能已关闭') if self.friend_allow[self.host_contact][1] == 3: # 关闭功能 self.friend_allow[self.host_contact][1] = None # 获取好友备注或昵称 friend_name = text # 记录是否找到好友 is_find = False # 查找好友 for contact in self.friend_contacts: if friend_name == contact.payload.alias or friend_name == contact.name: # 找到 is_find = True # 对好友关闭权限 self.friend_allow[contact] = [False, None] # 给好友发消息 conversation_friend = contact await conversation_friend.ready() await conversation_friend.say( '这是自动回复:你好,我是机器人,我的主人是Lovely-Pig,主人对你关闭了机器人的功能' ) break # 给主人反馈 if is_find: await conversation.say(f'亲爱的主人,已对{friend_name}关闭机器人功能') if not is_find: await conversation.say( f'亲爱的主人,{friend_name}不在您的好友列表里,“对好友关闭机器人”功能已关闭') if self.friend_allow[self.host_contact][1] == 4: # 关闭功能 self.friend_allow[self.host_contact][1] = None # 获取群聊名称 room_name = text # 记录是否找到群聊 is_find = False # 查找群聊 for room in self.rooms: if room_name == await room.topic(): # 找到 is_find = True # 对群聊开启权限 self.room_allow[room] = [True, None] # 给群聊发消息 conversation_room = room await conversation_room.ready() await conversation_room.say( '这是自动回复:你们好,我是机器人,我的主人是Lovely-Pig,主人对群开启了机器人的功能' ) await conversation_room.say( '这是自动回复:机器人目前的功能有:\n1 将图片转换为动漫风格\n回复相应数字即可开启对应功能' ) break # 给主人反馈 if is_find: await conversation.say(f'亲爱的主人,已对{room_name}开启机器人功能') if not is_find: await conversation.say( f'亲爱的主人,{room_name}不在您的群聊列表里,“对群聊开启机器人”功能已关闭') if self.friend_allow[self.host_contact][1] == 5: # 关闭功能 self.friend_allow[self.host_contact][1] = None # 获取群聊名称 room_name = text # 记录是否找到群聊 is_find = False # 查找群聊 for room in self.rooms: if room_name == await room.topic(): # 找到 is_find = True # 对群聊开启权限 self.room_allow[room] = [False, None] # 给群聊发消息 conversation_room = room await conversation_room.ready() await conversation_room.say( '这是自动回复:你们好,我是机器人,我的主人是Lovely-Pig,主人对群关闭了机器人的功能' ) break # 给主人反馈 if is_find: await conversation.say(f'亲爱的主人,已对{room_name}关闭机器人功能') if not is_find: await conversation.say( f'亲爱的主人,{room_name}不在您的群聊列表里,“对群聊关闭机器人”功能已关闭') if text == '1': self.friend_allow[self.host_contact][1] = 1 await conversation.say('亲爱的主人,您已开启“将图片转换为动漫风格”功能,请发给我一张图片') if text == '2': self.friend_allow[self.host_contact][1] = 2 await conversation.say('亲爱的主人,您已开启“对好友开启机器人”功能,请指明是哪一个好友') if text == '3': self.friend_allow[self.host_contact][1] = 3 await conversation.say('亲爱的主人,您已开启“对好友关闭机器人”功能,请指明是哪一个好友') if text == '4': self.friend_allow[self.host_contact][1] = 4 await conversation.say('亲爱的主人,您已开启“对群聊开启机器人”功能,请指明是哪一个群') if text == '5': self.friend_allow[self.host_contact][1] = 5 await conversation.say('亲爱的主人,您已开启“对群聊关闭机器人”功能,请指明是哪一个群') # 好友的消息 if from_contact in self.friend_contacts and from_contact != self.host_contact: # 如果好友有权限 if self.friend_allow[from_contact][0]: conversation = from_contact await conversation.ready() if text == '1': self.friend_allow[from_contact][1] = 1 await conversation.say( '这是自动回复:你已开启“将图片转换为动漫风格”功能,请发给我一张图片') # 如果消息类型是图片 if type == Message.Type.MESSAGE_TYPE_IMAGE: # 如果有权限 if from_contact in self.friend_contacts and self.friend_allow[ from_contact][0]: # 判断功能 if self.friend_allow[from_contact][1] == 1: # 关闭功能 self.friend_allow[from_contact][1] = None conversation = from_contact await conversation.ready() await conversation.say('这是自动回复:正在飞速处理中...') # 将msg转换为file_box file_box = await msg.to_file_box() # 获取图片名 img_name = file_box.name # 图片保存的路径 img_path = './images/' + img_name # 将图片保存到文件中 await file_box.to_file(file_path=img_path, overwrite=True) # 调用函数,获取图片新路径 img_new_path = img_to_anime(img_name, img_path) # 从文件中加载图片到file_box file_box_new = FileBox.from_file(img_new_path) await conversation.say(file_box_new) # 如果群聊有权限 if room is not None and self.room_allow[room][0]: conversation = room await conversation.ready() if text == '1': self.room_allow[room][1] = 1 await conversation.say('这是自动回复:已开启“将图片转换为动漫风格”功能,请发给我一张图片') # 如果消息类型是图片 if type == Message.Type.MESSAGE_TYPE_IMAGE: # 判断功能 if self.room_allow[room][1] == 1: # 关闭功能 self.room_allow[room][1] = None conversation = room await conversation.ready() await conversation.say('这是自动回复:正在飞速处理中...') # 将msg转换为file_box file_box = await msg.to_file_box() # 获取图片名 img_name = file_box.name # 图片保存的路径 img_path = './images/' + img_name # 将图片保存到文件中 await file_box.to_file(file_path=img_path, overwrite=True) # 调用函数,获取图片新路径 img_new_path = img_to_anime(img_name, img_path) # 从文件中加载图片到file_box file_box_new = FileBox.from_file(img_new_path) await conversation.say(file_box_new)
async def on_message(self, msg: Message): """ listen for message event """ from_contact = msg.talker() text = msg.text() type = msg.type() room = msg.room() # 不处理群消息 if room is None: if text == 'hi' or text == '你好': conversation = from_contact await conversation.ready() await conversation.say( '这是自动回复:机器人目前的功能有:\n1 收到"ding",自动回复"dong dong dong"\n2 收到"图片",自动回复一张图片\n3 收到两张图片,将第一张图片转换为第二张图片的风格并返回,如需使用此功能,请回复“风格转换”' ) if text == 'ding': conversation = from_contact await conversation.ready() await conversation.say('这是自动回复:dong dong dong') if text == '图片': conversation = from_contact # 从网络上加载图片到file_box img_url = 'https://images.unsplash.com/photo-1470770903676-69b98201ea1c?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1500&q=80' file_box = FileBox.from_url(img_url, name='xx.jpg') await conversation.ready() await conversation.say('这是自动回复:') await conversation.say(file_box) if text == '风格转换': self.img[1] = 0 conversation = from_contact await conversation.ready() await conversation.say('这是自动回复:请输入第一张图片') # 如果消息类型是图片 if type == Message.Type.MESSAGE_TYPE_IMAGE: if from_contact == self.friend_contact: self.img[0] = not self.img[0] if self.img[1] == 0: self.img[1] = 1 # 将msg转换为file_box file_box = await msg.to_file_box() # 获取图片名 self.img[2] = file_box.name # 图片保存的路径 self.img[3] = './images/' + self.img[2] # 将图片保存到文件中 await file_box.to_file(file_path=self.img[3], overwrite=True) conversation = from_contact await conversation.ready() await conversation.say('这是自动回复:请输入第二张图片') if self.img[1] == 1 and self.img[0]: self.img[1] = None conversation = from_contact await conversation.ready() await conversation.say('这是自动回复:正在飞速处理中...') # 将msg转换为file_box file_box_art = await msg.to_file_box() # 获取图片名 self.img[4] = file_box_art.name # 图片保存的路径 self.img[5] = './images/' + self.img[4] # 将图片保存到文件中 await file_box_art.to_file(file_path=self.img[5], overwrite=True) # 调用函数,获取图片新路径 img_new_path = img_to_art(self.img[2], self.img[3], self.img[5]) # 从文件中加载图片到file_box file_box_new = FileBox.from_file(img_new_path) await conversation.say(file_box_new)
async def on_message(self, msg: Message): """ listen for message event """ from_contact = msg.talker() text = msg.text() type = msg.type() room = msg.room() # username = from_contact.name if username=='KFu': print('message from myself') return # 不处理群消息 # if room is None: if msg.type() == Message.Type.MESSAGE_TYPE_IMAGE: print('__image') image_file_box = await msg.to_file_box() filename='p'+str(time.time())+'.jpg' await image_file_box.to_file(file_path=filename,overwrite=True) inputdata="#pic#"+filename bot = self.bm.run(username, inputdata) if bot is not None: # print('bot',bot) # print('bot replys',bot.replys[-1]) # print('bot.replys_index',bot.replys_index) for i in range(bot.replys_index): bot, rdict = self.tm.run(bot) print('rdict',rdict) if len(list(rdict.keys()))==0:continue if list(rdict.keys())[0] == "str": print('reply str') conversation: Union[ Room, Contact] = from_contact if room is None else room print('ready') await conversation.ready() print(list(rdict.values())[0]) await conversation.say(list(rdict.values())[0]) elif list(rdict.keys())[0] == "pic" or 'mov': print('reply pic/mov') conversation: Union[ Room, Contact] = from_contact if room is None else room await conversation.ready() try: file_box = FileBox.from_file(list(rdict.values())[0]) except Exception as e: print('file box error',e) file_box='嗯嗯' await conversation.say(file_box) elif msg.type() == Message.Type.MESSAGE_TYPE_TEXT: inputdata = "#str#" + msg.text() print('————text') bot = self.bm.run(username, inputdata) if bot is not None: # print('bot', bot) # print('bot replys',bot.replys[-1]) # print('bot.replys_index',bot.replys_index) for i in range(bot.replys_index): bot, rdict = self.tm.run(bot) print('rdict',rdict) if len(list(rdict.keys()))==0:continue if list(rdict.keys())[0] == "str": print('reply str') conversation: Union[ Room, Contact] = from_contact if room is None else room await conversation.ready() print('rdict[splitNum:]',list(rdict.values())[0]) await conversation.say(list(rdict.values())[0]) elif list(rdict.keys())[0] == "pic" or 'mov': print('reply pic/mov') conversation: Union[ Room, Contact] = from_contact if room is None else room await conversation.ready() try: file_box = FileBox.from_file(list(rdict.values())[0]) except Exception as e: print('file box error',e) file_box='嗯嗯' await conversation.say(file_box) else: print('__new for dict') conversation: Union[ Room, Contact] = from_contact if room is None else room await conversation.ready() await conversation.say('暂时不支持这种类型的消息哦')
async def message(msg: Message): """back on message""" from_contact = msg.talker() username = from_contact.name text = msg.text() room = msg.room() conversation: Union[Room, Contact] = from_contact if room is None else room global chat_friend global splitNum global bm global tm if "吐槽" in text or "图槽" in text or "树洞" in text: chat_friend.append(conversation) inputdata = "#str#" + msg.text() print('————text') bot = bm.run(username, inputdata) if bot is not None: # print('bot', bot) # print('bot replys',bot.replys[-1]) # print('bot.replys_index',bot.replys_index) for i in range(bot.replys_index): bot, rdict = tm.run(bot) print('rdict', rdict) if len(list(rdict.keys())) == 0: continue if list(rdict.keys())[0] == "str": print('reply str') conversation: Union[ Room, Contact] = from_contact if room is None else room await conversation.ready() print('rdict[splitNum:]', list(rdict.values())[0]) await conversation.say(list(rdict.values())[0]) elif list(rdict.keys())[0] == "pic" or 'mov': print('reply pic/mov') conversation: Union[ Room, Contact] = from_contact if room is None else room await conversation.ready() try: file_box = FileBox.from_file(list(rdict.values())[0]) except Exception as e: print('file box error', e) file_box = '嗯嗯' await conversation.say(file_box) # await conversation.ready() # await conversation.say('闲聊功能开启成功!现在你可以和我聊天啦!') return if conversation in chat_friend: if username == 'KFu': print('KFu') return if msg.type() == Message.Type.MESSAGE_TYPE_IMAGE: print('__image') image_file_box = await msg.to_file_box() filename = 'p' + str(time.time()) + '.jpg' await image_file_box.to_file(file_path=filename, overwrite=True) inputdata = "#pic#" + filename elif msg.type() == Message.Type.MESSAGE_TYPE_TEXT: print('--text') inputdata = "#str#" + msg.text() bot = bm.run(username, inputdata) if bot is not None: # print('bot', bot) # print('bot replys',bot.replys[-1]) # print('bot.replys_index',bot.replys_index) for i in range(bot.replys_index): bot, rdict = tm.run(bot) print('rdict', rdict) if len(list(rdict.keys())) == 0: continue if list(rdict.keys())[0] == "str": print('reply str') conversation: Union[ Room, Contact] = from_contact if room is None else room await conversation.ready() print('rdict[splitNum:]', list(rdict.values())[0]) await conversation.say(list(rdict.values())[0]) elif list(rdict.keys())[0] == "pic" or 'mov': print('reply pic/mov') conversation: Union[ Room, Contact] = from_contact if room is None else room await conversation.ready() try: file_box = FileBox.from_file( list(rdict.values())[0]) except Exception as e: print('file box error', e) file_box = '嗯嗯' await conversation.say(file_box) # data = TencentAI(text) # await conversation.ready() # await conversation.say(data) return else: print('not in friend') return