Skip to content
Simple Twitter bot with Tweepy
Python
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.env.example
.gitignore
LICENSE
README.md
main.py
tweet_author.py
tweet_listener.py
tweet_status.py
tweet_utils.py

README.md

How to build a Twitter bot?

Table of Contents

Prerequisites

# Create a Twitter app and grab your credenials

# Install dependencies
pip3 install git+https://github.com/tweepy/tweepy
pip3 install python-dotenv

Environment setup

# Create an .env file, it should look like this
TWITTER_CONSUMER_KEY=<your_consumer_key>
TWITTER_CONSUMER_SECRET=<your_consumer_secret>
TWITTER_ACCESS_TOKEN=<your_access_token>
TWITTER_ACCESS_TOKEN_SECRET=<your_access_token_secret>

Authenticate your app

import os
import dotenv
import tweepy

# Load the environment variables
dotenv.load_dotenv()

auth = tweepy.OAuthHandler(
    os.getenv("TWITTER_CONSUMER_KEY"),
    os.getenv("TWITTER_CONSUMER_SECRET"))

auth.set_access_token(
    os.getenv("TWITTER_ACCESS_TOKEN"),
    os.getenv("TWITTER_ACCESS_TOKEN_SECRET"))

Create a Tweet listener

import tweepy

class TweetListener(tweepy.StreamListener):
    def on_data(self, data):
        # Print the Tweet
        print(data)
        print(100 * '*')
        return True

    def on_error(self, status):
        print(status)

Fire up the stream!

# Keywords example
keywords = ['bitcoin', 'ethereum', 'ripple']

listener = TweetListener()
stream = tweepy.Stream(auth, listener, tweet_mode='extended')
stream.filter(track=keywords)
You can’t perform that action at this time.