async def on_message(self, msg: Message): """ listen for message event """ from_contact = msg.talker() text = msg.text() 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. 收到"图片",自动回复一张图片') 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)
async def run(): """async run method""" img_url = 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy' \ '/it/u=1257042014,3164688936&fm=26&gp=0.jpg' plugin = AutoReplyPlugin(options=AutoReplyOptions(rules=[ AutoReplyRule(keyword='ding', reply_content='dong'), AutoReplyRule(keyword='七龙珠', reply_content='七龙珠'), AutoReplyRule(keyword='七龙珠', reply_content=FileBox.from_url(img_url, name='python.png')), AutoReplyRule(keyword='wechaty-icon', reply_content=FileBox.from_url(img_url, name='python.png')) ], matchers=[ ContactMatcher('秋客'), ])) bot = Wechaty().use(plugin) await bot.start()
async def message_emoticon(self, message: str) -> FileBox: """ emoticon from message :param message: :return: """ DOMTree = xml.dom.minidom.parseString(message) collection = DOMTree.documentElement file_box = FileBox.from_url( url=collection.getElementsByTagName('emoji')[0].getAttribute('cdnurl'), name=collection.getElementsByTagName('emoji')[0].getAttribute('md5') + '.gif' ) return file_box
async def message(msg: Message) -> None: """back on message""" from_contact = msg.talker() text = msg.text() room = msg.room() if text == '#ding': conversation: Union[ Room, Contact] = from_contact if room is None else room await conversation.ready() await conversation.say('dong') file_box = FileBox.from_url( 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/' 'u=1116676390,2305043183&fm=26&gp=0.jpg', name='ding-dong.jpg') await conversation.say(file_box)
async def room_avatar(self, room_id: str) -> FileBox: """ get room avatar :param room_id: :return: """ room_avatar_response = await self.puppet_stub.room_avatar(id=room_id) file_box_data = json.loads(room_avatar_response.filebox) if 'remoteUrl' not in file_box_data: raise WechatyPuppetPayloadError('invalid room avatar response') file_box = FileBox.from_url(url=file_box_data['remoteUrl'], name=f'avatar-{room_id}.jpeg') return file_box
async def room_avatar(self, room_id: str) -> FileBox: """ get room avatar :param room_id: :return: """ if self.puppet_stub is None: raise Exception('puppet_stub should not be none') room_avatar_response = await self.puppet_stub.room_avatar(id=room_id) file_box_data = json.loads(room_avatar_response.filebox) if 'remoteUrl' not in file_box_data: raise Exception('invalid room avatar response') file_box = FileBox.from_url(url=file_box_data['remoteUrl'], name=f'avatar-{room_id}.jpeg') return file_box
async def message(msg: Message): """back on message""" from_contact = msg.talker() text = msg.text() room = msg.room() conversation: Union[Room, Contact] = from_contact if room is None else room global chat_friend if "#关闭闲聊" == text: try: chat_friend.remove(conversation) except Exception as e: return await conversation.ready() await conversation.say('好的,有需要随时叫我') return elif "#开启闲聊" == text: chat_friend.append(conversation) await conversation.ready() await conversation.say('闲聊功能开启成功!现在你可以和我聊天啦!') return if conversation in chat_friend: data = TencentAI(text) await conversation.ready() await conversation.say(data) return if text == '#ding': await conversation.ready() await conversation.say('dong') file_box = FileBox.from_url( 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/' 'u=1116676390,2305043183&fm=26&gp=0.jpg', name='ding-dong.jpg') await conversation.say(file_box)
def get_music_file_url(name: str) -> FileBox: log.info("name=%s" % name) music_url, music_name = get_music_url(name) log.info("url=%s,name=%s" % (music_url, music_name)) return FileBox.from_url(music_url, music_name)
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)