Introduction

Bots have always been something that’s caught my attention, but I had always assumed they were much to complicated to make myself. This project opened my eyes to the possibilities, and showed me how its possible to make bots, with minimal effort.

Materials

  • Mini HDMI Cord
  • USB-C Power Cord
  • Cooling system / Fan
  • Keyboard
  • Mouse
  • Raspberry PI 4
  • Desktop / Laptop
  • Micro SD Card

Resources

  • https://www.youtube.com/watch?v=Cj7NhuLkvdQ
  • https://www.youtube.com/watch?v=Vk9TY3qNdr8&t=1262s
  • https://www.youtube.com/watch?v=2UBcRiddwAo
  • https://www.youtube.com/watch?v=ppwtk9WHixA
  • https://docs.tweepy.org/en/stable/changelog.html#version-4-0-0-2021-09-25
  • https://docs.tweepy.org/en/stable/install.html
  • https://developer.twitter.com/en/docs/twitter-api

Development

Creating this Twitter Bot, I had very little clue of how I was going to get it working. My first step was looking on YouTube for a tutorial, after a bit of searching I came across a tutorial, which I would later realize was outdated, since the Twitter API (Tweepy) was constantly changing and updating, this would become a theme throughout the making of this bot.

Despite this I was able to create a Twitter Development Account, after typing a 250 character word explanation on what I would be using this account for, I was able to grab the correct API Key’s to authenticate my Bot.

Once an up to date tutorial was found on the code itself, the struggle was to get it to work, after troubleshooting for a few hours, I decided the best step would to eliminate the possible of the Raspberry PI itself being an issue, and tested the same code ran on my PI, on my PC. It would work fine without issue on the PC.

Realizing the Raspberry PI was running on a very outdated version of Python (Python2), I began going through the hassle of updating it, luckily I found an informative tutorial that was able to get me through it. With the PI overheating once or twice, Python3 was installed. After getting the system to default to using Python3, instead of Python2, I was able to install Tweepy V.4.14.0 and get the Tweet to finally send through.

My original idea for the bots actual functionality was to be a joke bot, that would post a funny joke everyday, I decided to go with that idea and create a “Mad Libs Bot”. Predetermining 7 animals, and 7 actions, I would randomize a number and come out with a sentence like this “Did you know a pig can pinch a chicken 25221 times?”. This would then go through each and every day and print a different combination of words!

Code

#Add the needed imports
import random
import tweepy
from time import *
from random import *

# Each key is used to authenticate with Twitter
API_KEY = ‘ ‘
API_KEY_SECRET = ‘ ‘
ACCESS_KEY = ‘ ‘
ACCESS_KEY_SECRET = ‘ ‘
BEARER_TOKEN = ‘ ‘

# Create the client instance with the correct credentials
client = tweepy.Client(BEARER_TOKEN, API_KEY, API_KEY_SECRET, ACCESS_KEY, ACCESS_KEY_SECRET)

# Authenticate in order to use the API
auth = tweepy.OAuthHandler(API_KEY, API_KEY_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_KEY_SECRET)

api = tweepy.API(auth)

# Create a list of different words and such
animalList = [ ‘dog’, ‘cat’, ‘pig’, ‘cow’, ‘chicken’, ‘donkey’, ‘airplane’, ‘t-rex’, ‘rooster’, ‘alligator’, ‘sloth’ ]
actionList = [ ‘jump’, ‘pinch’, ‘throw’, ‘hug’, ‘catch’, ‘record’ ]

while True: 

    # Randomize different elements of our twee

numberAnimal1 = randint(0, len(animalList ) – 1)
    numberAnimal2 = randint(0, len(animalList) – 1)
    numberAction = randint(0, len(actionList) – 1)
    number = randint(10000, 30000)

    # Create the final tweet, and tweet it!
    message = ‘Did you know a ‘ + animalList[numberAnimal1] + ‘ can ‘ +           actionList[numberAction] + ‘ a ‘ + animalList[numberAnimal2] + ‘ ‘ + str(number) + ‘

 times?’

    client.create_tweet(text = message)

    # Wait a day before posting another Tweet
    sleep(60 * 60 * 60 * 24)

Conclusion

This was by far on of my favorite projects I’ve gotten to work on. This project has really opened up a lot of different possibilities for me in terms of creating bots with even the Raspberry PI itself. The most annoying part of this project was by far getting everything up to date on the PI, but overall it was very fun to work on!

Categories: Raspberry Pi

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

css.php