Hive Developer logo

Hive Developer Portal

PY: Submit Comment Reply

How to submit a comment on a post to the Hive blockchain.

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 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.

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.

Also see:

Steps

  1. App setup - Library install and import. Connection to Hive node
  2. Variable input and format - Input and creation of varialbes
  3. Initialize class - Initialize the beem class with the relevant node and private key
  4. Post submission and result - Committing of transaction to the blockchain

1. App setup

In this tutorial we use the following packages:

We import the libraries and connect to the testnet.

import random
import string
import getpass
from beem import Hive
from beem.transactionbuilder import TransactionBuilder
from beembase.operations import Comment

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.

#capture variables
parent_author = input('Parent author: ')
parent_permlink = input('Parent permlink: ')
author = input('Username: ')
title = input('Post Title: ')
body = input('Post Body: ')

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.

#connect node and private posting key
client = Hive('http://127.0.0.1:8090', 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('http://127.0.0.1:8090')
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.

You can also check on your local testnet using database_api.find_comments for the post.

To Run the tutorial

Before running this tutorial, launch your local testnet, with port 8090 mapped locally to the docker container:

docker run -d -p 8090:8090 inertia/tintoy:latest

For details on running a local testnet, see: Setting Up a Testnet

  1. review dev requirements
  2. git clone https://gitlab.syncad.com/hive/devportal.git
  3. cd devportal/tutorials/python/11_submit_comment_reply
  4. pip install -r requirements.txt
  5. python index.py
  6. After a few moments, you should see a prompt for input in terminal screen.