Wednesday, 27 Nov 2024

How to make a Twitter bot

If Elon Musk is correct, Twitter is lousy with bots. Accounts that run automatically: liking, tweeting and retweeting according to a script.

Often relatively simple, these programs are easy to set up and can be run by pretty much anyone.

Plenty of bots are used for nefarious activity. They can post spam, artificially inflate follower and engagement figures and even push international disinformation campaigns.

But bots can also do all kinds of useful, exciting and silly things.

Take BoschBot, which posts snippets of painter Hieronymus Bosch’s famous ‘Garden of Earthly Delights.’ Or Earthquake Robot, which automatically shares alerts on quakes of magnitude 5 or greater.

Numerous bots are dedicated to Mr Musk, including the recently-suspended ElonJet, which post updates on journeys taken by his private jet.

If you’re interested in learning to code, bots can be a relatively easy way to get to grips with programming.

How can I make a Twitter bot?

You’ll need a developer account, a ‘project’ and an ‘app’ to register your bot with Twitter. The process is relatively simple and should only take a few minutes.

All you need to get started are your regular Twitter login credentials. But your account will need to have a verified phone number on file to qualify.

With these, you can request a developer account at developer.twitter.com. You’ll need to provide details such as your name, location, as well as your intentions for using the portal.

You can even select ‘making a bot’ from a dropdown list.

This will give you a ‘Essential’ access to the portal, allowing you to perform tasks like downloading a user’s Twitter history. If you want your to be able to post automatically, you’ll need to apply for ‘Elevated’ access.

You can do this easily within the developer portal. It requires providing more information about your intended use, which will be reviewed before upgraded access is granted.

When new versions of the API are released, this process may change.

Whichever level of access you have, once you’re logged into the portal, you’ll need to create a ‘project’ and an ‘app’. You’ll need to provide basic details — a name and a description, for example — about the intended bot.

At this point, Twitter will give you several keys that your bot will use to access its application programming interface or ‘API.’

Make sure you store these somewhere safe, as you’ll need them when you program your bot.

You can access, revoke and update these keys inside the developer dashboard for your app:

Now you have your API keys, the fun can start!

Programming your bot

There are several ways to program your bot, some of which don’t require any coding knowledge at all.

Of you just want to do something simple, like automatically repost certain tweets, you can use a free service like the Twitter Bots app to create your bot.

To do so, you’ll need to paste in your relevant API keys and select exactly what you want your bot to do — and when you’d like it to run — from the platform’s drop down menus.

Assuming your account has the right level of access, this platform will make your bot perform according to your specifications. It will autorun, meaning you can press ‘save’ and leave it working away in the background.

If you want to code your own bot, but don’t have any experience, there’s a host of material online to help you.

You can write your bot using Python, one of the most accessible coding languages available.

To use it, you’ll need to download Python itself, or use an online platform like pythonanywhere. If you’re planning to write and save code on your computer, it can be helpful to install a program like Visual Studio Code or Pycharm Community.

You can use these to store and run your code, bearing in mind it will only work while your computer is connected to the internet. If you want your bot to run 24/7, you’ll need to host it on a server like Heroku.

Once you’ve got these installed and opened your first Python file, you’ll need to actually write something!

freeCodeCamp is a great resource for getting to grips with the language. You can find plenty of video courses that will give you the very basics and have you building simple programs in just a few hours.

You can even jump straight in with tutorials like this one from CS Dojo that teach you how to make a simple Python Twitter bot with no experience at all.

As you start to code, you’ll quickly find that Google is your friend. Examples of successful code, and the answers to many error messages can also be found on sites like stack overflow.

In the early days, it’s likely you’ll cobble together much of your code from various tutorials and forum posts.

If you have Essentials access, you can read tweets and collect data about user activity. The Metro.co.uk program pictured below returns the last seven days’ worth of tweets by Elon Musk.

They are displayed in a plain text form in a terminal on the Visual Studio Code:

If you have Elevated access, you can use a tutorial like this Youtube video from coder CreepyD to make a simple ‘like’ bot that automatically likes Tweets that mention certain hashtags.

This creates a ‘stream’ of tweets that fulfill any search criteria you’ve specified. In Metro.co.uk’s version, the bot is looking for tweets that include ‘#elonmusk’.

Once you’ve written (or, realistically, copied and pasted) the code, press ‘run’ in your coding software. Don’t be phased if the first batch of text that appears in your terminal seems like gibberish.

If it’s working, you’ll soon start getting plain-text version of the tweets to your terminal:

As you can see below, the bot profile is automatically ‘liking’ these same tweets:

What if I can’t get my bot to work?

There are several reasons you might not be able to get your bot working. It might be that you’re trying to perform ‘Elevated’ access level tasks on an ‘Essentials’ account.

You may also have copied your API keys incorrectly into your code.

But if all these details are correct, it could be there’s something wrong with the program you’re writing.

In these cases, Google is your friend!

Searching for specific error messages will likely lead you to plenty of online forums discussing the same problems. On sites like stack overflow, you’ll find pages and pages of discussions about how to solve problems, as well as ways to improve your bot.

Source: Read Full Article

Related Posts