Update app.py
Browse files
app.py
CHANGED
@@ -2,8 +2,65 @@ from nostr_sdk import Client, NostrSigner, Keys, Event, UnsignedEvent, Filter, \
|
|
2 |
HandleNotification, Timestamp, nip04_decrypt, UnwrappedGift, init_logger, LogLevel, Kind, KindEnum
|
3 |
import time
|
4 |
|
5 |
-
keys = Keys.parse("
|
6 |
|
7 |
sk = keys.secret_key()
|
8 |
pk = keys.public_key()
|
9 |
-
print(f"Bot public key: {pk.to_bech32()}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
HandleNotification, Timestamp, nip04_decrypt, UnwrappedGift, init_logger, LogLevel, Kind, KindEnum
|
3 |
import time
|
4 |
|
5 |
+
keys = Keys.parse("nsec1zrl5ahr9hy43vwns0r423c0crtv45myjgelf6gf2kl8ae90x9nkqex90cg")
|
6 |
|
7 |
sk = keys.secret_key()
|
8 |
pk = keys.public_key()
|
9 |
+
print(f"Bot public key: {pk.to_bech32()}")
|
10 |
+
|
11 |
+
signer = NostrSigner.keys(keys)
|
12 |
+
client = Client(signer)
|
13 |
+
|
14 |
+
client.add_relay("wss://relay.damus.io")
|
15 |
+
client.add_relay("wss://relay.notoshi.win")
|
16 |
+
client.add_relay("wss://relay.siamstr.com")
|
17 |
+
client.connect()
|
18 |
+
|
19 |
+
now = Timestamp.now()
|
20 |
+
|
21 |
+
nip04_filter = Filter().pubkey(pk).kind(Kind.from_enum(KindEnum.ENCRYPTED_DIRECT_MESSAGE())).since(now)
|
22 |
+
nip59_filter = Filter().pubkey(pk).kind(Kind.from_enum(KindEnum.GIFT_WRAP())).since(
|
23 |
+
Timestamp.from_secs(now.as_secs() - 60 * 60 * 24 * 7)) # NIP59 have a tweaked timestamp (in the past)
|
24 |
+
client.subscribe([nip04_filter, nip59_filter], None)
|
25 |
+
|
26 |
+
|
27 |
+
class NotificationHandler(HandleNotification):
|
28 |
+
def handle(self, relay_url, subscription_id, event: Event):
|
29 |
+
print(f"Received new event from {relay_url}: {event.as_json()}")
|
30 |
+
if event.kind().match_enum(KindEnum.ENCRYPTED_DIRECT_MESSAGE()):
|
31 |
+
print("Decrypting NIP04 event")
|
32 |
+
try:
|
33 |
+
msg = nip04_decrypt(sk, event.author(), event.content())
|
34 |
+
print(f"Received new msg: {msg}")
|
35 |
+
client.send_direct_msg(event.author(), f"Echo: {msg}", event.id())
|
36 |
+
except Exception as e:
|
37 |
+
print(f"Error during content NIP04 decryption: {e}")
|
38 |
+
elif event.kind().match_enum(KindEnum.GIFT_WRAP()):
|
39 |
+
print("Decrypting NIP59 event")
|
40 |
+
try:
|
41 |
+
# Extract rumor
|
42 |
+
unwrapped_gift = UnwrappedGift.from_gift_wrap(keys, event)
|
43 |
+
sender = unwrapped_gift.sender()
|
44 |
+
rumor: UnsignedEvent = unwrapped_gift.rumor()
|
45 |
+
|
46 |
+
# Check timestamp of rumor
|
47 |
+
if rumor.created_at().as_secs() >= now.as_secs():
|
48 |
+
if rumor.kind().match_enum(KindEnum.SEALED_DIRECT()):
|
49 |
+
msg = rumor.content()
|
50 |
+
print(f"Received new msg [sealed]: {msg}")
|
51 |
+
client.send_sealed_msg(sender, f"Echo: {msg}", None)
|
52 |
+
else:
|
53 |
+
print(f"{rumor.as_json()}")
|
54 |
+
except Exception as e:
|
55 |
+
print(f"Error during content NIP59 decryption: {e}")
|
56 |
+
|
57 |
+
def handle_msg(self, relay_url, msg):
|
58 |
+
None
|
59 |
+
|
60 |
+
|
61 |
+
abortable = client.handle_notifications(NotificationHandler())
|
62 |
+
# Optionally, to abort handle notifications look, call abortable.abort()
|
63 |
+
|
64 |
+
while True:
|
65 |
+
time.sleep(5.0)
|
66 |
+
# abortable.abort()
|