diff --git a/fetch_metadata.py b/fetch_metadata.py new file mode 100644 index 0000000..0988fa3 --- /dev/null +++ b/fetch_metadata.py @@ -0,0 +1,52 @@ +import json +import asyncio +import websockets + +your_relay_url = "wss://nos.lol" +sender_pubkey = "16f1a0100d4cfffbcc4230e8e0e4290cc5849c1adc64d6653fda07c031b1074b" + +async def listen_to_relay(your_pubkey, your_relay_url): + uri = f"{your_relay_url}/ws" + async with websockets.connect(uri) as websocket: + # Subscribe to all kind 1 events + subscription_id = "kind1" # Shortened subscription ID + await websocket.send(json.dumps(["REQ", subscription_id, {"kinds": [1]}])) + + while True: + response = await websocket.recv() + await handle_event(response, your_pubkey) + +async def handle_event(event_json, your_pubkey): + event = json.loads(event_json) + if event[0] == "EVENT" and event[2]["kind"] == 9735: # Zap receipt event + await handle_metadata_receipt(event, your_pubkey) + +async def handle_metadata_receipt(zap_receipt_event, your_pubkey): + recipient_pubkey = None + for tag in zap_receipt_event[2]["tags"]: + if tag[0] == "p": + recipient_pubkey = tag[1] + + if recipient_pubkey == your_pubkey: + sender_pubkey = next((tag[1] for tag in zap_receipt_event[2]["tags"] if tag[0].lower() == "p"), None) + sender_metadata = await get_metadata(sender_pubkey) + print(f"Received zap receipt event metadata: {sender_metadata}") + +async def get_metadata(pubkey): + uri = f"{your_relay_url}/ws" + async with websockets.connect(uri) as websocket: + subscription_id = f"meta_{pubkey[:8]}" # Shortened subscription ID + subscription_payload = json.dumps(["REQ", subscription_id, {"kinds": [0], "authors": [pubkey]}]) + await websocket.send(subscription_payload) + print(f"Sent metadata request for pubkey: {pubkey}") + + while True: + response = await websocket.recv() + print(f"Received response: {response}") + event = json.loads(response) + if event[0] == "EVENT" and event[2]["kind"] == 0: + content = json.loads(event[2]["content"]) + return content # Return the entire metadata content + +if __name__ == "__main__": + asyncio.get_event_loop().run_until_complete(get_metadata(sender_pubkey)) diff --git a/new_zap_notification.py b/new_zap_notification.py index 82ea228..eeb3141 100644 --- a/new_zap_notification.py +++ b/new_zap_notification.py @@ -19,29 +19,36 @@ async def listen_to_relay(): while True: response = await websocket.recv() - # print(f"Received response: {response}") await handle_event(response) async def handle_event(event_json): event = json.loads(event_json) - # print(f"Received event: {event}") + #print(f"Received event: {json.dumps(event, indent=2)}") if event[0] == "EVENT" and event[2]["kind"] == 9735: # Zap receipt event await handle_zap_receipt(event) async def handle_zap_receipt(zap_receipt_event): recipient_pubkey = None + sender_pubkey = None for tag in zap_receipt_event[2]["tags"]: if tag[0] == "p": recipient_pubkey = tag[1] - # print(f"{recipient_pubkey}") if recipient_pubkey == your_pubkey: - sender_pubkey = next((value for key, value in zap_receipt_event[2]["tags"] if key.lower() == "p"), None) + # Extract the sender's pubkey from the description tag + description_tag = next((tag for tag in zap_receipt_event[2]["tags"] if tag[0] == "description"), None) + if description_tag: + description_content = json.loads(description_tag[1]) + sender_pubkey = description_content.get("pubkey") - bolt11_invoice = next((value for key, value in zap_receipt_event[2]["tags"] if key == "bolt11"), None) - if bolt11_invoice: + if sender_pubkey: + sender_metadata = await get_metadata(sender_pubkey) + sender_name = sender_metadata.get("name", sender_pubkey) + + bolt11_invoice = next((value for key, value in zap_receipt_event[2]["tags"] if key == "bolt11"), None) + if bolt11_invoice: amount_pos = bolt11_invoice.find("lnbc") + 4 # Position of the amount in the string amount_str = "" while amount_pos < len(bolt11_invoice) and (bolt11_invoice[amount_pos].isdigit() or bolt11_invoice[amount_pos] in "upm"): @@ -50,7 +57,7 @@ async def handle_zap_receipt(zap_receipt_event): amount_multiplier = amount_str[-1] amount_value = int(amount_str[:-1]) if amount_str[:-1].isdigit() else 1 - + if amount_multiplier == "u": amount_sats = amount_value * 100 elif amount_multiplier == "m": @@ -58,11 +65,27 @@ async def handle_zap_receipt(zap_receipt_event): elif amount_multiplier == "p": amount_sats = amount_value // 1000 elif amount_multiplier == "n": - amount_sats= amount_value // 10 + amount_sats = amount_value // 10 else: amount_sats = amount_value - print(f"Received a zap from {sender_pubkey} for {amount_sats} sats") # from {sender_pubkey} to {recipient_pubkey}") + print(f"{sender_name} sent you {amount_sats} sats") + +async def get_metadata(pubkey): + uri = f"{your_relay_url}/ws" + async with websockets.connect(uri) as websocket: + subscription_id = f"meta_{pubkey[:8]}" # Shortened subscription ID + subscription_payload = json.dumps(["REQ", subscription_id, {"kinds": [0], "authors": [pubkey]}]) + await websocket.send(subscription_payload) + #print(f"Sent metadata request for pubkey: {pubkey}") + + while True: + response = await websocket.recv() + #print(f"Received response: {response}") + event = json.loads(response) + if event[0] == "EVENT" and event[2]["kind"] == 0: + content = json.loads(event[2]["content"]) + return content # Return the entire metadata content if __name__ == "__main__": asyncio.get_event_loop().run_until_complete(listen_to_relay())