Skip to content
Learn Python with Rune

Learn Python with Rune

I help people succeed with Python for Data Science & Machine Learning

  • Start here!
    • Start Python Today
    • Start Data Science
    • Start Machine Learning
    • Start Crypto (tutorials)
    • Start OpenCV (tutorials)
    • Start Data Science (tutorials)
    • Start Machine Learning (tutorials)
    • Start Excel (tutorials)
    • Start Data Structures (tutorials)
    • Start Sort & Search (tutorials)
    • About
  • FREE Video Courses
    • Learn Python Simple, Intuitive and Intended
    • Expert Data Science Blueprint
    • Machine Learning with Python
    • Financial Analysis (Part I): Technical Analysis
    • Financial Analysis (Part II): Risk and Return
  • Online Courses
    • Python for Finance: Financial Analysis for Investing
    • Master Modern Security and Cryptography by Coding in Python
    • Start OpenCV with Python: Real-time Processing with Webcam
    • Python for Data Science: Master NumPy & Pandas on Real Data
    • Master Data Structures for Optimal Solutions in Python
    • Master Sort & Search Algorithms – Learn it Easy with Python
  • YouTube
  • Twitter
  • Facebook
  • GitHub

Tag: tweet

Posted on May 22, 2020August 6, 2020

From Zero to a Working Twitter Bot in Python – All You Need to do in 6 Easy Steps

What will we cover

  • All the steps from creating a twitter account to implement a working Python program that will post a message in that account.

To achieve that you need the to go through the following steps.

  1. Create a Twitter Account, if you don’t already have one.
  2. Apply for a developer account, otherwise you will not be able to use the Twitter API
  3. Create an App in Twitter, which is used to generate the tokens you need to connect your program.
  4. Generate the needed tokens.
  5. Install Tweepy, which is a Python library that makes all the API stuff easy for you.
  6. The actual Python program.

Well, let’s get ready.

Step 1: A Twitter Account

Okay, this one is simple. You need to have a Twitter account to tweet from. If you do not already have one, then go to twitter.com and create an account.

Step 2: Apply for a Developer Account

Yes, you need to apply for a developer account. Don’t worry it is an automated process and if you follow the instructions here, you should get it immediately.

Log in to developer.twitter.com with your twitter credentials (if it does not happen automatically).

See in the top right corner it says Apply. Press that and continue to this screen.

Press the “Apply for a developer account”. Then you end up here.

Choose under Hobbyist “Making a bot” and click Next.

That will guide you to a verification page. You might need to enter your phone number, your country and what to call you (your name).

You will be promoted by questions that you need to fill out.

And then press next. Verify the data you entered and press “Verify”. Then finally you need to submit the application.

Then you should receive an email with confirmation.

Step 3: Create an App

From the link in the confirmation mail you get directed to here.

Where you should find the menu with Apps and click it.

Then create an App. That is, you need to click “Create an app” in the upper right corner.

You should obviously enter you own URL.

Finally enter something similar and press create.

Step 4: Create Access tokens and get API tokens

To do that you need to click Keys and Tokens.

On that page you will get your API key and API secret key (not included in picture, but you should find them there).

Next is to generate Access token and Access token secret key. Press “Generate” and copy them. You will need them.

Step 5: Install Tweepy

Tweepy is an easy to use library to Python that will make your life easy. In a terminal type.

pip install tweepy

You might be using pip3 and you might need to be admin or install it locally.

Step 6: Implement your first Python Bot

Now to the fun stuff.

import tweepy
# You need to replace all these with your tokens.
consumer_key = "Replace this with your API token here"
consumer_secret = "Replace this with your API secret token here"
access_token = "Replace this with your Access token"
access_token_secret = "Replace this with your Access secret token"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# Now this is actually doing what we want.
api.update_status(status="Hello, World!")

Free ebook

  • 70+ pages
  • 17 projects
  • Downloadable code

Get your copy




Recent Posts

  • Batch Process Face Detection in 3 Steps with OpenCV
  • The Ultimate Pic Chart Guide for Matplotlib
  • 11 Useful pandas Charts with One Line of Code
Tweets by PythonWithRune
YouTube

Do like 10,000s of others...


  • TwitterTwitter
  • Facebook
  • YouTube
  • GitHub
Proudly powered by WordPress
Go to mobile version