Assalamualaikum my fellows I hope you will be fine by the grace of Allah. Today I am going to participate in the steemit learning challenge season 22 week 4 by @alejos7ven under the umbrella of steemit team. It is about Broadcasting operations part 1: Transferring, and posting. Let us start exploring this week's teaching course.
Create a bot that detects when you vote for someone else's publication, when detecting your vote creates an automatic comment on that publication you voted for (the app that created your comment should be called your username and version 1.0, example app:'alejos7ven/1.0')
I saw many users and the trails when their vote is broadcasted then their comment is also published under that post. It is really an ideal work and saves time and effort to comment manually. And this task is about to create such type of bot where I will do my vote and the bot will automatically publish the comment from my account. For this I need to explore the Steem JS library to detect the vote using streamOperations
and then to comment based on that data under the post.
This is the complete code of the bot which will detect my vote using streamOperations
and then it will carry out the transaction of the comment from my account by using my posting key as without the posting key we cannot perform comment, vote, post and other similar transactions. I have removed my posting key from the display.
This bot code is split into different functions and processes that are designed to do specific things to ensure that detection and commenting on posts when I vote go smoothly. Here is a breakdown of what the code does:
Initialization and Configuration: The code starts by importing the
steem
library and defining constantsUSERNAME
for your Steem account username andPOSTING_KEY
for private posting key. These variables are needed to identify the bot account and authorize actions such as making comments.The
createComment
function: It manages the process of creating a comment on a post. It invokes as follows: generating a uniquecommentPermlink
by attaching the current timestamp to prevent duplicity of link. The body of the comment extends appreciation to the author of that post for hosting his content on the network. It states how bot had voted for such a post. The metadata (json_metadata
) contains tags relating to the posted content which are #bot and #autocomment. And theapp
field sets the username for the bot and also its version which can be seen in the metadata while exploring the transaction details. Now thesteem.broadcast.comment
method is called. Once this is successful it logs the URL of the comment otherwise logging is done if an error has occurred.Streaming Blockchain Operations: Using
steem.api.streamOperations
, the bot listens to real-time blockchain operations. Thus, there is a continuous receiving of all operations currently happening on the Steem blockchain. This is the callback function, which first checks if it is a "vote" type of operation and if the voter is the bot username. If that's the case and the weight of the vote is positive, it logs this vote and proceeds to callcreateComment
, posting an automated comment on the voted post:.Comment Automation: This process is achieved by linking the real time voting detection with the
createComment
function. If the bot detects that my username has voted then a comment will be posted on the post automatically without my intervention.Logging and Debugging: Logging is added throughout the code to indicate what the bot is working such as detecting a vote or successfully creating a comment. Errors are also logged to help with debugging if something goes wrong.
So it is a bot that listens to the blockchain for voting activity. Once I vote on a post it automatically creates a comment on that post using my Steem account. It increases engagement and provides feedback to the authors.
After running this bot I got the satisfactory results as you can see in the above screenshot. After running it I performed voting on 2 posts and the bot successfully detected the voting transaction using the streamOperations
and after this it suddenly done comment on that post using my private posting posting key and the ready made content with the specified tags and metadata. After broadcasting the comment it has provided the parent link as well.
Before Vote | After voting the comment was also made automatically |
I checked this comment transaction for my account from SteemWolrd.org and I got this result and the app information like my username and version (akmalshakir/1.0) is also visible. Moreover the tags and the title of my comment is also visible which I added in the code.
Before Vote | After voting the comment was also made automatically |
Similarly I checked this comment transaction for my account from SteemWolrd.org and I got this result and the app information like my username and version (akmalshakir/1.0) is also visible. Moreover the tags and the title of my comment is also visible which I added in the code.
This bot is perfectly working fine as per the directions in the question from the teacher. And I am enjoying doing it.
Use SteemWorld, Ecosynthesizer, or any other explorer to show the transactions you have created, leave link just like me.
For this purpose I will choose Ecosynthesizer because with the help of the Steemworld I have already shared some screenshots above about the transactions. So let us move towards the Ecosynthesizer to share the transactions about the
All the transactions which I performed while running the bot to detect the vpote and pubish comment are in the above screenshot. It can be noticed that firt I broadcasted the vote for the account eight888 and then for the kafio and my bot similarly broadcasted the comment first for the user eight888 and then for kafio.
This is the transaction detail for my vote to user eight888.
https://ecosynthesizer.com/steem/trx/29650de983738f7799df72db8886e54a5f032faf
This is the transaction detail for my automated comment to user eight888.
https://ecosynthesizer.com/steem/trx/be8721e4b38468e984a568009293135b76150382
This is the transaction detail for my vote to user kafio.
https://ecosynthesizer.com/steem/trx/ed48d2a9c3b49babf4ff79291f5384cbc4a4e218
This is the transaction detail for my automated comment to user kafio right after my vote.
This is the detail of my comments and vote transactions and in the details the username and app version is visible as it was in the steemworld.org.
Transfer 0.001 STEEM to @eight888 using Steem JS, add in the memo the title of this lesson.
To transfer with the code while running it to the specified code I will use the Steem JS library and then transfer
function.
This code shows how to transfer 0.001 STEEM to the account @eight888
using the Steem JS library with the memo field set to the title of the lesson. Here is a detailed explanation of how the code works:
Initialization and Configuration
The script imports the Steem JS library. This is the library used for interacting with the Steem blockchain. Two constants are defined in the script USERNAME
and ACTIVE_KEY
. USERNAME
represents the Steem account from which the transfer is initiated while ACTIVE_KEY
is the private key necessary to authorize the transfer operation.
Creating the Transfer Function
The transferSteem
function is where the transfer operation is handled. It accepts three parameters:
recipient
: The username of the account to receive the transfer in this case the username is@eight888
.amount
: The amount of STEEM to transfer which is set to 0.001.memo
: A string that will appear in the transaction memo field. Here it is set to the lesson title: "Domain Steem with JavaScript: Lesson #4 - Broadcasting operations part 1: Transferring, and posting."
The steem.broadcast.transfer
function is called which broadcasts the transfer operation to the blockchain. It is called with sender active key, sender username, recipient username, the amount written as three decimal digits followed by the unit "STEEM" and memo text.
Handling Success and Errors
The function calls a callback for the result of the broadcast. If the transfer is successful it logs a message along with the link to the transaction history for verification. If there is an error it logs the error message to the console to make debugging quite easy.
Transfer Execution
At the end the program calls the transferSteem
function. And it passes the recipient as @eight888
, the amount as 0.001 STEEM, and the memo with the title of the lesson. The transfer operation is executed on the blockchain.
This is the live outpout of this program which is performing transaction of the transfer of the STEEM to the user.
This script sends STEEM from one account to another with a meaningful memo attached. It uses the steem.broadcast.transfer
method to securely and effectively interact with the blockchain. The memo field containing the lesson title is a record of the purpose of the transaction especially useful for educational or tracking purposes.
https://ecosynthesizer.com/steem/trx/cba048c0212764c30e0b2a119c753e31e23db837
This is the detail of the transaction of this transfer. The amount of the transfer is visible in the details with the memo of this transaction. The block details and the operations details is also in this transaction can be seen.