Free: auto-rotating relay pool for Nostr bots (handles flaky relays)
```python
import json, time, random
import websocket, ssl
RELAYS = [
"wss://nos.lol",
"wss://relay.primal.net",
"wss://relay.damus.io",
"wss://nostr.wine",
]
class RelayPool:
def __init__(self, relays=RELAYS, min_working=2):
self.relays = relays
self.health = {r: True for r in relays}
self.min_working = min_working
def publish(self, event_dict):
sent, failed = 0, []
for relay in self.relays:
if not self.health[relay]:
continue
try:
ws = websocket.create_connection(relay, timeout=8,
sslopt={"cert_reqs": ssl.CERT_NONE})
ws.send(json.dumps(["EVENT", event_dict]))
ws.recv()
ws.close()
sent += 1
except:
self.health[relay] = False
failed.append(relay)
# Retry failed relays after 5 min
if failed:
time.sleep(300)
for r in failed:
self.health[r] = True
return sent
pool = RelayPool()
# pool.publish(your_event)
```
Building a Nostr bot and want a full resilient publisher (with dedup, retry logic, health scoring)? $9. DM or 👉
https://getting-overnight-basement-drain.trycloudflare.com
#nostr #python #bot #developer