Seeing that the previous tinkering brought several feed backs, I've decided to keep going, so here's a more scrutinized version of the probable implementation of NWC with X(Twitter)

This code should now do the following:

  1. Fetch a Twitter user's bio
  2. Parse the bio for a Nostr public key (npub)
  3. Extract the Nostr public key from the npub
  4. Create and sign a Nostr event with a custom message
  5. Publish the event to the Nostr network
Please replace the hardcoded values with your own credentials and values.
import os import requests import re from alby_sdk import Alby from nostr import NWC, Relay from nostr.event import Event from datetime import datetime # Set up Alby SDK alby = Alby() # Set up NWC nwc_public_key = "YOUR_NWC_PUBLIC_KEY" nwc_private_key = "YOUR_NWC_PRIVATE_KEY" nwc = NWC(relay_url="wss://relay.damus.io", public_key=nwc_public_key, private_key=nwc_private_key) # Twitter API credentials twitter_api_key = "YOUR_API_KEY" twitter_api_secret = "YOUR_API_SECRET" # Function to parse Twitter bio for Nostr public key (npub) def parse_twitter_bio(twitter_bio): npub_pattern = r"npub[0-9A-Za-z]{40,}" npub_match = re.search(npub_pattern, twitter_bio) if npub_match: return npub_match.group() return None # Function to extract Nostr public key from npub def extract_nostr_public_key(npub): try: return alby.parse_npub(npub) except Exception as e: print(f"Error parsing npub: {e}") return None # Function to create and sign a Nostr event def create_and_sign_event(event_data): try: event = Event(**event_data) signed_event = nwc.sign_event(event) return signed_event except Exception as e: print(f"Error creating and signing event: {e}") return None # Main program logic def main(): # Fetch Twitter user's bio twitter_username = "twitter_username" twitter_user = requests.get(f"(link unavailable)", headers={"Authorization": f"Bearer {twitter_api_key}"}) twitter_bio = twitter_user.json()["data"]["description"] # Parse Twitter bio for Nostr public key (npub) npub = parse_twitter_bio(twitter_bio) # If npub is found, extract Nostr public key if npub: nostr_public_key = extract_nostr_public_key(npub) # If Nostr public key is extracted, create and sign a Nostr event if nostr_public_key: event_data = { "id": "YOUR_EVENT_ID", "pubkey": nostr_public_key, "created_at": int(datetime.now().timestamp()), "kind": 1, "tags": [], "content": f"I just zapped {npub}'s Twitter ({twitter_username}) account" } signed_event = create_and_sign_event(event_data) # If event is signed, publish it to the Nostr network if signed_event: nwc.publish(signed_event) if __name__ == "__main__": main()