Government, Misc.

A Twitter bot to wish Congress happy birthday

I’m going to write a little about a Twitter bot I created: @HR_15951. It’s been active since January 2021, shortly after the current House and Senate sessions began. The bot has one very important job: to wish representatives and senators a happy birthday.

I’m not going to post the full code here. For one, the main Python script is way too long. But also because this is intended to be a general overview of the project. If you want to create your own Twitter bot there are better resources available. If you’d like to learn about @HR_15951, this is the place.

I’ll touch on a few points:

  1. Why this project exists.
  2. The Twitter API.
  3. The project’s structure.
  4. The data.
  5. Deployment.

1. Why does the bot exist?

This is an easy one. It seemed like a fun project and no one else had done it. More precisely, someone had done it before but they shut it down a few months before I had the idea. In fact they claimed the Twitter handle I would have chosen if I had first dibs, but more on that later.

It isn’t a very serious project. I understand you always have to consider the possibility when government is involved, but there are no hidden intentions. I don’t collect data on anyone who interacts with the tweets. That information isn’t visible to me anyway.

I was interested in getting my feet wet with the Twitter API and the bot has helped with that. I’ve also gotten to know the names of many congresspeople and senators that I wouldn’t know otherwise.


2. What’s the Twitter API?

The API allows you to compose tweets like @HR_15951 does, but that’s just one small part of its feature set. You can also extract information about tweets that already exist, follow and unfollow accounts, and—perhaps most importantly—view a real-time stream of tweets posted across the platform. I’m sure people can (and do) monitor the Twitter firehose to pick up on news and public opinion before it becomes more widely known. My humble bot is uninterested in such things.


3. How does it actually work?

I maintain a dataset of politicians that includes their name, birthday, district or state, and Twitter handle. Every morning at 8:00 AM ET a Python script reads the dataset and checks who, if anyone, is celebrating a birthday.

Tweets look slightly different depending on how many people share the day. For example if three or more people are listed, the names are separated with commas and an “and” before the final name. I hope details like that make the bot seem slightly more human.

To the point of seeming more human, emojis at the beginning of tweets are chosen at random, e.g. 🎂Happy Birthday🎂. You’ll also occasionally notice emojis at the end of tweets. The script checks if it’s currently a holiday and if so, it adds a little extra flair. For example on St. Patrick’s Day: 🍀.

After composing a tweet the bot uses Tweepy to send it. It then follows the accounts it tweeted at.

Besides daily birthday wishes the bot sends congratulations for a few other things:

  1. When one of the five oldest or youngest members of either chamber celebrates a birthday.
  2. Two or more people were born on the same day of the same year.
  3. A notable retired member of Congress has a birthday. In that case it includes a fact about their career.

Every day the bot does a dry run of the next day’s tweet. If there is some problem, like the text exceeding 280 characters, it lets me know via email. It does so using smtplib from the Python standard library. I’m notified of any runtime errors as well.


4. Where does the data come from?

I wish I could rely on an API but I’ve chosen to maintain the data myself, for two reasons:

  1. I couldn’t find a reliable service that had all the information I wanted.
  2. Even if I could, I intend for the bot to operate indefinitely and I’d rather not make it dependent on someone else providing a free service.

When I began programming a couple years ago I made a weather monitor that interfaced with the Accuweather API. When their free service was discontinued I jumped to Dark Sky. Then Apple bought them and shut down their free service, so now I’m working on moving again. I’d like to avoid that experience if possible. I can’t personally collect weather data 24/7/365 but I can keep tabs on Congress.

To that end, I’ve been able to rely on the dedicated anonymous editors of Wikipedia. I’ve built scrapers that check once per day for any changes in membership. If a change is detected I’m notified by email and I update the dataset.


5. Where does the bot live?

It lives in an AWS data center somewhere in the eastern half of the United States—probably Virginia or Ohio. I already had an EC2 instance hosting the backend of a web site and the bot moved in next door.


The bot’s handle is a reference to H.R. 15951, a bill which established several federal holidays in 1968.

Come say hello: https://twitter.com/HR_15951