Skip to content

#119: Follow, Like and Retweet With Tweepy

Tweepy is not only good for housekeeping, but we can also use it for more positive interactions on Twitter. Let's see how we can like and retweet tweets.

Preparation

As with the examples before, we need the user authentication tokens to work on behalf of that user. We use the V2 endpoints for this post:

import tweepy
import os
import random
from dotenv import load_dotenv
load_dotenv()


# read keys from .env
api_key = os.getenv('api-key')
api_secret = os.getenv('api-key-secret')
client_id = os.getenv('client-id')
client_secret = os.getenv('client-secret')
user_token = os.getenv('access-token')
user_token_secret = os.getenv('access-token-secret')
bearer_token = os.getenv('bearer-token')

client = tweepy.Client(
    bearer_token=bearer_token,
    consumer_key=api_key, consumer_secret=api_secret,
    access_token=user_token, access_token_secret=user_token_secret,
    wait_on_rate_limit=True
)

As a reminder, with this code you can get the user id from a screen name:

1
2
3
4
5
# find user id by username
user_to_follow = "ThePSF"
response = client.get_user(username=user_to_follow)
user = response.data
print(f"id of user '@{user_to_follow}': {user.id}")

id of user '@ThePSF': 63873759

Follow an account

You can follow an account with the follow() method:

1
2
3
# follow a user
result = client.follow_user(target_user_id=user.id)
print(f"user followed? {result.data['following']}")

user followed? True

Unfollow an account

If you have called the follow() method with the wrong id, you can correct this with the unfollow() method.

1
2
3
# unfollow a user
result = client.unfollow_user(target_user_id=user.id)
print(f"user unfollowed? {result.data['following']}")

user followed? False

Like a tweet

We can like a tweet with the method like():

1
2
3
4
# like a tweet
tweet_id = 1499822223338655756
result = client.like(tweet_id=tweet_id)
print(f"liked the tweet? {result.data['liked']}")

liked the tweet? True

Retweet a tweet

The names for the methods in Tweepy are predictable, that means we can us the retweet() method to make a retweet:

1
2
3
# retweet a tweet
result = client.retweet(tweet_id=tweet_id)
print(f"retweeted the tweet? {result.data['retweeted']}")

retweeted the tweet? True

Send a tweet with the V2 client

So far, we only used the V1 API to send a tweet. With the V2 client we can write a tweet with the create_tweet() method:

1
2
3
4
5
6
# write a tweet
result = client.create_tweet(
                text=f"A tweet from the V2 client of #Tweepy {random.random()}",
                reply_settings='following')
print(result)
print(f"Tweet #{result.data['id']} created")

Tweet #1501292904916021249 created

The V2 endpoint offers all the new features that Twitter introduced lately. The Tweepy documentation offers a good overview on all the settings you can use.

Working with bookmarks

Twitter added an endpoint for bookmarks that I explore in this post.

Parting Thoughts

Over the last 10 weeks, I've been working intensively with the Twitter API and Tweepy. I have covered all the parts that I think you need to work effectively with Tweepy. Some parts like the filtering rules for streams offer nasty surprises.

Nonetheless, the Twitter API works well, and you should now have all the pieces together to create your own Twitter bot. For the time being, this is where I stop. I figured out what I need to know about the capabilities of the Twitter API and now is time for something different.

I hope you enjoyed the journey.