Let's make a Twitter Bot with Python!

What is Twitter Bot?

Bot is the etymology of Robot and refers to something that does something automatically. On twitter

--Automatic Tweet Bot --Automatic Like Bot --Automatic follow bot

There are a wide variety of bots.

What are the benefits of bots?

Automatic tweets act like notifications. For example, "Tweet automatically when the live information of your favorite artist is updated". Auto-like and auto-follow work as a tool to let an unspecified number of people know your account. This is used, for example, for PR activities on your Twitter account.

Bot creation procedure

The procedure for creating a bot is mainly the following 3 steps.

  1. Twitter Developer registration
  2. Program Creation (Python)
  3. Periodic execution registration (Cron)

Implementation

This time, we will create a bot that regularly tweets the current time. Details are introduced in the following video.

https://youtu.be/Ab6TU9sFBM4

English edition https://youtu.be/mmKXdLUhG_k

Source code

credential.py


CONSUMER_KEY        = '********'
CONSUMER_SECRET     = '********'
ACCESS_TOKEN_KEY    = '********'
ACCESS_TOKEN_SECRET = '********'


twitter_bot.py


from credential import CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET
from requests_oauthlib import OAuth1Session
from http import HTTPStatus
from datetime import datetime

def post_tweet(body):
    #Authentication process
    twitter = OAuth1Session(
        CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET
    )
    #Tweet processing
    res = twitter.post("https://api.twitter.com/1.1/statuses/update.json", params={"status": body})
    print(res)

    #Error handling
    if res.status_code == HTTPStatus.OK:
        print("Successfuly posted")
    else:
        print(f"Failed: {res.status_code}")

def main():
    # body = "Test post 2"
    now = datetime.now()
    post_tweet(now)


if __name__ == '__main__':
    main()

Recommended Posts

Let's make a Twitter Bot with Python!
Make a Twitter trend bot with heroku + Python
Let's make a GUI with python.
Let's make a graph with python! !!
[Super easy] Let's make a LINE BOT with Python.
Let's make a shiritori game with Python
Let's make a web framework with Python! (1)
Let's make a web framework with Python! (2)
Let's make a Discord Bot.
Make a fortune with Python
Steps to create a Twitter bot with python
Let's replace UWSC with Python (5) Let's make a Robot
I made a Twitter BOT with GAE (python) (with a reference)
[Let's play with Python] Make a household account book
Let's make a simple game with Python 3 and iPhone
[For play] Let's make Yubaba a LINE Bot (Python)
Make a recommender system with python
Let's make a supercomputer with xCAT
If you want to make a discord bot with python, let's use a framework
Let's make a websocket client with Python. (Access token authentication)
Create a Twitter BOT with the GoogleAppEngine SDK for Python
Let's create a free group with Python
Let's make a simple language with PLY 1
[Python] Let's make matplotlib compatible with Japanese
Let's make a tic-tac-toe AI with Pylearn 2
Let's make a combination calculation in Python
Make a desktop app with Python with Electron
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
Let's make a web chat using WebSocket with AWS serverless (Python)!
[Python] Make a game with Pyxel-Use an editor-
Create a LINE BOT with Minette for Python
I made a Twitter fujoshi blocker with Python ①
I want to make a game with Python
Try to make a "cryptanalysis" cipher with Python
[Python] Make a simple maze game with Pyxel
Try to make a dihedral group with Python
Make a LINE WORKS bot with Amazon Lex
I made a Mattermost bot with Python (+ Flask)
Python beginners decided to make a LINE bot with Flask (Flask rough commentary)
[Ev3dev] Let's make a remote control program by Python with RPyC protocol
Make one repeating string with a Python regular expression.
Make a morphological analysis bot loosely with LINE + Flask
Try to make a command standby tool with python
Twitter graphing memo with Python
Get Twitter timeline with python
Let's run Excel with Python
Use Twitter API with Python
[Practice] Make a Watson app with Python! # 2 [Translation function]
[Practice] Make a Watson app with Python! # 1 [Language discrimination]
Make a simple Slackbot with interactive button in python
Let's make Othello with wxPython
Make a breakpoint on the c layer with python
Make Puyo Puyo AI with Python
Make a LINE BOT (chat)
Let's make dice with tkinter
I made a LINE BOT with Python and Heroku
Make a bookmarklet in Python
Let's make dependency management with pip a little easier
Let's write python with cinema4d.
Make a CSV formatting tool with Python Pandas PyInstaller
Let's make a Mac app with Tkinter and py2app