the red penguin
HOME ABOUT SITEMAP BLOG LOGIN

How to create a simple Twitter bot

This article originally appeared on https://robmanuelfuckyeah.substack.com/p/how-to-create-a-simple-twitter-bot

People often ask me how to get started on writing bots and generally I give them two answers depending on how technical they are.

The first, least nuts and bolts answer, is to use this service:

https://cheapbotsdonequick.com/

Cheap Bots Done Quick will allow you to create a simple bot like @yokoonobot, without getting your fingers dirty with code. The level of knowledge you need is being able to follow instructions on their website and being comfortable editing lists of phrases and words.

CBDQ an excellent service and it’s definitely what you should use if you’re not a coder and/or mostly interested the messing with language possibilities of bots.

However, it’s not the technology I use for bots. I hand-code bots like some 18th century blacksmith – this is because it gives me more control and more options.

So the question is, how do I build this type of bot?

Well, shit, this is going to take a lot of typing and I’m not sure I can be arsed to type every damn detail, so I’ll try and give an overview in Q&A format and hopefully you, if you’re a bit techy, can fill in the blanks yourself.

What language should I use? PYTHON

Now telling people to use one programming language over another always winds people up, however what’s amazing about Python is all the libraries people have written to do stuff with it. Whatever you want to do, you can probably build it gluing some libraries together. Someone has already done 90% of the work for you and the rest of it is slotting Lego together.

Where should I host it? Use AWS

Right there’s two options really for running bots – from your home computer or on a server somewhere. AWS is as good as anyway. Yes this is “the cloud” AKA a server somewhere.

To be fair for your first bot you’ll probably be ok running it from your home computer, I just personally got fed up with my fan spinning up in the middle of the night and decided to make that someone else’s problem.

How do I install Python?

Well one of the things I like about Python is that it’s already installed on Macs. Open a terminal window, type ‘python’ and you’re already there. Perfect.

In you’re using Windows then here’s some instructions.

https://docs.google.com/document/d/1kVE07_QtfzdWhXsCJxkOFePSXHQ4hca710XLnlKkcns/edit?usp=sharing

Can you just write a tutorial?

OK first you need to know how to do a hello world:

print "Hello World"

save this out as a text file called bot1.py and you can exectute it on the command line line as

python bot1.py

Great. Now you need to know how to shuffle language a bit

import random

world=["Kate Bush","Wolverhampton", "England", "London", "Internet", "Wankers"]

print "Hello %s" % (random.choice(world))

That’s going to print a random word. Cool huh?

Now you need to know how to make that into a phrase:

import random

hello=["Hello","Greetings","Hi","Yo","Hiya","Hullo","Bonjour"]
world=["Kate Bush","Wolverhampton", "England", "London", "Internet", "Wankers"]

print "%s %s" % (random.choice(hello),random.choice(world))

Now you need to know how to tweet it. You’re going to have to register a twitter account as a dev account and get all the API Key stuff.

dev.twitter.com

You’re going to have to install tweepy – a library that talks to twitter from python

pip install tweepy

and now all this bollocks here will allow you to tweet it via code:

# -*- coding: utf-8 -*-

import tweepy, random

def getApiKey():
    apikey={
        "tck" : "copy API from dev twitter",
        "tcs" : "copy API from dev twitter",
        "tat" : "copy API from dev twitter",
        "tas" : "copy API from dev twitter"
    }
    return (apikey)

def openTwitter():
    apikey=getApiKey()
    auth = tweepy.OAuthHandler(apikey['tck'], apikey['tcs'])
    auth.set_access_token(apikey['tat'],apikey['tas'])
    api = tweepy.API(auth)
    return api

hello=["Hello","Greetings","Hi","Yo","Hiya","Hullo","Bonjour"]
world=["Kate Bush","Wolverhampton", "England", "London", "Internet", "Wankers"]
tweet="%s %s" % (random.choice(hello),random.choice(world))
api = openTwitter()
api.update_status(tweet)

Right now the only bit missing that makes it a bot is making it happen automatically by sticking it in a cronjob.

on the command line something like

crontab -e

that’ll let you edit it

then add the line

0 * * * * python $HOME/python/bot1.py

to make it execute every hour.

Saturday 19 February 2022, 377 views


Leave a Reply

Your email address will not be published. Required fields are marked *