I am uploading media to use on multiple DMs. The upload seems to go through fine, and the media shows up fine on the DM if I only send to one person. But I get an when I try to send the same media object to more that one person.
Here are the params I’m sending to the image upload:
"Params: media: #<File:0x00007f9248050c80>, media_category_prefix: dm, shared: true"
Which is yielding a media id:
"Twitter media id: 1029489610953506817"
But if sent to a second person I get the following error:
#<Twitter::Error::Forbidden: There was an error sending your message: media id already used.>

Are you able to confirm that you did send that media ID attached to a Direct Message to one user?

Not sure what type of confirmation would be helpful, but I can confirm that the upload happens inside of a messages loop, and a single message can have multiple recipients, the twitter media id is sent on each recipient.
Here is the code:

# Get the media object if present
      media = Medium.find(message.media_id) if message.media_id.present?
      # p "Media: #{media}"


      # if the media search above returned an object then we upload it to twitter
      twitter_media_id = nil
      
      if media.present?
        # file = Paperclip.io_adapters.for(media.object).read
        temp_file = Tempfile.new(media.object_file_name)
        temp_file.binmode
        media.object.copy_to_local_file(:medium, temp_file.path)
        # NOTE: Does not appear that marking a media object as shared is working so for now the solution is to upload media for each recipient otherwise this could be done once per message
        twitter_media_id = MediaHelper.upload_media_to_twitter(message.oauth_token, message.oauth_secret, temp_file)
        # p "Twitter media id: #{twitter_media_id}"
      end


      # Attempt to send message and record status on recipient
      recipients.each do |rec|
        # p '****** Debug ******'
        # p "Message: #{message.inspect}"
        # p "Recipient: #{rec.inspect}"

        result = MessagesHelper.send_twitter_dm(message.oauth_token, message.oauth_secret, message.body, rec.recipient, rec.name, twitter_media_id)
        if result.present?
          rec.platform_id = result['id']
          rec.status = result['status']
          rec.response = result['response']
          rec.sent_at = Time.now if result['status'] == 'sent'
          rec.save
        end
      end

Hope that is useful…

I’m not exactly sure what is going on here, so I’m trying to work through and reproduce.

My understanding - which I’ll admit may be flawed or missing some specific detail is:

  • uploading a media item successfully - either for Tweeting, or sending in a Direct Message - means that the media object is stored for 24 hours (the expires_after_secs value on the response message).
    • you can attach non-shared images just to one object (Tweet or Direct Message), after which the media ID is n longer valid as the data is “hardened” and attached to that object.
    • you cannot attach a dm_image to a Tweet, or a tweet_image to a Direct Message - error 324 unsupported raw media type will be thrown.
    • shared media objects are not supported for Tweets, but are for Direct Messages, and they still have to be used within 24 hours regardless.

When you were previously setting the value of shared in the upload INIT step, did you do so as a Boolean, or as a String?

Sorry there is obvisouly some business logic not explained there:
Here is a gist with the files in question

The message_queue_job loops through a table looking for “message” ready to be sent based on a scheduled time.

Assuming a message is ready to send, we check if the user has attached media to the message (stored in our system)

If the message has media, we get a temp_file to upload to twitter (we mark it DM, and mark it as shared with a boolean)

Then we loop over all the recipients of the particular message and send them a dm, with the twitter_media_id as the media object

Does that help at all?

Just poking this message to see if you’ve had any insight, know you’re busy…

@andypiper any news here?