Part 8: How To Create Your Own Upvote Bot Using Steem-Python

in #utopian-io6 years ago (edited)

steem-python.png

This tutorial is part of a series where we explain different aspects of programming with steem-python. Links to the other tutorials can be found in the curriculum section below. Today we will learn how to stream the blockchain for transfers directed towards us and then perform actions based on its content!


What will I learn

  • How to filter the blockchain for transfers
  • What a transfer looks like
  • How to handle the transfer

Requirements

  • Python3.6
  • steem-python

Difficulty

  • Basic

Tutorial

Streaming the blockchain for transfers

We have gone over this in previous tutorials, so you should know how to do this by heart! We simply set the operation to filter the blockchain by to transfers, like so

from steem import Steem
from steem.blockchain import Blockchain

steem = Steem()
blockchain = Blockchain()
stream = blockchain.stream(filter_by=["transfer"])

for transfer in stream:
    # DO SOMETHING

What does a transfer on the blockchain look like?

Before we can implement our program that replies to incoming transfers we need to know what a transfer actually looks like. Printing a transfer out gives the following example

{
  "from": "ginabot",
  "to": "elnazry",
  "amount": "0.001 STEEM",
  "memo": "Your account has been validated!",
  "_id": "b2a57b62afa8b1a86bb7d42389e774cdd6949a75",
  "type": "transfer",
  "timestamp": "2018-01-17 14:59:39",
  "block_num": 19059815,
  "trx_id": "1af0c84531381911be9a6570455e881bcfa3b2fd"
}

As you can see there's a lot of useful information, but we are only interested in who sent the transfer, to whom the transfer was sent, what the amount is and what the memo is.

Checking the transfer

The first step when streaming transfers on the blockchain is to make sure the transfer is actually meant for us. To do this we can simply check every "to" key of each transfer and see if it matches our username, then print some information about the transfer, like so

for transfer in stream:
    if transfer["to"] == username:
        print(f"{transfer['from']} {transfer['amount']} {transfer['memo']}")

After testing it with another account it printed the following

$ python memo.py 
amosbastian 0.001 SBD hi, this is a test!

and checking it on Steemit also shows it works

transfer_test.png

Upvoting the post in the memo

Since we are creating an upvote bot we should upvote whatever the URL in the memo is (if it's a valid Steemit URL of course). So how can we do this? First we need to extract the URL from the memo and make sure it's a valid Steemit URL. After doing this we need to upvote the given post. Seems pretty easy, right? We can use the following code to accomplish this

username = "amosbastian"

for transfer in stream:
    if transfer["to"] == username:
        url, permlink = transfer["memo"].split("@")
        if "https://steemit.com/" in url:
            steem.vote(f"@{permlink}", 100)

and indeed, after testing it with another account (made available to me via @juliank), it works as expected!

vote_memo.png

Currently we don't check the amount of the transfer (and our URL checking could fail), but that seemed beyong the scope of this tutorial. If you want to implement that yourself, then go ahead, but otherwise congratulations, you have created your very first upvote bot!

Curriculum


The code for this tutorial can be found on GitHub!

This tutorial was written by @amosbastian in conjunction with @juliank.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hi bro, would you mind visiting my last post? I am looking for coders to work with. Is Juliank a coder too?

Hi, just checked it out, looks very cool! We are both coders, we actually met at university studying computer science. What can we help you with?

Hey @steempytutorials I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.034
BTC 64513.75
ETH 3146.11
USDT 1.00
SBD 3.95