Tag Archives: bot

I recently wanted to a CSV of questions and answers in to a Twitter account that tweeted questions followed by the correct answers every hour like so:

My CSV looks like this when opened in excel. There are 5 columns in each row, the first of which is the question, followed by a description of the answer, the answer itself and two incorrect answers. For this project I am not going to use the description of the answer in the second column, although I will be leaving it in for a different project.

The process is broken down in 4 parts with a few steps in each.

1. Setting up a *nix environment to handle it.

I wanted to do this on a Raspberry Pi because it is cheap to leave on all day and I wanted to do a bunch of other stuff with it. Also I can leave the Pi on and it can push tweets to the account 24/7. Unfortunately I don’t actually have a Raspberry Pi yet, I am doing this on OS X tho so the process is very similar and should be easy to follow. When I do get a Pi I will update the tutorial accordingly!

The only things I really need are Python and Cron, both of which are available on *nix systems. If you are using windows then you will have to find a way yourself to periodically run my script.

Python is already installed on MAC OS X and should be on Debian based systems. The script uses the Tweepy package which will not be installed and needs to be via:

pip install tweepy

2. Create a twitter APP

To use the Twitter API you need to create an application. I’ve found this to be a real pain recently as Twitter seems to be turning its back on small time developers having fun to concentrate on the big customers, I quite often get all sorts of unexplained errors while creating an app, these errors appear to be an attempt to crack down on small time developers having a good time. To combat this it seems best to use an account that has been active for a while and has a mobile phone number associated with it.

You need to sign in to https://apps.twitter.com/ and create a new app, the details you fill in when creating the account are not that important and you can leave the callback URL blank. Once you’ve created the application you will see a bunch of information about your application, you need to make note of 4 things:

consumer key

secret key

access key

access secret

3. Write a Python Script

I want my twitter account to do the following:

Pick a random question

Tweet the Question

Shuffle the answers

Tweet the 3 options

The script below imports the modules we need, opens a CSV file,

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

import tweepy,time,sys

import random

import csv

CONSUMER_KEY='xxxxxx'#keep the quotes, replace this with your consumer key

CONSUMER_SECRET='xxxxxx'#keep the quotes, replace this with your co$

ACCESS_KEY='xxxxxx'#keep the quotes, replace this with your access $

ACCESS_SECRET='xxxx'#keep the quotes, replace this with your access to$

auth=tweepy.OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)

auth.set_access_token(ACCESS_KEY,ACCESS_SECRET)

api=tweepy.API(auth)

f=open('bio.csv','rU')

ran=random.randint(1,(len(f.readlines())))

f=open('bio.csv','rU')

csv_f=csv.reader(f)

tweet=""

i=0

forrow incsv_f:

i=i+1

ifi==ran:

tweet="Q: "+row[0]

tweet2=row[2]

tweet3=row[3]

tweet4=row[4]

api.update_status(tweet)

array=[tweet2,tweet3,tweet4]

random.shuffle(array)

api.update_status("A: "+array[0])

api.update_status("B: "+array[1])

api.update_status("C: "+array[2])

4. Time the script to run every hour or so

You need to add this script to add this to Cron, a system for scheduling bash commands ran at a scedule:

Edit Me

This site is a collection of things I've found interesting or wanted to write about. I often don't proof read - it is full of bad spelling, grammar mistakes. Sometimes there are better ways to explain or demonstrate things.

All the posts here can be found on GitHub, If you want to make an edit you can by taking a fork of my repository, updating the master branch and sending a pull request. I will review and merge.