Part 6: How To Automatically Reply To Mentions Using Steem-Python

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 and see who mentions you using steem-python!


What will I learn

  • How to create a regular expression
  • How to find people mentioning you
  • How to reply to them

Requirements

  • Python3.6
  • steem-python
  • re

Difficulty

  • Basic

Tutorial

Streaming the blockchain for mentions

We have already gone over how to stream the blockchain in part 2 of this tutorial series, so if you need to, read up about it there. In the previous tutorials we could simply filter the blockchain for the operations we wanted, for example posts or votes, but unfortunately mentions aren't an operation on the blockchain. What this means is that the best way to find out who mentions you is to stream all posts on the blockchain and then check its body to see if someone has mentioned you. So once again, we simply stream all posts on the blockchain like so

from steem import Steem
from steem.blockchain import Blockchain
from steem.post import Post

steem = Steem()
blockchain = Blockchain()
stream = map(Post, blockchain.stream(filter_by=["comment"]))

for post in stream:
    # DO SOMETHING

Creating a regular expression

To find out who mentions us we should read the body of each post and find out if it contains @username. A great way to do this is by using a regular expression. We can use a regular expression to specify the rules for the string that we want to match, a mention in this case. Luckily a good regex to do this can be found here

(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9]+)

We can then use the function findall() to create a list of all mentions in a post. Once we have this we can simply check if our username is in the given list! An example is given below

text = "hello @amosbastian and @juliank I love @steempytutorials, contact me at username@hotmail.com"
mentions = re.findall("(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9]+)", text)
print(mentions)

which outputs

['amosbastian', 'juliank', 'steempytutorials']

Mentions on the blockchain

So now we know how to find mentions in a string we can use this to find mentions in the body of posts. To do this we can use the regex we created above and do the following

REGEX = "(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9]+)"
username = "steempytutorials"

while True:
    try:
        for post in stream:
            mentions = re.findall(REGEX, post["body"])
            if username in mentions:
                print(f"{post['author']} just mentioned you!")

    except Exception as error:
        print(repr(error))
        continue

Running this program and testing it showed the following

$ python mentions.py 
amosbastian just mentioned you!

Replying to mentions

If someone mentions you, you could do many things, but for the sake of this tutorial we are simply going to reply to them. To this we can use the reply() function, which makes replying to a post very simple! Instead of printing the post we will change it to

post.reply(f"Hey @{post['author']} thanks for mentioning me!")

and indeed, it works as expected

reply_mention.png

Congratulations, you can now automatically reply to anyone who mentions you using steem-python!

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

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now