Exemplo n.º 1
0
async def sendMediaMsgToContact(contact, fileUrl, filePath):
    # print(contact)
    if not contact:  # 好友不存在直接返回
        return
    if fileUrl:
        fileBox1 = FileBox.from_url(url=fileUrl, name='wxapplet.png')
        await contact.say(fileBox1)
    if filePath:
        fileBox2 = FileBox.from_file(filePath)
        await contact.say(fileBox2)
Exemplo n.º 2
0
 def files(self, args: str):
     if args:
         if os.path.exists('files/%s' % args):
             return FileBox.from_file('files/%s' % args)
         return 'file %s not exists' % args
     else:
         return str(os.listdir('files'))
Exemplo n.º 3
0
 async def on_message(self, msg: Message):
     """listen message event"""
     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)
Exemplo n.º 4
0
    async def on_message(self, msg: Message):
        """
        listen for message event
        """
        from_contact: Contact = msg.talker()
        text: str = msg.text()
        room: Optional[Room] = msg.room()
        msg_type: MessageType = msg.type()
        file_box: Optional[FileBox] = None
        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)

        elif msg_type == MessageType.MESSAGE_TYPE_IMAGE:
            logger.info('receving image file')
            # file_box: FileBox = await msg.to_file_box()
            image: Image = msg.to_image()

            hd_file_box: FileBox = await image.hd()
            await hd_file_box.to_file('./hd-image.jpg', overwrite=True)

            thumbnail_file_box: FileBox = await image.thumbnail()
            await thumbnail_file_box.to_file('./thumbnail-image.jpg',
                                             overwrite=True)
            artwork_file_box: FileBox = await image.artwork()
            await artwork_file_box.to_file('./artwork-image.jpg',
                                           overwrite=True)
            # reply the image
            await msg.say(hd_file_box)

        # pylint: disable=C0301
        elif msg_type in [
                MessageType.MESSAGE_TYPE_AUDIO,
                MessageType.MESSAGE_TYPE_ATTACHMENT,
                MessageType.MESSAGE_TYPE_VIDEO
        ]:
            logger.info('receving file ...')
            file_box = await msg.to_file_box()
            if file_box:
                await file_box.to_file(file_box.name)

        elif msg_type == MessageType.MESSAGE_TYPE_MINI_PROGRAM:
            logger.info('receving mini-program ...')
            mini_program: Optional[MiniProgram] = await msg.to_mini_program()
            if mini_program:
                await msg.say(mini_program)

        elif text == 'get room members' and room:
            logger.info('get room members ...')
            room_members: List[Contact] = await room.member_list()
            names: List[str] = [
                room_member.name for room_member in room_members
            ]
            await msg.say('\n'.join(names))

        elif text.startswith('remove room member:'):
            logger.info('remove room member:')
            if not room:
                await msg.say('this is not room zone')
                return

            room_member_name = text[len('remove room member:') + 1:]

            room_member: Optional[Contact] = await room.member(
                query=RoomMemberQueryFilter(name=room_member_name))
            if room_member:
                if self.login_user and self.login_user.contact_id in room.payload.admin_ids:
                    await room.delete(room_member)
                else:
                    await msg.say('登录用户不是该群管理员...')

            else:
                await msg.say(
                    f'can not fine room member by name<{room_member_name}>')
        elif text.startswith('get room topic'):
            logger.info('get room topic')
            if room:
                topic: Optional[str] = await room.topic()
                if topic:
                    await msg.say(topic)

        elif text.startswith('rename room topic:'):
            logger.info('rename room topic ...')
            if room:
                new_topic = text[len('rename room topic:') + 1:]
                await msg.say(new_topic)
        elif text.startswith('add new friend:'):
            logger.info('add new friendship ...')
            identity_info = text[len('add new friend:') + 1:]
            weixin_contact: Optional[Contact] = await self.Friendship.search(
                weixin=identity_info)
            phone_contact: Optional[Contact] = await self.Friendship.search(
                phone=identity_info)
            contact: Optional[Contact] = weixin_contact or phone_contact
            if contact:
                self.Friendship.add(contact, 'hello world ...')

        elif text.startswith('at me'):
            await msg.say(self.login_user)

        else:
            pass
Exemplo n.º 5
0
async def on_message(msg: Message):
    if msg.text() == 'ding':
        await msg.say('这是自动回复: dong dong dong')

    if msg.text() == '小队长':
        await msg.say('我好想你的!!快点好起来,上班来看我!!')

    if msg.text() == '小仙女':
        await msg.say('宇宙超级无敌可爱美丽的人,303之光!')

    if msg.text() == '小余':
        await msg.say('一直想要骗我钱的女人!但是我还是很爱她!')

    if msg.text() == 'hi' or msg.text() == '你好':
        await msg.say(
            '这是自动回复: 机器人目前的功能是\n- 收到"ding", 自动回复"dong dong dong"\n- 收到"图片", 自动回复一张图片\n- 还会把图片变成漫画样子,不过这个案例是参考细菌的\n- 收到"藏头诗:我是菜鸟",自动以"我是菜鸟"作藏头诗一首,当然也可以输入其他的4个字作诗'
        )

    if msg.text().startswith('藏头诗'):
        #await msg.say('请输入4个字作为藏头诗的头')
        test_texts = [msg.text()[-4:]]
        results = module.generate(texts=test_texts, use_gpu=True, beam_width=5)
        #for result in results:
        await msg.say(results[0][0])

    if msg.text() == '图片':
        url = 'http://qrul2d5a1.hn-bkt.clouddn.com/image/street.jpg'
        file_box_12 = FileBox.from_url(url=url, name='xx.jpg')

        await msg.say(file_box_12)

    if msg.text() == '周深':
        url = 'https://z3.ax1x.com/2021/04/27/gC0EtA.jpg'
        file_box_11 = FileBox.from_url(url=url, name='xx.jpg')

        await msg.say(file_box_11)

    if msg.text() == '费玉清' or msg.text() == '小哥':
        url = 'https://z3.ax1x.com/2021/04/27/gCBeUJ.jpg'

        # 构建一个FileBox
        file_box_1 = FileBox.from_url(url=url, name='xx.jpg')

        await msg.say(file_box_1)
    # 如果收到的message是一张图片
    if msg.type() == Message.Type.MESSAGE_TYPE_IMAGE:

        # 将Message转换为FileBox
        file_box_2 = await msg.to_file_box()

        # 获取图片名
        img_name = file_box_2.name

        # 图片保存的路径
        img_path = './image/' + img_name

        # 将图片保存为本地文件
        await file_box_2.to_file(file_path=img_path)

        # 调用图片风格转换的函数
        img_new_path = img_transform(img_path, img_name)
        img_new_path1 = img_transform1(img_path, img_name)

        # 从新的路径获取图片
        file_box_3 = FileBox.from_file(img_new_path)
        file_box_4 = FileBox.from_file(img_new_path1)
        await msg.say(file_box_4)