10

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 ?

Keelah
  • 192
  • 3
  • 15

1 Answers1

19

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)
shuuji3
  • 1,233
  • 13
  • 20
  • Thanks a lot for this answer ! I am actually using quotes wich are quite simple to use. I may help a lot of people though. – Keelah May 03 '17 at 07:26
  • 1
    `media_ids = [api.media_upload(f).media_id_string for f in filenames]` – polfosol ఠ_ఠ Nov 25 '20 at 05:06
  • This solution is prompting the following error: `update_status_with_media() missing 1 required positional argument: 'filename' ` Any idea ? – bog Dec 21 '21 at 14:25
  • @bog Hello. According to the docs (https://docs.tweepy.org/en/stable/api.html#tweepy.API.update_status_with_media), it seems like `update_status_with_media()` has different parameters. It was deprecated so maybe you'd like consider use `media_update()` and `update_status()`. – shuuji3 Dec 24 '21 at 03:16
  • @shuuji3 Thanks, this worked for me: `msg = "Test with 2 img" pics = ("ninja.jpg", "ninja2.jpg") media_ids = [api.media_upload(i).media_id_string for i in pics] api.update_status(status=msg, media_ids=media_ids)` – bog Dec 26 '21 at 17:08
  • @bog Glad to hear that! – shuuji3 Dec 26 '21 at 23:55
  • @shuuji3 hey i tried the same thing but i keep getting the error 'media_ids parameter is invalid' any idea whys this coming up? – sensualbumfuzzle Apr 02 '22 at 17:49