I built a Twitter Bot with no code in 10 mins
I created a simple Twitter bot that tweets and replies to @mentions... Here's how
Hello again, I hope you’re all doing well - physically and mentally - in these uncertain times. Today’s post will be slightly more technical than usual. As the title says, I played around with Twitter bots recently, and I thought to document and share the process.
Diving straight in, ‘What exactly does my Twitter bot do?’ For now, two simple things:
Auto-tweeting the spot prices (in USD) of BTC and ETH at 8 am WAT daily.
Auto-replying @mention requests with the current prices of BTC and ETH within 15 seconds.
Now, I’ll like to clarify - I’m not a coding novice. Yes, I’m a (non-technical) Product Manager, but I understand basic concepts of programming, and I’ve written functional software using C++ and Python in the past.
That being said, I was able to implement 1. using a no-code workflow tool and Coinbase API; while 2. was successfully implemented using the Python and Twitter API.
Your next question might be - Why did I do this?
Well, a few reasons:
Firstly, I wanted a better understanding of APIs. I mean, I know how they work in theory; But I wanted to actually put one to use, by creating something that uses them to work.
I’m a big fan of automated processes in general, and the working of bots has always fascinated me.
Twitter is my favourite social media app. I’ve always admired their development tools from afar, and I was certain this wouldn’t be a cumbersome process.
Lastly, well, I was on leave and decided to take a day off from.. resting :-)
I should also add that there’s a practical use case, as seen on the screenshot below, where I’ve turned on Tweet notifications for the bot account. This means I get daily updates of these crypto prices straight to my notification feed. Pretty cool.
If you’re still reading, then you must be genuinely interested in this next part - How did I do this? I’ll break this into two parts:
Daily Auto-Tweeting (No Code)
Instantaneous @mention replies (Python)
Part 1. Daily Auto-Tweeting
I’ve always wanted to explore no-code tools, and this felt like a great place to start. Initially, I had a vague idea of how the implementation for this would work. I knew the process flow was something like:
When it’s 8 am → Get the prices → Log into Twitter → Tweet
For the prices, I had a source: Coinbase. I’ve built personal finance Google Spreadsheets in the past, and Coinbase was my source for instantaneous spot prices when tracking Crypto investments. For the rest, well, I just had to find a tool that would encompass all of that.
After a quick search, I stumbled across Zapier, a tool that allows you to integrate web apps and automate workflows. They also have over 3,000 apps integrated, and thankfully Twitter is one of them. It’s totally free for the first 14 days ($19.99 afterwards), which was more than enough time for me to get my hands in and try this out. In summary, no code was needed and it was free!
After a quick sign-up, I got to work. I must say that the process to create this took less than 10 mins and I was really surprised by how easy this was. Here’s a screenshot of the Workflow I created below:
You can check out the full Workflow (or Zap) here.
For the ‘GET’ Action, I used a simple Coinbase API implementation that returns a json with a list of all the current exchange rates in USD. Then I did some formatting on the ‘Numbers’ action to get out the figures I wanted to display. After a quick test, we were all good to go.
But this was too easy. I wanted more.
Part 2. Instantaneous @mention replies (Python)
I thought - In addition to tweeting daily updates, what if we could also get the bot to reply @mentions with requests for live crypto prices? For this part, I decided to write some code.
I drafted the process flow for this as something like:
Check for @mentions → Check if the tweeter asks for ETH or BTC → Get spot price → Tweet reply → Repeat
Firstly, I signed up on the twitter developer platform to get the access and API keys I needed.
Next, as Pythonistas might guess, a Python library already existed that works brilliantly with Twitter APIs: Tweepy. I installed this locally and got to work trying things out.
The first thing I realized was that the Twitter access levels needed to fully implement this required me to submit a request for an upgrade. This took surprisingly less time than I anticipated (about 2-3 hours). Basically, Twitter needed more context on what I was trying to do with their API. These API keys allow you to auto-tweet, DM, collect data, and many more, and so it’s understandable that they have these checks.
For the instantaneous prices, the beautiful people at Coinbase also have a python library for their API. I had to sign-up on Coinbase to get unique keys, but this process only took a few minutes.
After some hours of studying documentation, some lines of basic functions and conditional statements, and then some nice old debugging, we were ready to test. I ran it on my local machine and went on Twitter to test. It worked!
Finally, I needed a place to host and run this (if not, I had to keep my laptop on forever) and I knew pythonanywhere offered exactly that, with a (free version) terminal that I’d need to restart every other day. So I moved the files, pip installed tweepy and coinbase on the Bash console, and we were Live. It’s still up and running, so feel free to try the Bot out yourself too!
I can’t share the actual source code for the entire Python doc because of private auth keys for both Twitter and Coinbase, but you can reach me via LinkedIn and I’ll be happy to share snippets.
As usual, if you enjoyed reading this, please share with people who might also be interested, and also consider subscribing to my newsletter so that you don’t miss any future posts.
Lastly, you can find me on Twitter here, where I often share random musings.
Godspeed.
This is amazing. Keep up the good work.
Nice one! You can use GitHub gist to share the source code and replace your keys with placeholders