Categories
bots python python-3.x tweepy twitter

Sending multiple medias with tweepy

I am trying to make a Twitter bot with tweepy. It’s actually my first twitter BOT, I’m kinda new to it.

I have a list of medias containing the path of each image I need to send.
I am able to send tweets with text

api.update_status(status="some text")

Or sending tweets with one single media

api.update_with_media(filename, status="some text with media")

But I need to send many images with my tweet.
I heard that I need to upload my files first but I don’t know how to integrate them in the tweet.
Or maybe there is another way of doing it ?

In case you want to upload multiple images, you can use Twitter API’s media/upload via Tweepy’s api.media_upload() method.

This method returns a response object containing media_id and you can attach multiple media_ids to api.update_status().

So the code you may want to write is like this:

# Upload images and get media_ids
filenames = ['1.png', '2.png', ...]
media_ids = []
for filename in filenames:
res = api.media_upload(filename)
media_ids.append(res.media_id)
# Tweet with multiple images
api.update_status(status="many images!✨", media_ids=media_ids)