Based on a tweet by @TheOtherTracy, I’ve been working on a Twitter bot to handle promotional tweets. Here is the full write-up.
By standing on the shoulders of giants, so to speak, I modified that script to handle replies, line break, and images. Here’s what I ended up with. The lines beginning with # generally contain instructions.
import tweepy
import random
import requests
import os
#These are your credentials. Follow along here to set this up: https://www.theothertracy.com/2022/01/11/automatic-promotion-tweets-python-and-tweepy/
#When applying for elevated access, be very explicit about using your bot to promote games you make using images you created. I recommend answer "NO" for anything regarding DMs, follows, and data aggregation/analysis
access_token = 'your token here'
access_token_secret = 'your token secret here'
#API Key is the consumer_key. API Secret key is the consumer_secret
consumer_key = 'your api key here'
consumer_secret = 'your api secret key here'
# Authenticate to Twitter
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
# Create API Object
api = tweepy.API(auth, wait_on_rate_limit=True)
# Open the Tweets file and pick a random line into a string.
def publictweet():
#change this path to the location you are installing this script. Forward slashes only.
path='C:/Use/Your/Own/Path/Here'
#images are in the above path in the /images/ subfolder
#lines are stored in a file named tweets2.txt in the path above.
#lines are formatted like so:
#This is a tweet\nThis is the second line of the tweet.\nThis is the third line. |Check it out here: https://www.example.com |image1.png,image2.png,image3.png,image4.png
#the segments are separated by pipes |. The first segment is the main tweet. The second is the reply, which is a common place to put a link. The third is a list of images, in the images folder. Images are separated by commas. image1.jpg,image.jpg, etc.
# \n is how you add line breaks.
#lines are selected randomly. There's no brain there.
#If you want images, you have to use a reply tweet. If you don't want images, leave off that segment.
#If you don't want a reply tweet, leave that and the images off of the line.
#Don't spam.
lines = open(path + 'tweets2.txt').read().splitlines()
tweettopublish = random.choice(lines).split("|")
tweetpartone=splitlines(tweettopublish[0])
if (len(tweettopublish)>1):
tweetparttwo=splitlines(tweettopublish[1])
if (len(tweettopublish)>2):
tweetpartthree=tweettopublish[2]
print(tweetpartthree)
filenames=tweetpartthree.split(',')
media_ids = []
for filename in filenames:
print(filename)
res = api.media_upload(path + 'images/' + filename.rstrip())
media_ids.append(res.media_id)
if (len(tweettopublish)>2):
response=api.update_status(status=tweetpartone,media_ids=media_ids)
else:
response=api.update_status(status=tweetpartone)
print(tweetpartone)
print(response)
if (len(tweettopublish)>1):
response2=api.update_status(status = tweetparttwo, in_reply_to_status_id = response.id_str)
print(tweetparttwo)
print(response)
def splitlines(texttosplit):
split_on_break = texttosplit.rstrip('\n').split('\\n')
multiline_tweet = "\n".join(split_on_break)
return multiline_tweet.replace("\\n","")
# Run the function
publictweet()