Esempio n. 1
0
def test_not_reply():
    """not saying to the bot"""
    controller = TagController(DataTransfer(Database(PATH, CONFIG)))
    reply = controller.handle_msg(quoted='null',
                                  msg='null',
                                  talker='me',
                                  to_bot=False)
    assert not reply
Esempio n. 2
0
def test_wrong_cron_format():
    """test timed delete with wrong format"""
    controller = TagController(DataTransfer(Database(PATH)))
    ifreply = controller.handle_msg(quoted=None,
                                    msg=f'{KEY_DELETE} {KEY_SPLIT}*-*-0-0-0-1',
                                    talker='me',
                                    to_bot=True)
    assert ifreply
    assert controller.reply == reply.parse_datetime_error()
Esempio n. 3
0
def test_expiry():
    """test expiry of messages"""
    interface = DataTransfer(Database(PATH))
    controller = TagController(interface)
    data_num = len(interface.get_all_msgs())
    expiry = datetime.now() + timedelta(seconds=1)
    msg = f'test expiry, {KEY_EXPIRY}{KEY_SPLIT}{str(expiry)}'
    controller.handle_msg(quoted='second', msg=msg, talker='me', to_bot=True)
    sleep(1)
    assert data_num == len(interface.get_all_msgs())
Esempio n. 4
0
def test_save_msg():
    """test save messaga"""
    interface = DataTransfer(Database(PATH))
    controller = TagController(interface)
    ifreply = controller.handle_msg(quoted='first',
                                    msg='test save msg',
                                    talker='me',
                                    to_bot=True)
    assert ifreply
    assert controller.reply == reply.save_msg_success()
    assert len(interface.get_all_msgs()) == 1
Esempio n. 5
0
 def __init__(self, data_path='database.db', config_path='config.pkl'):
     """params:
         data_path: path of database file,
         config_path: path of database config file, which
                      contains auto-created information"""
     self.data_path = data_path
     self.config_path = config_path
     self.interface = DataTransfer(Database(data_path, config_path))
     self.question_answering = QuestionAnswering(self.interface)
     self.tag_controller = TagController(self.interface)
     self.display = Display(self.interface)
Esempio n. 6
0
def test_delete():
    """test delete message"""
    interface = DataTransfer(Database(PATH))
    controller = TagController(interface)
    data_num = len(interface.get_all_msgs())
    ifreply = controller.handle_msg(quoted=None,
                                    msg=f'{KEY_DELETE} {KEY_SPLIT}1',
                                    talker='me',
                                    to_bot=True)
    assert ifreply
    assert controller.reply == reply.del_msg_success(1)
    assert len(interface.get_all_msgs()) == data_num - 1
Esempio n. 7
0
def test_timed_delete():
    """test timed delete"""
    controller = TagController(DataTransfer(Database(PATH)))
    ifreply = controller.handle_msg(quoted=None,
                                    msg=f'{KEY_DELETE} {KEY_SPLIT}*-*-2-0-0-1',
                                    talker='me',
                                    to_bot=True)
    assert ifreply
    # substitute former plan
    ifreply = controller.handle_msg(quoted=None,
                                    msg=f'{KEY_DELETE} {KEY_SPLIT}*-*-*-*-*-1',
                                    talker='me',
                                    to_bot=True)
    assert ifreply
Esempio n. 8
0
def test_create():
    """create tag_controller"""
    for path in [PATH, CONFIG]:
        try:
            os.remove(path)
        except FileNotFoundError:
            pass
    controller = TagController(DataTransfer(Database(PATH, CONFIG)))
Esempio n. 9
0
def test_stop_timed_delete():
    """test stop timed delete task"""
    controller = TagController(DataTransfer(Database(PATH)))
    controller.handle_msg(quoted=None,
                          msg=f'{KEY_DELETE} {KEY_SPLIT}*-*-2-0-0-1',
                          talker='me',
                          to_bot=True)
    ifreply = controller.handle_msg(quoted=None,
                                    msg=f'{KEY_DELETE}{KEY_SPLIT}{KEY_STOP}',
                                    talker='me',
                                    to_bot=True)
    assert ifreply
    assert controller.reply == reply.stop_timed_delete(True)
    ifreply = controller.handle_msg(quoted=None,
                                    msg=f'{KEY_DELETE}{KEY_SPLIT}{KEY_STOP}',
                                    talker='me',
                                    to_bot=True)
    assert ifreply
    assert controller.reply == reply.stop_timed_delete(False)
Esempio n. 10
0
class Tagging(WechatyPlugin):
    """tagging system plugin for bot"""
    @property
    def name(self) -> str:
        """get the name of the plugin"""
        return 'tagging'

    def __init__(self, data_path='database.db', config_path='config.pkl'):
        """params:
            data_path: path of database file,
            config_path: path of database config file, which
                         contains auto-created information"""
        self.data_path = data_path
        self.config_path = config_path
        self.interface = DataTransfer(Database(data_path, config_path))
        self.question_answering = QuestionAnswering(self.interface)
        self.tag_controller = TagController(self.interface)
        self.display = Display(self.interface)

    async def on_message(self, msg: Message):
        """listen message event"""
        self_contact = await self.my_self()
        from_contact = msg.talker()
        log.info('below are dict of message: ')
        log.info(str(msg.__dict__))
        quoted, text, mention = split_quote_and_mention(msg.text())
        log.info('finish spliting')
        log.info(f'quoted: {quoted}, text: {text}, mention: {mention}')
        room = msg.room()
        to_bot = self_contact.get_id() in msg.payload.mention_ids or\
                 self_contact.get_id() == msg.payload.to_id or\
                 self_contact.payload.name == mention
        conversation: Union[
            Room, Contact] = from_contact if room is None else room
        await conversation.ready()
        log.info(f'finish preprocess, talker: {from_contact.get_id()}, to_bot: {to_bot}')
        try:
            if self.tag_controller.handle_msg(quoted, text, from_contact.get_id(), to_bot):
                log.info('tag controller found reply')
                await conversation.say(self.tag_controller.get_reply())
            elif self.question_answering.handle_msg(text, to_bot):
                log.info('question answering found reply')
                await conversation.say(self.question_answering.get_reply())
            elif self.display.handle_msg(text, to_bot):
                log.info('display found reply')
                await conversation.say(self.display.get_reply())
        except Exception as error:
            log.info(f'something went wrong for tagging plugin: {error}')

    async def my_self(self) -> Contact:
        """get self contact"""
        my_contact_id = self.bot.contact_id
        contact = Contact.load(my_contact_id)
        await contact.ready()
        log.info(f'load self contact: {contact}')
        return contact

    async def on_login(self, contact: Contact):
        """store contact of self"""
        log.info(f'login as {contact}')
        bot_contact = contact