Exemple #1
0
 async def watcher(self, message):
     if not self.config["CLIENT_KEY"]:
         logger.debug("no key set for lydia, returning")
         return
     if not isinstance(message, types.Message):
         return
     if self._lydia is None:
         self._lydia = coffeehouse.LydiaAI(self.config["CLIENT_KEY"])
     if (isinstance(message.to_id, types.PeerUser) and not self.get_allowed(message.from_id)) or \
             self.is_forced(utils.get_chat_id(message), message.from_id):
         user = await utils.get_user(message)
         if user.is_self or user.bot or user.verified:
             logger.debug("User is self, bot or verified.")
             return
         else:
             if not isinstance(message.message, str):
                 return
             if len(message.message) == 0:
                 return
             if self.config["IGNORE_NO_COMMON"] and not self.is_forced(utils.get_chat_id(message), message.from_id):
                 fulluser = await message.client(functions.users.GetFullUserRequest(await utils.get_user(message)))
                 if fulluser.common_chats_count == 0:
                     return
             await message.client(functions.messages.SetTypingRequest(
                 peer=await utils.get_user(message),
                 action=types.SendMessageTypingAction()
             ))
             try:
                 # Get a session
                 sessions = self._db.get(__name__, "sessions", {})
                 session = sessions.get(utils.get_chat_id(message), None)
                 if session is None or session["expires"] < time.time():
                     session = await utils.run_sync(self._lydia.create_session)
                     session = {"session_id": session.id, "expires": session.expires}
                     logger.debug(session)
                     sessions[utils.get_chat_id(message)] = session
                     logger.debug(sessions)
                     self._db.set(__name__, "sessions", sessions)
                     if self._cleanup is not None:
                         self._cleanup.cancel()
                     self._cleanup = asyncio.ensure_future(self.schedule_cleanups())
                 logger.debug(session)
                 # AI Response method
                 msg = message.message
                 airesp = await utils.run_sync(self._lydia.think_thought, session["session_id"], str(msg))
                 logger.debug("AI says %s", airesp)
                 if random.randint(0, 1) and isinstance(message.to_id, types.PeerUser):
                     await message.respond(airesp)
                 else:
                     await message.reply(airesp)
             finally:
                 await message.client(functions.messages.SetTypingRequest(
                     peer=await utils.get_user(message),
                     action=types.SendMessageCancelAction()
                 ))
Exemple #2
0
 async def watcher(self, message):
     if self.config["CLIENT_KEY"] == "":
         logger.debug("no key set for lydia, returning")
         return
     if getattr(message.to_id, 'user_id', None) == self._me.id:
         logger.debug("pm'd!")
         user = await utils.get_user(message)
         if user.is_self or user.bot or user.verified:
             logger.debug("User is self, bot or verified.")
             return
         if self.get_allowed(message.from_id):
             logger.debug(
                 "PM received from user who is not using AI service")
         else:
             await message.client(
                 functions.messages.SetTypingRequest(
                     peer=await utils.get_user(message),
                     action=types.SendMessageTypingAction()))
             try:
                 # Get a session
                 sessions = self._db.get(__name__, "sessions", {})
                 session = sessions.get(message.from_id, None)
                 if session is None or session["expires"] < time.time():
                     session = await self._lydia.create_session()
                     logger.debug(session)
                     sessions[message.from_id] = session
                     logger.debug(sessions)
                     self._db.set(__name__, "sessions", sessions)
                     if not self._cleanup is None:
                         self._cleanup.cancel()
                     self._cleanup = asyncio.ensure_future(
                         self.schedule_cleanups())
                 logger.debug(session)
                 # AI Response method
                 msg = message.message if isinstance(message.message,
                                                     str) else " "
                 airesp = await self._lydia.think(session["session_id"],
                                                  str(msg))
                 logger.debug("AI says %s", airesp)
                 if random.randint(0, 1):
                     await message.respond(airesp["output"])
                 else:
                     await message.reply(airesp["output"])
             finally:
                 await message.client(
                     functions.messages.SetTypingRequest(
                         peer=await utils.get_user(message),
                         action=types.SendMessageCancelAction()))
 async def watcher(self, message):
     if getattr(message.to_id, 'user_id', None) == self._me.id:
         logger.debug("pm'd!")
         if message.from_id in self._ratelimit:
             self._ratelimit.remove(message.from_id)
         else:
             self._ratelimit += [message.from_id]
         if self.get_allowed(message.from_id):
             logger.debug(
                 "PM received from user who is not using AI service")
         else:
             await message.client(
                 functions.messages.SetTypingRequest(
                     peer=message.from_id,
                     action=types.SendMessageTypingAction()))
             # AI Response method
             await message.respond(await self._lydia.think(
                 str(message.from_id), str(message.message)))
             await message.client(
                 functions.messages.SetTypingRequest(
                     peer=message.from_id,
                     action=types.SendMessageCancelAction()))