mirror of
https://github.com/aljazceru/python-nostr.git
synced 2025-12-19 15:24:19 +01:00
clean
This commit is contained in:
57
main.py
57
main.py
@@ -1,57 +1,80 @@
|
|||||||
|
|
||||||
from nostr.client.client import NostrClient
|
from nostr.client.client import NostrClient
|
||||||
from nostr.event import Event
|
from nostr.event import Event
|
||||||
from nostr.key import PublicKey
|
from nostr.key import PublicKey
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import threading
|
||||||
|
|
||||||
|
|
||||||
async def dm():
|
async def dm():
|
||||||
print("This is an example NIP-04 DM flow")
|
print("This is an example NIP-04 DM flow")
|
||||||
pk = input("Enter your privatekey to post from (enter nothing for a random one): ")
|
pk = input("Enter your privatekey to post from (enter nothing for a random one): ")
|
||||||
|
|
||||||
def callback(event: Event, decrypted_content):
|
def callback(event: Event, decrypted_content):
|
||||||
print(f"From {event.public_key[:3]}..{event.public_key[-3:]}: {decrypted_content}")
|
"""
|
||||||
|
Callback to trigger when a DM is received.
|
||||||
client = NostrClient(
|
"""
|
||||||
privatekey_hex=pk
|
print(
|
||||||
|
f"From {event.public_key[:3]}..{event.public_key[-3:]}: {decrypted_content}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
client = NostrClient(privatekey_hex=pk)
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
|
t = threading.Thread(
|
||||||
import threading
|
target=client.get_dm,
|
||||||
t = threading.Thread(target=client.get_dm, args=(client.public_key,callback,))
|
args=(
|
||||||
|
client.public_key,
|
||||||
|
callback,
|
||||||
|
),
|
||||||
|
)
|
||||||
t.start()
|
t.start()
|
||||||
|
|
||||||
to_pubk_hex = input("Enter other pubkey to post to (enter nothing to DM yourself): ") or client.public_key.hex()
|
to_pubk_hex = (
|
||||||
|
input("Enter other pubkey to post to (enter nothing to DM yourself): ")
|
||||||
|
or client.public_key.hex()
|
||||||
|
)
|
||||||
print(f"Subscribing to DMs to {to_pubk_hex}")
|
print(f"Subscribing to DMs to {to_pubk_hex}")
|
||||||
while True:
|
while True:
|
||||||
msg = input("\nEnter message: ")
|
msg = input("\nEnter message: ")
|
||||||
client.dm(msg, PublicKey(bytes.fromhex(to_pubk_hex)))
|
client.dm(msg, PublicKey(bytes.fromhex(to_pubk_hex)))
|
||||||
|
|
||||||
|
|
||||||
async def post():
|
async def post():
|
||||||
print("This posts and reads a nostr note")
|
print("This posts and reads a nostr note")
|
||||||
pk = input("Enter your privatekey to post from (enter nothing for a random one): ")
|
pk = input("Enter your privatekey to post from (enter nothing for a random one): ")
|
||||||
|
|
||||||
def callback(event: Event):
|
def callback(event: Event):
|
||||||
|
"""
|
||||||
|
Callback to trigger when post appers.
|
||||||
|
"""
|
||||||
print(f"From {event.public_key[:3]}..{event.public_key[-3:]}: {event.content}")
|
print(f"From {event.public_key[:3]}..{event.public_key[-3:]}: {event.content}")
|
||||||
|
|
||||||
sender_client = NostrClient(
|
sender_client = NostrClient(privatekey_hex=pk)
|
||||||
privatekey_hex=pk
|
|
||||||
)
|
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
to_pubk_hex = input("Enter other pubkey (enter nothing to read your own posts): ") or sender_client.public_key.hex()
|
to_pubk_hex = (
|
||||||
|
input("Enter other pubkey (enter nothing to read your own posts): ")
|
||||||
|
or sender_client.public_key.hex()
|
||||||
|
)
|
||||||
print(f"Subscribing to posts by {to_pubk_hex}")
|
print(f"Subscribing to posts by {to_pubk_hex}")
|
||||||
to_pubk = PublicKey(bytes.fromhex(to_pubk_hex))
|
to_pubk = PublicKey(bytes.fromhex(to_pubk_hex))
|
||||||
|
|
||||||
import threading
|
t = threading.Thread(
|
||||||
t = threading.Thread(target=sender_client.get_post, args=(to_pubk, callback,))
|
target=sender_client.get_post,
|
||||||
|
args=(
|
||||||
|
to_pubk,
|
||||||
|
callback,
|
||||||
|
),
|
||||||
|
)
|
||||||
t.start()
|
t.start()
|
||||||
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
msg = input("\nEnter post: ")
|
msg = input("\nEnter post: ")
|
||||||
sender_client.post(msg)
|
sender_client.post(msg)
|
||||||
|
|
||||||
asyncio.run(post())
|
|
||||||
|
|
||||||
|
# write a DM and receive DMs
|
||||||
|
asyncio.run(dm())
|
||||||
|
|
||||||
|
# make a post and subscribe to posts
|
||||||
|
# asyncio.run(post())
|
||||||
|
|||||||
Reference in New Issue
Block a user