This repo is a simple template for asocial Twitter bots using Tweepy, a fancy module for Python which wraps the authentication process.
- Python 3 installed;
- Tweepy installed;
- Your own:
- Twitter application: you can create it after login in Twitter for developers;
- Keys and access tokens of your application, which you can get in your new application menu;
- JSON file (in my case, secret.json) with these keys and tokens so you don't hardcode them inside your .py script. You can skip this item in case you're not sharing your script anywhere;
- Input .txt file preferably saved with UTF8 encoding, with the content you want to tweet. I had some errors trying to tweet with another default encoding from the Windows notepad.
Once you have everything above mentioned, you can start replacing my garbage secret.json
and file.txt
with your valid files.
You need to replace my garbage keys and access tokens strings with your valid ones:
{"Twitter":
{
"apiKey" : "kPyv06kPfxTQV4zLCSt4u7slk",
"apiSecret" : "41nJ9bJHQtKh09WO2guPB07e1NjAx5jiuJFYfBlgLRrHDoPD54",
"accessToken" : "996001281827057775-UPou9k6v63KRqJMndioey0E5Ma0WNp5",
"accessTokenSecret" : "vl03mgC1kNePAL3KlWDpgsBOSGJNJiHm8EncRpH85ZI0A"
}
}
Then, replace my garbage file.txt
with whatever you want the bot tweeting. Note that the script will read the input file line by line, so you should try to create a proper file separated in 140-length lines or less; otherwise the script will tweet every large line divided into as many lines as possible to tweet them (preceded by "1:", "2:" and so on):
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque
laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto
beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut
odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro
quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora
incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam,
nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
Additionally, in the opposite case, if the script finds short lines (i.e. three consecutive lines with a total length shorter than 141 characters), it will concatenate them in a single tweet.
You can also change the tweeting frequency in lines 37-38 of the bot_template.py
file. My bot, for instance, writes 10 consecutive tweets every hour:
37 time_between_tweets = 30 # Time in secs between consecutive tweets.
38 time_between_tweeting = 3600 - 30*10 # Time in secs between tweeting.
This is a ultra-simple bot which I found useful for learning the basics of Python, Tweepy and Raspberry Pi, but you can try whatever you imagine. Change your input, use other Twitter functions through Tweepy, make an interactive bot... and enjoy.