Refactor and implement queueing messages
This commit is contained in:
46
core/lib/bot.py
Normal file
46
core/lib/bot.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from signalbot import SignalBot
|
||||
import aiohttp
|
||||
|
||||
from core.util import logs
|
||||
|
||||
log = logs.get_logger("signalbot")
|
||||
|
||||
|
||||
class NewSignalBot(SignalBot):
|
||||
def __init__(self, config):
|
||||
super().__init__(config)
|
||||
self.bot_uuid = None # Initialize with None
|
||||
|
||||
async def get_own_uuid(self) -> str:
|
||||
"""Fetch bot's UUID by checking contacts, groups, or profile."""
|
||||
async with aiohttp.ClientSession() as session:
|
||||
uri_contacts = f"http://{self._signal.signal_service}/v1/contacts/{self._signal.phone_number}"
|
||||
try:
|
||||
resp = await session.get(uri_contacts)
|
||||
if resp.status == 200:
|
||||
contacts_data = await resp.json()
|
||||
if isinstance(contacts_data, list):
|
||||
for contact in contacts_data:
|
||||
if contact.get("number") == self._phone_number:
|
||||
return contact.get("uuid")
|
||||
except Exception as e:
|
||||
log.error(f"Failed to get UUID from contacts: {e}")
|
||||
|
||||
async def initialize_bot(self):
|
||||
"""Fetch bot's UUID and store it in self.bot_uuid."""
|
||||
try:
|
||||
self.bot_uuid = await self.get_own_uuid()
|
||||
if self.bot_uuid:
|
||||
log.info(f"Own UUID: {self.bot_uuid}")
|
||||
else:
|
||||
log.warning("Unable to fetch bot UUID.")
|
||||
except Exception as e:
|
||||
log.error(f"Failed to initialize bot UUID: {e}")
|
||||
|
||||
def start(self):
|
||||
"""Start bot without blocking event loop."""
|
||||
self._event_loop.create_task(self.initialize_bot()) # Fetch UUID first
|
||||
self._event_loop.create_task(self._detect_groups()) # Sync groups
|
||||
self._event_loop.create_task(self._produce_consume_messages()) # Process messages
|
||||
|
||||
self.scheduler.start() # Start async job scheduler
|
||||
Reference in New Issue
Block a user