def ask_lydia(user_id, lydia_query, coffeehouse_api_key): lydia = LydiaAI(coffeehouse_api_key) conn = sqlite3.connect('db.sqlite3') curr = conn.cursor() curr.execute(f'SELECT * FROM lydia WHERE user_id="{user_id}"') rows = curr.fetchall() if rows == []: session = lydia.create_session() curr.execute( f'INSERT INTO lydia VALUES ("{user_id}","{session.id}","{session.expires}")' ) conn.commit() conn.close() return session.think_thought(lydia_query[1:]) else: if int(time.time()) > int(rows[0][2]): curr.execute(f'DELETE FROM lydia WHERE (user_id = "{user_id}")') session = lydia.create_session() curr.execute( f'INSERT INTO lydia VALUES ("{user_id}","{session.id}","{session.expires}")' ) conn.commit() conn.close() return session.think_thought(lydia_query[1:]) else: conn.close() return lydia.think_thought(rows[0][1], lydia_query[1:])
async def lydia_private(_client, message): global lydia_status, coffeehouse_api, lydia, session if lydia_api == "": await message.edit( "`lydia API key is not set!\nSet your lydia API key by adding Config Vars in heroku with " "name lydia_api with value your lydia key API`") return if lydia_status: await message.edit("Turning off lydia...") asyncio.sleep(0.5) lydia_status = False await message.edit("Lydia will not reply your message") else: await message.edit("Turning on lydia...") try: coffeehouse_api = API(lydia_api) # Create Lydia instance lydia = LydiaAI(coffeehouse_api) # Create a new chat session (Like a conversation) session = lydia.create_session() except: await message.edit("Wrong lydia API key!") return lydia_status = True await message.edit("now Lydia will reply your message!")
async def lydia_disable_enable(event): if event.fwd_from: return if Config.LYDIA_API is None: await event.edit( "please add required `LYDIA_API` env var,get it from coffeehouse.intellivoid.net" ) return else: api_key = Config.LYDIA_API api_client = API(api_key) lydia = LydiaAI(api_client) input_str = event.pattern_match.group(1) if event.reply_to_msg_id is not None or input_str == "list" or event.is_private: reply_msg = None user_id = None chat_id = event.chat_id if event.is_private: user_id = event.chat_id if event.reply_to_msg_id is not None: reply_msg = await event.get_reply_message() user_id = reply_msg.from_id await event.edit("Processing...") if input_str == "enable": session = lydia.create_session() logger.info(session) logger.info(add_s(user_id, chat_id, session.id, session.expires)) await event.edit(f"Hello there {user_id}!") elif input_str == "disable": logger.info(remove_s(user_id, chat_id)) await event.edit(f"No, no, no, i am out.") elif input_str == "list": lsts = get_all_s() if len(lsts) > 0: output_str = "AI enabled users:\n\n" for lydia_ai in lsts: output_str += f"[user](tg://user?id={lydia_ai.user_id}) in chat `{lydia_ai.chat_id}`\n" else: output_str = "no Lydia AI enabled users / chats. Start by replying `.enableai` to any user in any chat!" if len(output_str) > Config.MAX_MESSAGE_SIZE_LIMIT: with io.BytesIO(str.encode(output_str)) as out_file: out_file.name = "lydia_ai.text" await borg.send_file(event.chat_id, out_file, force_document=True, allow_cache=False, caption="Lydia AI enabled users", reply_to=event) else: await event.edit(output_str) else: await event.edit( "Reply To User Message to Add / Delete them from Lydia Auto-Chat." ) else: await event.edit( "Reply To A User's Message to Add / Delete them from Lydia Auto-Chat." )
async def lydia_disable_enable(event): if event.fwd_from: return if ENV.LYDIA_API is None: await event.edit("Please add required `LYDIA_API` enviroment variable." ) return else: api_key = ENV.LYDIA_API api_client = API(api_key) lydia = LydiaAI(api_client) input_str = event.pattern_match.group(1) if event.reply_to_msg_id is not None or input_str == "list" or event.is_private: reply_msg = None user_id = None chat_id = event.chat_id if event.is_private: user_id = event.chat_id if event.reply_to_msg_id is not None: reply_msg = await event.get_reply_message() user_id = reply_msg.from_id # await event.edit("Processing...") if input_str == "enable": session = lydia.create_session() logger.info(session) logger.info(add_s(user_id, chat_id, session.id, session.expires)) await event.edit(f"Lydia mode activated!") elif input_str == "disable": logger.info(remove_s(user_id, chat_id)) await event.edit(f"Lydia was yeeted by CyberDoge.") elif input_str == "list": lsts = get_all_s() if len(lsts) > 0: output_str = "AI enabled users:\n\n" for lydia_ai in lsts: output_str += f"[user](tg://user?id={lydia_ai.user_id}) in chat `{lydia_ai.chat_id}`\n" else: output_str = "No Lydia AI enabled users / chats. Start by replying `.enableai` to any user in any chat!" if len(output_str) > ENV.MAX_MESSAGE_SIZE_LIMIT: with io.BytesIO(str.encode(output_str)) as out_file: out_file.name = "lydia_ai.text" await borg.send_file(event.chat_id, out_file, force_document=True, allow_cache=False, caption="Lydia AI enabled users", reply_to=event) else: await event.edit(output_str) else: await event.edit( "Reply to a user's message to add / delete them from lydia ai-chat." ) else: await event.edit( "Reply to a user's message to add / delete them from Lydia ai-chat." )