-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_msg.py
More file actions
80 lines (62 loc) · 2.09 KB
/
send_msg.py
File metadata and controls
80 lines (62 loc) · 2.09 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
# send_msg.py
#
# SPDX-FileCopyrightText: Copyright 2021 Fabbio Protopapa
#
# SPDX-License-Identifier: MIT
#
# Send a message to the IOTA tangle
# For e.g.
# https://explorer.iota.org/mainnet/message/497c1b68e5480d07819bbd9c989c8d245fa748667a89fdf7dac884741f493326
#
import iota_client
import os
import pprint
# Config
env_node_address = 'HORNET_NODE_ADDRESS'
# Print Message data
def show_message(message):
print('''
Message send!
Please check message on https://explorer.iota.org/mainnet/message/{}
Message details:
'''.format(message['message_id']))
pprint.pprint(message)
# Connect to node and send message
def main():
import argparse
parser = argparse.ArgumentParser(description='Send message to IOTA tangle.')
parser.add_argument('--msg', dest='msg',
default='Projekt badawczy - IOTA w IoT',
help='Message to send')
parser.add_argument('--node_info', dest='node_info',
default=False,
help='Print node information')
parser.add_argument('--indx', dest='indx',
default='Hello',
help='Set indexation for message')
args = parser.parse_args()
message_str = args.msg
node_info = args.node_info
indexation = args.indx
NODE_URL = os.getenv(env_node_address)
if not NODE_URL:
raise Exception("Please define environment variable with node URL.")
try:
# Initialize client
client = iota_client.Client(
nodes_name_password=[[NODE_URL]], node_sync_disabled=True)
except:
raise Exception('Node not found.')
# Get node information
if node_info:
print('Node Information:')
pprint.pprint(client.get_info())
print('----------------------------------------------------------')
# Create message
message_byte = message_str.encode("utf8")
message_id_indexation = client.message(
index=indexation, data=message_byte)
# Print send message
show_message(message_id_indexation)
if __name__ == "__main__":
main()