This tutorial will explain and show you how to submit a new comment to the Hive blockchain using the commit class found within the beem library.
Intro
The beem library has a built-in function to transmit transactions to the blockchain. We are using the transactionbuilder in the the library. It should be noted that comments and new post are both treated as the comment operation with the only difference being that a comment/reply has an additional parameter containing the parent_author/parent_permlink that corresponds to the original post/comment.
author - The account that you are posting from
title - The title of the reply (usually empty for replies)
body - The body of the reply
permlink - A unique identifier, scoped to author
parent_author - Author being replied to (for replies) or empty string (for posts)
parent_permlink - Permlink being replied to (for replies) or category (for posts)
We will only be using the above parameters as these are the only ones required to create a basic post. If you want to explore the other parameters further you can find more information HERE.
A comment made on a post is defined as a root comment. You can also comment on someone else’s (or your own) comment, in which case the parent parameters would be that of the comment, and not the original post.
Because this tutorial alters the blockchain we have to connect to the testnet. We also require the private posting key of the contributing author in order to commit the post which is why we’re using a testnet node.
2. Variable input and format
The first three variables are captured via a simple string input.
We also use a random generator to create a new permlink for the post being created.
#random generator to create post permlink
permlink=''.join(random.choices(string.digits,k=10))
The random generator is limited to 10 characters in this case but the permlink can be up to 256 bytes. The permlink is unique to the author only which means that multiple authors can have the same permlink for the their reply.
3. Initialize class
We initialize the beem class by connecting to the specific testnet node. We also require the private posting key of the contributing author in order to commit the post which is also specified during this operation.
# node_url = 'https://testnet.openhive.network' # Public Testnet
node_url='http://127.0.0.1:8090'# Local Testnet
#connect node and private posting key
client=Hive(node_url,keys=[wif])
4. Post submission and result
The last step is to transmit the post through to the blockchain. All the defined parameters are signed and broadcasted. We also securely prompt for the posting key right before signing.
# client = Hive('https://testnet.openhive.network') # Public Testnet
client=Hive('http://127.0.0.1:8090')# Local Testnet
tx=TransactionBuilder(blockchain_instance=client)tx.appendOps(Comment(**{"parent_author":'',"parent_permlink":taglist[0],# we use the first tag as the category
"author":author,"permlink":permlink,"title":title,"body":body,"json_metadata":json.dumps({"tags":taglist})}))wif_posting_key=getpass.getpass('Posting Key: ')tx.appendWif(wif_posting_key)signed_tx=tx.sign()broadcast_tx=tx.broadcast(trx_id=True)print("Post created successfully: "+str(broadcast_tx))
A simple confirmation is printed on the screen if the post is committed successfully.
Submit Comment Reply
How to post a simple comment to Hive.
Full, runnable src of Submit Comment Reply can be downloaded as part of: tutorials/python (or download just this tutorial: devportal-master-tutorials-python-11_submit_comment_reply.zip).
This tutorial will explain and show you how to submit a new comment to the
Hive
blockchain using thecommit
class found within the beem library.Intro
The beem library has a built-in function to transmit transactions to the blockchain. We are using the
transactionbuilder
in the the library. It should be noted that comments and new post are both treated as thecomment
operation with the only difference being that a comment/reply has an additional parameter containing theparent_author
/parent_permlink
that corresponds to the original post/comment.We will only be using the above parameters as these are the only ones required to create a basic post. If you want to explore the other parameters further you can find more information HERE.
A comment made on a post is defined as a
root comment
. You can also comment on someone else’s (or your own) comment, in which case theparent
parameters would be that of the comment, and not the original post.Also see:
Steps
1. App setup
In this tutorial we use the following packages:
random
andstring
- used to create a random string used for thepermlink
getpass
- capture wif without showing it on the screenbeem
- hive library and interaction with BlockchainWe import the libraries and connect to the
testnet
.Because this tutorial alters the blockchain we have to connect to the testnet. We also require the
private posting key
of the contributing author in order to commit the post which is why we’re using atestnet
node.2. Variable input and format
The first three variables are captured via a simple string input.
We also use a random generator to create a new
permlink
for the post being created.The random generator is limited to 10 characters in this case but the permlink can be up to 256 bytes. The permlink is unique to the author only which means that multiple authors can have the same permlink for the their reply.
3. Initialize class
We initialize the beem class by connecting to the specific
testnet
node. We also require theprivate posting key
of the contributing author in order to commit the post which is also specified during this operation.4. Post submission and result
The last step is to transmit the post through to the blockchain. All the defined parameters are signed and broadcasted. We also securely prompt for the posting key right before signing.
A simple confirmation is printed on the screen if the post is committed successfully.
You can also check on your local testnet using database_api.find_comments for the post.
Final code:
To Run the tutorial
You can launch a local testnet, with port 8090 mapped locally to the docker container:
For details on running a local testnet, see: Setting Up a Testnet
git clone https://gitlab.syncad.com/hive/devportal.git
cd devportal/tutorials/python/11_submit_comment_reply
pip install -r requirements.txt
python index.py