Steem Developer logo

Steem Developer Portal

PY: Submit Comment Reply

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

Full, runnable src of Submit Comment Reply can be downloaded as part of the PY tutorials repository.

This tutorial will explain and show you how to submit a new comment to the Steem blockchain using the commit class found within the steem-python library.

Intro

The Steem python library has a built-in function to transmit transactions to the blockchain. We are using the post method found within the commit class in the the library. It should be noted that comments and post are both treated as a commit.post operation with the only difference being that a comment/reply has an additional parameter containing the parent post/comment. There are 11 parameters within the post method:

  1. title - The title of the post. This is a required parameter but comments don’t have a title so the parameter is assigned an empty string value
  2. body - The body of the post
  3. author - The account that you are posting from
  4. permlink - A unique adentifier for the
  5. tags - Between 1 and 5 key words that defines the post
  6. reply_idendifier - Identifier of the parent post(used for comments)
  7. json_metadata - JSON meta objec that can be attached to the post
  8. comment_options - JSON options object that can be attached to the post to specify additional options like ‘max_payouts’, ‘allow_votes’, etc.
  9. community - Name of the community you are posting into
  10. beneficiaries - A list of beneficiaries for posting reward distribution.
  11. self_vote - Upvote the post as author right after posting

We will only be using the parameters titel, body, author, permlink and reply_identifier as they are all that is required for a basic comment operation. 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 elses (or your own) comment, in which case the parent parameters would be that of the comment, and not the original post.

Steps

  1. App setup - Library install and import. Connection to Steem node
  2. Variable input and format - Input and creation of varialbes
  3. Initialize steem class - Initialize the steem 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 4 packages:

We import the libraries and connect to the testnet.

import random
import string
import steembase
import steem

steembase.chains.known_chains['STEEM'] = {
    'chain_id': '79276aea5d4877d9a25892eaa01b0adf019d3e5cb12a97478df3298ccdd01673',
    'prefix': 'STX', 'steem_symbol': 'STEEM', 'sbd_symbol': 'SBD', 'vests_symbol': 'VESTS'
}

Because this tutorial alters the blockchain we have to connect to the testnet. There is a demo account available for use, cdemo with private posting key 5JEZ1EiUjFKfsKP32b15Y7jybjvHQPhnvCYZ9BW62H1LDUnMvHz. You can also create your own demo account by following the instructions on the TESTNET site.

2. Variable input and format

The variables are captured via a simple string input and allocated as seen below. The wif variable is the private posting key of the user making the comment. This key is required to commit the post to the blockchain.

#capture variables
parentAuthor = input('Parent author: ')
parentPermlink = input('Parent permlink: ')
author = input('Username: ')
wif = input('Private posting key: ')
body = input('Comment Body: ')

We join the two parent values and assign it to the reply_identifier parameter. We also use a random generator to create a new permlink for the post being created

#combining parent values to create reply identifier
reply_identifier = '/'.join([parentAuthor,parentPermlink])

#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. If the permlink value is left empty then it auto creates a permlink based on the title of the post. The permlink is unique to the author only which means that multiple authors can have the same title for thier post.

3. Initialize steem class

We initialize the steem 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 = steem.Steem(nodes=['https://testnet.steem.vc'], keys=[wif])

4. Post submission and result

The last step is to transmit the post through to the blockchain. This is done with the post method within the commit class. All the defined parameters are submitted with the function. As stated earlier in the tutorial, there are quite a few parameters for this function but for a basic comment these 5 are all that’s required.

#commit post to blockchain
client.commit.post(title='', body=body, author=author, permlink=permlink, reply_identifier=reply_identifier)

print("Comment created successfully")
print(permlink)

A simple confirmation is printed on the screen if the comment is committed successfully. We also print the permlink for the comment on screen. This is purely for convenience to make it easier to retrieve the permlink if a new author or the same author would like to another comment on the one just made.

You can also check on the testportal for the comment or for a post to comment on. Alternatively you can create your own post to comment on following the 10_submit_post tutorial.

To Run the tutorial

  1. review dev requirements
  2. clone this repo
  3. cd tutorials/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.