示例#1
0
 async def on_message(self, msg: Message):
     if msg.age() > 3 * 60:
         log.info(
             'Bot' + 'on(message) skip age("%d") > 3 * 60 seconds: "%s"',
             msg.age(), msg)
         return
     room = msg.room()
     talker = msg.talker()
     text = msg.text()
     if not talker:
         return
     if msg.is_self():
         return
     if re.search('^ding$', text):
         if room:
             if re.search('^ding', await room.topic()):
                 await get_out_room(talker, room)
         else:
             try:
                 dingRoom = await self.Room.find('^ding')
                 if dingRoom:
                     log.info('Bot' + 'onMessage: got dingRoom: "%s"' %
                              await dingRoom.topic())
                     if await dingRoom.has(talker):
                         topic = await dingRoom.topic()
                         log.info(
                             'Bot' +
                             'onMessage: sender has already in dingRoom')
                         await dingRoom.say(
                             'I found you have joined in room "{0}"!'.
                             format(topic), talker)
                         await talker.say(
                             'no need to ding again, because you are already in room: "{}"'
                             .format(topic))
                     else:
                         log.info(
                             'Bot' +
                             'onMessage: add sender("%s") to dingRoom("%s")'
                             % (talker.name, dingRoom.topic()))
                         await talker.say('ok, I will put you in ding room!'
                                          )
                         await put_in_room(talker, dingRoom)
                 else:
                     log.info(
                         'Bot' +
                         'onMessage: dingRoom not found, try to create one')
                     newRoom = await create_ding_room(self, talker)
                     print('create_ding_room id:', newRoom.id)
                     await manage_ding_room(self)
             except Exception as e:
                 log.exception(e)
    async def on_message(self, msg: Message):
        if msg.age() > 3 * 60:
            log.info(
                'Bot' + 'on(message) skip age("%d") > 3 * 60 seconds: "%s"',
                msg.age(), msg)
            return
        room = msg.room()
        talker = msg.talker()
        text = msg.text()
        if not talker:
            return
        if msg.is_self():
            return

        if re.search('^market$', text):
            if room:
                if re.search(TOPIC_NAME, await room.topic()):
                    await get_out_room(talker, room)
            else:
                try:
                    marketRoom = await self.Room.find(TOPIC_NAME)
                    if marketRoom:
                        log.info('Bot' + 'onMessage: got marketRoom: "%s"' %
                                 await marketRoom.topic())
                        if await marketRoom.has(talker):
                            topic = await marketRoom.topic()
                            log.info(
                                'Bot' +
                                'onMessage: sender has already in marketRoom')
                            await talker.say("已加入群组{}".format(topic))
                        else:
                            log.info(
                                'Bot' +
                                'onMessage: add sender("%s") to marketRoom("%s")'
                                % (talker.name, marketRoom.topic()))
                            await talker.say("邀请加入群组")
                            await put_in_room(talker, marketRoom)
                    else:
                        log.info(
                            'Bot' +
                            'onMessage: marketRoom not found, try to create one'
                        )
                        await create_market_room(self, talker)
                        await manage_market_room(self)
                except Exception as e:
                    log.exception(e)