Repository navigation
Expand file tree
/
Copy pathbot.py
More file actions
79 lines (64 loc) · 2.69 KB
/
Copy pathbot.py
File metadata and controls
79 lines (64 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import asyncio
import logging
import sys
from config import config
from core.dedup import DeduplicationCache
from core.router import BridgeRouter
from core.orchestrator import BridgeOrchestrator
from adapters.discord_adapter import DiscordAdapter
from adapters.telegram_adapter import TelegramAdapter
from adapters.signal_adapter import SignalAdapter
# Configure root logger
logging.basicConfig(
stream=sys.stdout,
level=logging.INFO,
format='%(asctime)s [%(levelname)s] %(name)s: %(message)s'
)
logger = logging.getLogger("BridgeBot")
async def main():
logger.info("Initializing Bridge Bot...")
# 1. Init Deduplication Cache
dedup_cache = DeduplicationCache(config.REDIS_URL)
await dedup_cache.connect()
# 2. Init Router
router = BridgeRouter(config.channel_map)
# 3. Init Orchestrator
orchestrator = BridgeOrchestrator()
# 4. Initialize and add Adapters based on config
if config.DISCORD_ENABLED and config.DISCORD_TOKEN:
discord_adapter = DiscordAdapter(config.DISCORD_TOKEN, dedup_cache)
orchestrator.add_adapter(discord_adapter)
else:
logger.warning("Discord is disabled or DISCORD_TOKEN is missing.")
if config.TELEGRAM_ENABLED and config.TELEGRAM_TOKEN:
telegram_adapter = TelegramAdapter(config.TELEGRAM_TOKEN, dedup_cache)
orchestrator.add_adapter(telegram_adapter)
else:
logger.warning("Telegram is disabled or TELEGRAM_TOKEN is missing.")
if config.SIGNAL_ENABLED:
logger.info("Signal is enabled but not fully implemented.")
# signal_adapter = SignalAdapter(config.SIGNAL_PHONE_NUMBER, config.SIGNAL_API_URL, dedup_cache)
# orchestrator.add_adapter(signal_adapter)
# 5. Wire up the routing callbacks for the loaded adapters
for name, adapter in orchestrator.adapters.items():
# Define the async callback that passes the message to the router
async def on_msg(msg, source_id, curr_adapter_name=name):
logger.info(f"Routing message from {curr_adapter_name} channel {source_id}")
await router.route_message(msg, source_id, orchestrator.adapters)
adapter.on_message_callback = on_msg
try:
# 6. Run all
await orchestrator.run_all()
except KeyboardInterrupt:
logger.info("Received interrupt, shutting down...")
finally:
await orchestrator.shutdown()
await dedup_cache.close()
logger.info("Exited gracefully.")
if __name__ == "__main__":
if sys.platform == "win32":
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
try:
asyncio.run(main())
except KeyboardInterrupt:
pass