Files
ajarbot/examples/example_bot_usage.py

76 lines
1.9 KiB
Python
Raw Normal View History

"""
Example: Using the adapter system programmatically.
Demonstrates how to integrate adapters into your own code,
rather than using the bot_runner.py CLI.
"""
import asyncio
from adapters.base import AdapterConfig
from adapters.runtime import (
AdapterRuntime,
command_preprocessor,
markdown_postprocessor,
)
from adapters.slack.adapter import SlackAdapter
from adapters.telegram.adapter import TelegramAdapter
from agent import Agent
async def main() -> None:
# 1. Create the agent
agent = Agent(
provider="claude",
workspace_dir="./memory_workspace",
enable_heartbeat=False,
)
# 2. Create runtime
runtime = AdapterRuntime(agent)
# 3. Add preprocessors and postprocessors
runtime.add_preprocessor(command_preprocessor)
runtime.add_postprocessor(markdown_postprocessor)
# 4. Configure Slack adapter
slack_adapter = SlackAdapter(AdapterConfig(
platform="slack",
enabled=True,
credentials={
"bot_token": "xoxb-YOUR-TOKEN",
"app_token": "xapp-YOUR-TOKEN",
},
))
runtime.add_adapter(slack_adapter)
# 5. Configure Telegram adapter
telegram_adapter = TelegramAdapter(AdapterConfig(
platform="telegram",
enabled=True,
credentials={"bot_token": "YOUR-TELEGRAM-TOKEN"},
settings={"parse_mode": "Markdown"},
))
runtime.add_adapter(telegram_adapter)
# 6. Map users (optional)
runtime.map_user("slack:U12345", "alice")
runtime.map_user("telegram:123456789", "alice")
# 7. Start runtime
await runtime.start()
# 8. Keep running
try:
print("Bot is running! Press Ctrl+C to stop.")
while True:
await asyncio.sleep(1)
except KeyboardInterrupt:
print("\nStopping...")
finally:
await runtime.stop()
if __name__ == "__main__":
asyncio.run(main())