Cast a vote using beem | Steem Discord Bot Python #11

in Steem Devlast year

Welcome back, developers.

Greetings to all Steem enthusiasts! In today's lecture, we will learn how to install the beem library for interacting with the Steem blockchain, as well as how to cast a vote on a post.

lecture11.png

Summary

We established the necessary environment for developing a Python-based Discord bot during our first lecture. In our second lecture we create a discord bot token, configure the bot and generate the invite link. We used the invite link and added the bot to our server. In our third lecture, we will bring the Community-Bot online in our local environment.

In the 4th lecture, we successfully developed our initial bot command, !info. We also learned how to create functions and retrieve data using the requests library in 5th lecture. We implemented the !info command in 6th lecture which is used fetch the information of any steem user.

In the 7th lecture, we learned how to send an embedded message with columns, images, and a profile image. In lecture 8th we learn how to generate a report for the community. In lecture 9th we learned how to develop a report and create a text file, then attach it to a Discord bot. In 10th lecture we learned how to get a single-author report for a community. In today's lecture, we will learn how to install the beem library to interact with the Steem blockchain and how to case vote to a post.

Procedure
  • First of all, we need to install beem. Beem is a Python library for interacting with the Steem blockchain. Type pip install beem in your PyCharm terminal and hit enter. Make sure you are using Python 3.8.

image.png

  • Now we need to create an instance for the Steem. We will create an instance in the utils.py. So that we can access it anywhere in our project easily. Import from beem import Steem first and then add the private posting key and steem_vote_instance in Utils.
# private posting key of voter account
        self.voter_username = 'faisalamin'

        # private posting key of voter account
        self.voter_private_posting_key = ''

        # Nodes for the steem instance
        self.steem_nodes = "https://api.steemit.com"

        # steem vote instances
        self.steem_vote_instance = Steem(node=self.steem_nodes, keys=self.voter_private_posting_key)

image.png

  • Now add a command !vote in our command list. We will use this command to cast a vote with the percentage given by the user to the bot.
 # bot commands
        self.commands = ['!info', '!report', '!vote']
  • Now implement the command in main.py. Our command will have 3 parameters. Command itself, post link, and vote weight. We will extract the permlink (Permanent Link) from the post link.

 # !vote command
        if command == utils.commands[2]:
            if len(props) == 3:
                post_link = str(props[1]).split('/')
                post_link.reverse()
                perm_link = post_link[0]
                post_author = post_link[1]
                identifier = f'{post_author}/{perm_link}'
                weight = float(props[2])

                await message.channel.typing()
                try:
                    steem = utils.steem_vote_instance
                   

                except Exception as e:
                    await message.channel.send("Error: " + str(e))

image.png

  • Now we need to use the steem instance to cast the vote and check if it is successfully casted or not by checking the trx_id.
 # !vote command
        if command == utils.commands[2]:
            if len(props) == 3:
                post_link = str(props[1]).split('/')
                post_link.reverse()
                perm_link = post_link[0]
                post_author = post_link[1]
                identifier = f'{post_author}/{perm_link}'
                weight = float(props[2])

                await message.channel.typing()
                try:
                    steem = utils.steem_vote_instance
                    response = steem.vote(weight=weight, identifier=identifier, account=utils.voter_username, )
                    if response.get('trx_id'):
                        await message.reply(f'Voted at {weight}%')
                    else:
                        await message.reply(f'Failed')

                except Exception as e:
                    await message.channel.send("Error: " + str(e))

image.png

  • Let's run and test the command

image.png

Github
Steem Discord Bot Series
SteemPro Official

Cc: @blacks
Cc: @rme
Cc: @hungry-griffin
Cc: @steemchiller
Cc: @steemcurator01
Cc: @pennsif
Cc: @future.witness
Cc: @stephenkendal
Cc: @justyy


Best Regards @faisalamin

Posted using SteemPro Mobile

Sort:  

Hello testing the steem community bot created by @faisalamin

Hello testing the steem community bot created by @faisalamin

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.028
BTC 60638.91
ETH 2449.26
USDT 1.00
SBD 2.52