-
Notifications
You must be signed in to change notification settings - Fork 2
8. How to post tweets?
There is nothing more simple than posting a tweet, and now you might ask yourself why I didn't start with it?
Well, I do have a reason for this, viewing tweets is a prerequisite for replying to tweets. More below.
Structure:
API.update_status(status, *, in_reply_to_status_id, auto_populate_reply_metadata, exclude_reply_user_ids, attachment_url, media_ids, possibly_sensitive, lat, long, place_id, display_coordinates, trim_user, card_uri)
tweet = 'Hello from the dark side! This is the BerryNews Bot\nCall me, BNB.'
# tweet length cannot be bigger than 160 characters
if len(tweet) <160:
print('Posting tweet:', tweet,'\n')
api.update_status(tweet, in_reply_to_status_id= 1469339850851729412)
else:
print('Tweet is bigger than 150 characters, stopping...')
Result:
As you can see the source is the BerryNewsOrgBot, not Twitter .ios, Twitter Android or Twitter Web

π¨βπ» --> posting_tweet.py
What we did here was to get our last tweet's id, then reply to it
timeline = api.user_timeline(screen_name="berrynewsorg", count=2,exclude_replies=False)
print("Last tweet id:",timeline[0].id)
last_tweet_id = timeline[0].id
reply_of_tweet = "Still here.\nWith <3 BNBπ."
if len(reply_of_tweet) <160:
print('Posting tweet:', reply_of_tweet,'\n')
api.update_status(reply_of_tweet, in_reply_to_status_id=last_tweet_id)
else:
print('Tweet is bigger than 150 characers, stopping...')
print(len(tweet))
sys.exit()
Result:

π¨βπ» --> reply_to_own_tweet.py
But what if we want to reply to the other person that wrote in our original tweet? How do we do that?!
That would mean replying to a mention, so more about that in our next chapter: 9.