Creating a Twitter Bot to post when a UK Government passport service is online
This post is how I created a simple Twitter Bot that posts a tweet when a gov.uk passport service is online and appointments are available. I wanted to talk you through it, and how everyone can take existing infrastructure and make it a little more user-friendly with simple tools.
So why did I do this?
I have a family holiday in June; kids, grandparents, parents, aunties etc are having a well-deserved and overdue holiday to the Algarve. However, my Mum, whilst watching her regular ITV morning shows, caught wind that some travellers were being turned away at UK airports by some airlines because of a confusion over the “10 year” passport date interpretation.
In short. Your passport has two dates. A date of issue, and an expiry date. Some airlines were taking 10 years from the date of issue (a fair thing to do) and 3 or 6 months from the 10 years from the date of issue (rather than 3 or 6 months from the expiry date). This meant if you applied for your passport early 10 years ago, you may have not been able to travel. Good news is that this has since been cleared up.
Anyway. My Mum was in this situation. And she’s a worrier. I also knew from others on social media that people were struggling to get appointments for their passport renewal. So I decided to make use of my coding skills for a real-life application (mostly just to help my Mum ❤️).
What did I do?
The Twitter Bot is really simply in reality. It comprises:⃰
- A Python script (using the requests library) to check the webpages⃰* for specific text and create a response
- GitHub Actions to run the code automatically every 30 minutes (each run takes ~25 seconds, so each month I use a max of 310 mins of my 2,000 free minutes)
- Twitter API to post response to Twitter when a service is online
No other magic needed. Just GitHub secrets to keep my API keys safe and secure.
* https://www.gov.uk/get-a-passport-urgently/online-premium-service
* https://www.gov.uk/get-a-passport-urgently/1-week-fast-track-service
All the code can be found on this GitHub repo. The core code is found in the main.py
python script (note: I need to write tests etc), and the GitHub Actions is called by main.yml
The hardest thing was writing the GitHub Action yml file as I am still a little new to this. I think I got it down to a personal best of around 24 small edits and retries before it worked this time. Below is a copy:
name: check
on:
schedule:
- cron: "0,30 * * * *" # runs every 30 mins
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest steps:
- name: checkout repo main
uses: actions/checkout@v2
- name: setup python
uses: actions/setup-python@v2
with: python-version: 3.8
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name:
execute py script
run: |
python main.py
env:
consumer_key: ${{ secrets.CONSUMER_KEY }}
consumer_secret: ${{ secrets.CONSUMER_SECRET }}
access_token: ${{ secrets.ACCESS_TOKEN }}
access_token_secret: ${{ secrets.ACCESS_TOKEN_SECRET }}
access_token_github: ${{ secrets.ACCESS_TOKEN_GITHUB }}
To keep things tidy at the user-end, the script only posts when the service is online (or online and busy).
There is also a script scripts/plot.py
that plots when the services were online in the last week, every week through the GitHub Action plot.yml
. This is useful to see if there were any patterns in when the services were online, or not.
What have I learned from doing this?
1. Being public facing and dealing with questions and user frustrations
The bot has only been online for a week and a bit, but I have amassed almost 500 followers and have had plenty of questions (the DMs are open, but I may struggle to keep this that way going forward due to the volume of messages).
I’ve learned that because the service is online so sporadically, and there is a massive rush to get passports currently, the limited communication on the website means I get asked a lot of questions. I tried and answer these as best I can — but the reason I am getting asked these is that people are struggling to ask them on more official, proper systems (webchat, phonecalls, email).
The main questions I get asked are:
1.1. When will the next appointments be online?
Typically, I have no idea. There is no real pattern to when the services go online. But typically, there appears to be a manual step involved in making them go online. I suspect this because the majority of the times when services do go online, it is during working hours. And for the one weekend I have data for so far, no services when online during the weekend.
and
1.1. When will they likely become available?
This is similar to the above, but instead of people asking a precise time. They are when the likely times are. From the limited data I have, there isn’t a consistent time. Sorry.
and
1.3. There aren’t any appointments!
Frustratingly, sometimes even when you get a place in the queue, and then enter your details, you may be shown a message saying there is no appointments. In part, this Twitter Bot causes a problem, as it makes more people aware of when the services are online, and then more people hit the service at the same time. So as the follower count grows, the chances of success for each follower reduce. We start to hit a bottleneck. Whilst I have no control over the number of appointments, it means I will begin to have to deal with more frustrated Twitter users — and the days of 100% success and gratitude may disappear.
Ultimately though, I created this to help people. And to give people the best chance to get a passport appointment. As a person who wants to ultimately help everyone around me, I understand any frustration users have, and wish I could help more.
2. Do Twitter Bots like this have a place in official Gov systems?
I’m keen to express that this bot is unofficial. It was written to help a personal situation, and has now been used to help others. The question is always, should this bot need to exist?
Yes, in the perfect world maybe the passport application system needs updating to be more user-friendly. I am sure there will be lessons learned going forward and every Gov department does try their best to improve user-experience. But change is hard, and takes time. As a result, perhaps there is an interim world where Gov makes more use of automated systems such as this one to help users.
The reason for this is that they can be built on top of the existing system, whilst it is being redeveloped. None of what I have done here uses anything other than what is presented to the outside world on the current website.
However, I see the challenge of Gov departments creating things like this themselves. As whilst I have been inundated with positive messages about how I am helping people — because I am a person — I’m not as confident that a Gov department would received the same level of positivity from the general public if the services weren’t online and accessible 24/7.
This links to point 1.3 above, as I am currently dealing with more frustrated users than I did a few days ago as my follow count grows. A Government department with even more followers and outreach would have many, many, many more messages.
3. Coding and automation has practical applications — so learn how to do it!
Coding languages like Python, tools like GitHub Actions, and APIs like Twitter’s mean things like this are relatively easy to do. None of what I have shown here are outside the capabilities of anyone with a basic grasp of Python or another coding language.
One of the first ever times I coded was to automate moving files from my parents desktop PC from one folder to others based on categories. I was really proud of what I did then. And despite my day job being coding, and creating tools far more complex than this one here, I still got a real sense of pride knowing something I did was useful for others.
It’s why I really am happy to see coding skills being taught in schools. So that children can have that sense of pride in machine-related problem solving to sit alongside physical achievements such as sports and the arts.
So that’s it. If you have any questions, drop me a line and I will try and answer them.
Michael