forked from hiero-ledger/hiero-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_topic_message.py
More file actions
102 lines (86 loc) · 2.72 KB
/
query_topic_message.py
File metadata and controls
102 lines (86 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""
uv run examples/query_topic_message.py
python examples/query_topic_message.py
"""
import os
import time
import sys
from datetime import datetime, timezone
from dotenv import load_dotenv
from hiero_sdk_python import (
Network,
Client,
AccountId,
PrivateKey,
TopicCreateTransaction,
TopicMessageQuery
)
load_dotenv()
def setup_client():
"""Initialize and set up the client with operator account"""
print("Connecting to Hedera testnet...")
client = Client(Network(os.getenv('NETWORK')))
try:
operator_id = AccountId.from_string(os.getenv('OPERATOR_ID'))
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
client.set_operator(operator_id, operator_key)
return client, operator_id, operator_key
except (TypeError, ValueError):
print("❌ Error: Creating client, Please check your .env file")
sys.exit(1)
def create_topic(client, operator_key):
"""Create a new topic"""
print("\nSTEP 1: Creating a Topic...")
try:
topic_tx = (
TopicCreateTransaction(
memo="Python SDK created topic",
admin_key=operator_key.public_key()
)
.freeze_with(client)
.sign(operator_key)
)
topic_receipt = topic_tx.execute(client)
topic_id = topic_receipt.topic_id
print(f"✅ Success! Created topic: {topic_id}")
return topic_id
except Exception as e:
print(f"❌ Error: Creating topic: {e}")
sys.exit(1)
def query_topic_messages():
"""
A full example that creates a topic and perform query topic messages.
"""
# Config Client
client, _, operator_key = setup_client()
# Create Topic
topic_id = create_topic(client, operator_key)
# Query Topic Messages
print("\nSTEP 2: Query Topic Messages...")
def on_message_handler(topic_message):
print(f"Received topic message: {topic_message}")
def on_error_handler(e):
print(f"Subscription error: {e}")
query = TopicMessageQuery(
topic_id=topic_id,
start_time=datetime.now(timezone.utc),
limit=0,
chunking_enabled=True
)
handle = query.subscribe(
client,
on_message=on_message_handler,
on_error=on_error_handler
)
print("Subscription started. Will auto-cancel after 10 seconds or on Ctrl+C...")
try:
startTime = time.time();
while time.time() - startTime < 10:
time.sleep(1);
except KeyboardInterrupt:
print("✋ Ctrl+C detected. Cancelling subscription...")
finally:
handle.cancel()
print("✅ Subscription cancelled. Exiting.")
if __name__ == "__main__":
query_topic_messages()