Here is another example of a tweet bot in Python. It’s a simple bot that uses Markov chains to compose tweets. A Markov chain is a process that, in our example, choses words from all available possibilities in a given list without remembering the previous choices or combinations; the probability of the next choice would depend on the current choice. Depending on the word grouping and source text length, the output you receive may look like a random word dump. One of the free to download text sources I’ve used is textfiles.com . You can also use the excellent Gutenberg archive with over 58,000 free ebooks. For this occasion, I joined text from two random publications.
The code below should work on Python 3 as well as 2.7. Instructions below assume you’re on a Mac. If you do not have Python installed, you can download it here. Once installed, open a Terminal window and verify the installation:
Next, verify that Python Package Manager (pip) is installed:
Once pip is installed, we can use it to install the necessary libraries:
Also verify that oauthlib is there:
pip install requests requests_oauthlib
You will also need to get markovgen.py from robincamille / bot-tutorial. This will be the engine under the hood driving our bot. Next, you will need to obtain application credentials from twitter. Sign in to your twitter account and go to apps.twitter.com. Click on create new app on top right then follow directions to complete the form. Once your application is created, go to the Keys and Access Tokens tab. Click on the button to generate My Access Token and Token Secret. You will also need to copy the Consumer Key and Consumer Secret. This will be used in credentials.py
CONSUMER_KEY = "your_consumer_key"
CONSUMER_KEY_SECRET = "your_consumer_key_secret"
ACCESS_TOKEN = "your_access_token"
ACCESS_TOKEN_SECRET = "your_access_token_secret"
twip.py (twippy) will generate tweets by using content of your readme.txt file. Once executed, the script will generate twitme.txt
# readme.txt has to be in same dir as script
original = open('readme.txt')
output = open('twitme.txt','w')
mk = markovgen.Markov(original)
line = '\n' + mk.generate_markov_text()
exclude = ['"','(',')',';']
line = ''.join(ch for ch in line if ch not in exclude)
line = line.lower() + "."
bitbot.py is a script that will post text as tweets from your twitme.txt file.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# from original script:
import tweepy
import time
from credentials import *
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_KEY_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
# will tweet this
# twitme.txt generated by twip.py
filename = open('twitme.txt' , 'r')
totwit = filename.readlines()
filename.close()
for line in totwit:
api.update_status(status=line)
print line
time.sleep(3600) # 1hr in seconds
Keep in mind this is not the best or the most ‘intuitive’ bot. Here’s a snippet of a generated text from my sources.
most of them the lightness a.
number of instances, to expend double the sums so collected be offered as a matter of seeing the tubes resultant.
we ought to be raised next year will be seen, that.
stores, etc., as have rifles, pistols, and--at short range between british german.
arms, numbers, and civilization. what, i say, these declarations answer enough? but for england to suppose herself and.
govern it well and watchful,.
bills. in the month of may, the duke gloucester.
spies nor suspected as such your security is there.
them. i look out forrard, one,.
concerns: and when at war, make.
As you can see it’s fairly garbled and confusing. Code I posted above was written as an exercise; feel free to add to it or re-write it !