#!/usr/bin/env python3
import json, asyncio, sys
from nostr_sdk import Keys, Client, NostrSigner, Kind, EventBuilder, Tag, RelayUrl

RELAY_URL = "ws://143.198.27.163:2929"
KEYS_FILE = "/Users/apayne/.timmy/nostr/agent_keys.json"
GROUP_ID = "b082d1"
agent = sys.argv[1]

with open(KEYS_FILE) as f:
    all_keys = json.load(f)

async def main():
    keys = Keys.parse(all_keys[agent]["hex_sec"])
    signer = NostrSigner.keys(keys)
    client = Client(signer)
    await client.add_relay(RelayUrl.parse(RELAY_URL))
    await client.connect()
    await asyncio.sleep(1)
    builder = EventBuilder(Kind(9021), "request to join").tags([Tag.parse(["h", GROUP_ID])])
    result = await client.send_event_builder(builder)
    print(f"[{agent}] id={result.id.to_hex()} success={list(result.success)} failed={dict(result.failed)}")
    await client.disconnect()

asyncio.run(main())
