Skip to content

8. How to post tweets?

Bogdan Tudorache edited this page Feb 17, 2022 · 7 revisions

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)

A. CREATE TWEET

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 CCAE37EC-5465-47D0-85D2-C908F87B272E

πŸ‘¨β€πŸ’» --> posting_tweet.py

B. REPLYING TO YOUR OWN TWEET

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:

image

πŸ‘¨β€πŸ’» --> reply_to_own_tweet.py

WAIT

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.

Contact:

πŸ”—πŸŒ³ All-in-One

πŸ’Ό BT LinkedIn

Clone this wiki locally