Sorry for the choppy title, word limit (ironic). I've created a bot that responds with a randomly chosen string from a list when it's mentioned, pretty normal.
The thing is that most of the strings in my list are more than 280 chars, twitters word limit. Also the fact that the users twitter handle, the '@', and the space after the username (where the string should start) count towards the word limit
This is the code that I have come up with but I am stumped...
import replies # Separate file where all replies are stored
def reply_to_tweets():
print("Scanning for new mentions...")
last_seen_id = retrieve_id(FILE)
mentions = api.mentions_timeline(last_seen_id, tweet_mode="extended")
myself = api.me()
for tweet in reversed(mentions):
if myself.screen_name in tweet.full_text: # If my @ is in the full text
if tweet.in_reply_to_status_id is not None:
continue
last_seen_id = tweet.id
store_id(last_seen_id,FILE)
random_choice = random.choice(replies.strings_more_than_280) # Random string chosen
def splitter(chosen_string):
return [char for char in chosen_string] # This part may be obsolete but it gives
# me peace of mind as I know it counts
# emojis and spaces!
username = '@' + tweet.user.screen_name
# There is a space at the beginning of every string in my list, so this should
# account for the character after the @ where the string should go
length_of_user = len(username) # WORKS
splitter_in_action = splitter(random_choice)
length_of_string = len(splitter_in_action)
# This is where the space is counted, since there's a space in the beginning of
# the string already, then it'll count towards the length of the string when we
# calculate it using the splitter() function
difference = length_of_string - length_of_user
difference2 = difference - length_of_user
if length_of_string + length_of_user > 280:
print(username + random_choice[:difference2]) # status update goes here
# Now those last few lines of code, I'm just guessing, I have no idea how to
# calculate for the difference in my string minus the user's @ and then tweet up
# to 280 chars, then tweet the rest later. It's probably simple but I am perplexed!!!
The error is [{'code': 186, 'message': 'Tweet needs to be a bit shorter.'}]
- EDIT: Deleted the whole error callback (not sure exactly what it's called) and just added the major error above
Any help would be greatly as I've been stuck here a while. This isn't meant to spam people and be annoying, It's just that the messages I want the bot to print are too long, so yeah, I know it's a lot of code but I really hope someone can enlighten me!