Let's build Bots for STEEM - Part 06: Let's show all new Utopian posts in a Discord Channel

in #steem7 years ago (edited)

Hi Steemians, let's build BOTS!

On this blog I want to document my progress in implementing bots for the STEEM blockchain.


Image Source: https://pixabay.com/de/roboter-computer-bots-charakter-764951/

In my last post I've shown how easy it is to send messages from a python script into a Discord Channel using WEBHOOKS.

Today I want to add some features to get notified in Discord whenever a new utopian-io Post is created.

The Script

This time I'll first show you the complete script.

from steem.blockchain import Blockchain
from steem.post import Post
from steem.instance import set_shared_steemd_instance
from steem.steemd import Steemd
from steem.steem import Steem
from steembase.exceptions import PostDoesNotExist

from webcord import Webhook

webhook_url = "https://discordapp.com/api/webhooks/INSERT_YOUR_WEBHOOK_URL_HERE"
webhook = Webhook(webhook_url, avatar_url=None)

steemd_nodes = ['https://steemd.pevo.science', 'https://api.steemit.com']
set_shared_steemd_instance(Steemd(nodes=steemd_nodes))
steem = Steem()

b = Blockchain()
s = map(Post, b.stream(filter_by=['comment']))

post_urls = list()

while True:
    try:
        for post in s:
            if post.is_main_post():
                if "utopian-io" in post["tags"]:
                    post_url = 'https://steemit.com/tag/@' + post['author'] + '/' + post['permlink']
                    if post_url in post_urls:
                        print("Already posted")
                    else:
                        webhook.send_message(post_url,'botsultant')
                        post_urls.append(post_url)
    except PostDoesNotExist:
        print("Post does not exist", flush=True)

The Result

If we start the script, the URL of all new utopian-io posts is printed to the terminal.

$ python3 discord_webhook.py 
https://steemit.com/tag/@omeratagun/zencart-store-manager-turkish-translation-series-road-to-finish-part2
204
https://steemit.com/tag/@folly-pandy/the-hit-and-fall-of-cryptocurrency
204

In the Discord Channel we also see all new utopian-io posts.

Now, let's take a closer look at the script.

The Imports

For our script we need some classes of steem-python and webhook from the webcord library.

from steem.blockchain import Blockchain
from steem.post import Post
from steem.instance import set_shared_steemd_instance
from steem.steemd import Steemd
from steem.steem import Steem
from steembase.exceptions import PostDoesNotExist

from webcord import Webhook

The Webhook

These two lines are enough to connect the bot to Discord's webhook.

webhook_url = "https://discordapp.com/api/webhooks/INSERT_YOUR_WEBHOOK_URL_HERE"
webhook = Webhook(webhook_url, avatar_url=None)

Connect to the STEEM Blockchain

These 3 lines are my basic configuration for connecting to the Steemd nodes of Steemit Inc and other Witness.

steemd_nodes = ['https://steemd.pevo.science', 'https://api.steemit.com']
set_shared_steemd_instance(Steemd(nodes=steemd_nodes))
steem = Steem()

Give me all new Posts

These two lines of code are sufficient to generate a stream with all new posts.

b = Blockchain()
s = map(Post, b.stream(filter_by=['comment']))

The Main Loop

Our bot is supposed to run forever, so we need a while True: loop. We iterate over all new posts on the chain, and need to catch a PostDoesNotExists exception, which can occur if a post is deleted immediately after creation. Then the post is streamed, but if we try to access it, the exception is thrown.

while True:
    try:
        for post in s:
            if post.is_main_post():
                ...
    except PostDoesNotExist:
        print("Post does not exist", flush=True)

Filter utopian-io posts

We check if utopian-io is one of the hash tags of the article. To do this, we access the tags attribute of the post object.

                if "utopian-io" in post["tags"]:
                    post_url = 'https://steemit.com/tag/@' + post['author'] + '/' + post['permlink']

Check for Duplicates

If a post is edited, it will reappear in the stream. To prevent a post from being sent multiple times to Discord, we save the URLs of the posts in a list, and compare the URLs of new posts with the posts in the list.

                    post_url = 'https://steemit.com/tag/@' + post['author'] + '/' + post['permlink']
                    if post_url in post_urls:
                        print("Already posted")
                    else:
                        webhook.send_message(post_url,'botsultant')
                        post_urls.append(post_url)

What's next?

In my next post we will add some more functionality to the script. I want to get all new posts with hashtag "utopian-io" in my own channel.

Sort:  

Congratulations @botsultant! You received a personal award!

1 Year on Steemit

Click here to view your Board

Do not miss the last post from @steemitboard:

SteemWhales has officially moved to SteemitBoard Ranking
SteemitBoard - Witness Update

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @botsultant! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.028
BTC 58119.05
ETH 2357.18
USDT 1.00
SBD 2.36