Creating an X/Twitter and Bluesky bot.

Chase Carter
6 min readDec 30, 2024

--

Despite frequent news stories detailing the decreasing availability of computer science jobs, I have been spending time each day to improve my knowledge and experience in Python. My background in system and network administrator showed me how useful Python can be as a scripting language, but I always put off learning it.

However, after finding this course on Udemy by Dr. Angela Yu, I have spent an hour every day focusing on studying and practicing writing small programs. This course teaches a new concept each day, and reinforces this concept by having the student create a project daily that focuses on using the new knowledge gained. I have been uploading the projects to my GitHub page.

I decided to create a small project on my own, both as an exercise in creating my own program from inception to implementation, as well as to get some inspiration each day. The program I decided to create is a simple script that will post a motivational quote each day to my X/Twitter and Bluesky accounts.

X API

The first step was reading through the X API documentation. This requires registering for a developer account here. Reading through the documentation, I found a reference to a Python library called Tweepy. The Tweepy library had everything I needed, and had a GitHub page with examples that provided clear guidelines on how to use the package.

Tweepy GitHub example

To get the necessary data to use the Tweepy library (consumer key, consumer secret, access token, and access token secret), you need to navigate to your X developer dashboard. Here you will create a new project.

In the project settings, you will see a section titled “User authentication settings”. Click edit, and ensure that you have the app permissions set to read and write. Once that is done, go the the “keys and tokens” page of your project. Here you will need to locate the “Consumer Keys” and the “Authentication Tokens” sections. These correspond to the consumer key/secret and access token/secret fields needed for the Tweepy library in Python.

Consumer Keys and Authentication Tokens on X Developer Portal

I did not want to hard code my keys, tokens, and secrets into a .py file. While I am still learning Python, it is fairly obvious that this is not best practice. What I have been practicing in Udemy course is to use a .env file to store keys, tokens, etc, and to use the dotenv and os modules in Python to access these pieces of data.

How to store keys, tokens, and secrets in a .env file

Once the needed information is stored in a .env file, I can now access the data by using the load_dotenv() command, and the using os.getenv(variable_name) to store the information to a variable in the main.py file. Now it is time to test the Tweepy module. I used the example from the Tweepy GitHub page to test my first tweet.

Code to test Tweepy module
Successful test post

Bluesky API

Now that I know the Tweepy module works, the next part of the project is posting to Bluesky. Bluesky conveniently already has a Python package called atproto that can be imported into a project and used to post to Bluesky.

Bluesky sample Python code

One thing to note with the atproto module is that even though you are using a .env file to store login information, it is still a good idea to not use your Bluesky password. Bluesky provides a method to set app passwords, giving you a separate password for each app you provide access. If you navigate to the App Password section in your settings, you can generate a new 16 character password to use for your Python program.

App password page on Bluesky

Adding our username and app password to the .env file, we are ready to test the atproto module in our app.

Adding the Bluesky atproto code to our project
Successful test post

Quotes

Now that a successful test post has been made on both X/Twitter and Bluesky, the next part of this project is to get inspirational quotes to post. I looked a several different websites, trying to find a free API that returned quotes, but I could not find one to my liking. I’m sure there is a better way, but I decided to create a .json file with 365 quotes. This did not take long, as I found lists of quotes, and created a quick Python script that would read a text file by line and append the quote and author to the json file.

quotes.json file with 365 inspirational quotes

To access these quotes, I used the json module on Python to load the file and retrieve a quote. To make sure that the Python script would get a different quote each day, I used the datetime module to find how many days had passed since the beginning of the year, and then use that number to retrieve the quote from the quotes.json file based on it’s position within the file.

Code to get new quote from quotes.json file each day

Handling Errors

The last consideration was that I wanted to be alerted if an issue arose while posting to X/Twitter or Bluesky. I wrote a definition that would send me an email with the error code. I used the smtplib module, passing in the platform the error occurred on and the error message given. Once that was complete, it was time to test the code.

Modified code to email me if there are errors
Testing the code
Successful post to X/Twitter
Successful post to Bluesky

Set program to run daily

Now that I have tested the program, I wanted to set it to run each day. While I may look into services at a later date such as Azure App Service, Google App Engine, Amazon Lambda, Heroku, or Render, I decided to simply run the script from my local machine. To do this, I used PyInstaller to create a .exe file of my Python program.

PyInstaller command to create .exe

PyInstaller will create the .exe file in the same location where your Python project is located. The issue I noticed is that since the .env and .json files used in the project were coded using relative paths, those files had to be in the same directory where the new .exe file was. I decided it was best to move the three files to a new location in my AppData directory.

New directory in AppData folder

The final step was to use Windows Task Scheduler to run the .exe every day at 8:00 AM.

Using Task Scheduler
Run .exe everyday at 8:00 AM

Project Complete

I enjoyed this project. It exposed me to several utilities I have not seen in my Udemy course. I interfaced with two different social media platforms, set up an email alert for errors, and scheduled the Python program to run daily. I hope that this project will give others a little motivation each day when they check their X/Twitter and Bluesky accounts.

You can view my final code on my GitHub with this link.

Follow me on X/Twitter and Bluesky to get an inspirational quote daily.

--

--

Chase Carter
Chase Carter

Written by Chase Carter

Certified IT Professional | Programming Hobbyist | MS Cybersecurity | CISSP | CISM | CASP+

No responses yet