Some fixes

This commit is contained in:
2024-02-11 12:41:41 +03:00
parent f3cdeeff3c
commit 883479b0d8
3 changed files with 11 additions and 9 deletions

16
main.py
View File

@@ -8,13 +8,13 @@ import aiohttp
from handlers.admin_commands import AdminCommands from handlers.admin_commands import AdminCommands
from handlers.user_commands import UserCommands from handlers.user_commands import UserCommands
from neuroapi.config import Config from neuroapi.config import GlobalConfig as Config
from neuroapi.types import NeuroApiBot from neuroapi.types import NeuroApiBot
async def delay_bot()->None: async def delay_bot()->None:
if Config().token is None: if Config().token is None:
print('Delay bot needs token in environment') logging.warning('Delay bot needs token in environment')
return return
bot = NeuroApiBot(Config().token) bot = NeuroApiBot(Config().token)
bot.include_router(AdminCommands, UserCommands) bot.include_router(AdminCommands, UserCommands)
@@ -22,7 +22,7 @@ async def delay_bot()->None:
async def proxy_bot()->None: async def proxy_bot()->None:
if Config().proxy_token is None: if Config().proxy_token is None:
print('Proxy bot needs token in environment') logging.warning('Proxy bot needs token in environment')
return return
bot = NeuroApiBot(Config().proxy_token) bot = NeuroApiBot(Config().proxy_token)
bot.include_router() bot.include_router()
@@ -30,30 +30,30 @@ async def proxy_bot()->None:
async def main() -> None: async def main() -> None:
for i in range(5): for i in range(5):
print(f'Checking connectivity to backend ({i+1}/5)...') logging.warning(f'Checking connectivity to backend ({i+1}/5)...')
try: try:
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
response = await session.get(Config().api_url+'/ping') response = await session.get(Config().api_url+'/ping')
data = str(await response.content.read(), encoding='utf-8') data = str(await response.content.read(), encoding='utf-8')
if data == 'pong': if data == 'pong':
print('Successfully connected to backend') logging.warning('Successfully connected to backend')
break break
else: else:
raise TimeoutError() raise TimeoutError()
except: except:
print('Error! Waiting 3 secs and retrying...') logging.error('Waiting 3 secs and retrying...')
await asyncio.sleep(3) await asyncio.sleep(3)
tasks = [asyncio.create_task(delay_bot()), asyncio.create_task(proxy_bot())] tasks = [asyncio.create_task(delay_bot()), asyncio.create_task(proxy_bot())]
await asyncio.gather(*tasks) await asyncio.gather(*tasks)
if __name__ == '__main__': if __name__ == '__main__':
logging.basicConfig(level=logging.INFO, stream=sys.stdout) logging.basicConfig(level=logging.WARNING, stream=sys.stdout, format='%(asctime)s %(levelname)s: %(message)s', datefmt='%d.%m.%Y %H:%M:%S')
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
if platform.system() == 'Windows': if platform.system() == 'Windows':
try: try:
loop.run_until_complete(main()) loop.run_until_complete(main())
except KeyboardInterrupt: except KeyboardInterrupt:
print("KeyboardInterrupt occurred") logging.error("KeyboardInterrupt occurred")
finally: finally:
loop.close() loop.close()
else: else:

View File

@@ -78,7 +78,6 @@ class Post(ApiMethod):
if message_entities is not None: if message_entities is not None:
mes_ent = list(map(lambda x: x.model_dump(), message_entities)) mes_ent = list(map(lambda x: x.model_dump(), message_entities))
arr =[] arr =[]
print(mes_ent)
for item in mes_ent: for item in mes_ent:
if item['type'] == 'bot_command': continue if item['type'] == 'bot_command': continue
item['offset'] -= 7+len(order) item['offset'] -= 7+len(order)

View File

@@ -24,6 +24,9 @@ class Post(ApiModel):
@classmethod @classmethod
def from_dict(cls: 'Post', obj: Dict[str, Any]) -> 'Post': def from_dict(cls: 'Post', obj: Dict[str, Any]) -> 'Post':
mes_ent = json.loads(obj.get('message_entities', '[]')) mes_ent = json.loads(obj.get('message_entities', '[]'))
media_group_id_data = obj.get('media_group_id')
media_group_id = media_group_id_data if media_group_id_data is not None else 'None'
obj['media_group_id'] = media_group_id
obj['message_entities'] = mes_ent obj['message_entities'] = mes_ent
return cls(**obj) return cls(**obj)