Linux Format

Twitter bots

Nate Drake sets up a Twitter bot on his Raspberry Pi to spam the net!

-

Set up your own Twitter bot on the Raspberry Pi to spam the net, and then blame Nate Drake.

The simplest descriptio­n of a Twitter bot is a program designed to produce automated posts on Twitter. Given how simple it is to tweet yourself, it’s worth explaining why people go to the trouble of doing this.

The most common use for bots is for ( Russian?– Ed) spam. The bot examines key words – for example, “cleaning products”– and then responds with a promotiona­l link for people to click to be taken to the spammer’s website.

This feature has non-spammy uses, however. Bots can be programmed to search for any word or phrase, and respond accordingl­y. One entertaini­ng implementa­tion of this was @BDZNappa, which would search for people tweeting the phrase “over 9,000”, to which it would always respond, “WHAT!? NINE THOUSAND?” to the person in question.

Bots can also tweet from a text source, such as @SunTzuBot, which tweets daily quotes from TheArtofWa­r. More sophistica­ted bots, such as @JustDiedBo­t, actively search the internet for source material. @JustDiedBo­t searches Wikipedia for informatio­n about recent deaths, and tweets RIP announceme­nts as and when they happen.

Bots have practical uses, too. They can be programmed to tweet at regular intervals, so can be used as a ‘dead man’s switch’ to tweet a message unless you reset it every day. It’s also possible to schedule a tweet for a future date, so you can use the bot to send reminders.

Pi Bot Ready!

If you find any of these possibilit­ies intriguing, you need to set up a dedicated Twitter account for your bot. Don’t be tempted to use your existing Twitter account for this if you have one, because repeated tweets could be mistaken as spam by Twitter, and your account could be suspended.

In order to proceed, we need a new Twitter account with a confirmed mobile phone number. If you already have a Twitter account, visit https://support. twitter.com/ articles/81940 and follow the steps there to delete your mobile phone number from your account for the time being.

Next visit www.twitter.com and choose Sign Up. You can use any email address you wish, provided it’s not already registered with Twitter. Choose a name that’s appropriat­e for the bot. For the purposes of this tutorial, we’ll create a bot named Sherlock Bot, with user ID @holmesbot1.

You’re asked to provide your phone number and then confirm it by SMS. Twitter requires this for bots to reduce the likelihood of spam.

Once your Twitter account has been created, you may need to click Continue a few times at the top-right to skip invitation­s to import your contacts or follow other users.

Next, we need to create a Twitter applicatio­n that will enable your Pi to access your Twitter account. Go to http://apps.twitter.com, then click Create New App.

You’re asked to fill in the Applicatio­n Details. For the purposes of this tutorial, we’re creating a bot that regularly tweets the Sherlock Holmes stories, but you’re welcome to change the Name and Descriptio­n as you see fit.

Under Website, for now simply put www.twitter.com. Leave the field named Callback URL blank. Tick the box to say you agree to the Twitter Developer Agreement, then click the grey button marked Create your Twitter Applicatio­n.

Make sure Access Level reads Read and Write. If not, click Modify App Permission­s to change it.

Next click Manage Keys and Access Tokens. Scroll to the bottom of the page and click the grey button marked Create My Access Token.

You’ll see a message saying the access token has been generated. Keep a copy of this page in a safe place or leave it open in your browser, because we will need it shortly.

Next, open the terminal app on your Pi or connect to it via SSH. First, we need to install some extra software by using the following command: sudo easy_install pip

Next, create a directory for the bot and open it: mkdir holmesbot1 && cd holmesbot1

For this example, we’re going to a specialise­d Python library created by Edwin Dalmaijer, named Markovbot. The software essentiall­y takes some text from a source (in this case The Adventures­of SherlockHo­lmes) and randomly constructs plausible looking sentences with it.

You need to download and unzip the software with the following command: wget https://github.com/esdalmaije­r/markovbot/archive/ master.zip && unzip master.zip

Move to the new directory with cd markovbot-master and install more required software with these commands: wget https://bootstrap. pypa.io/ ez_setup.py && sudo python ez_setup.py sudo easy_install twitter

Next, we need to download a text file to use as the source for our random tweets. This file comes from the Project Gutenberg website, but feel free to use any TXT file you like: wget http://www.gutenberg.org/cache/epub/1661/pg1661.txt

Then we create an empty file to place our code. You can choose any name you like, provided that you use the extension .py at the end: nano sherlock1.py

 ??  ?? Once the applicatio­n has been created, scroll down to enable the access tokens. This enables the bot to log in to your Twitter account.
Once the applicatio­n has been created, scroll down to enable the access tokens. This enables the bot to log in to your Twitter account.

Newspapers in English

Newspapers from Australia