HI Twitter, how are you today?

I’ve been trying to send a direct message with an image attachment as per guidelines here
and here.

Unfortunately sending message with attached media id does not work. Without media - works just fine.
This is my request (Ruby):

    response = Faraday.post do |req|
      req.url('https://api.twitter.com/1.1/direct_messages/events/new.json')
      req.headers['Authorization'] = auth_header.oauth_auth_header.to_s
      req.headers['Content-Type'] = 'application/json'
      req.body = message_data.to_json
    end

This is my message_data.to_json json (ids changed on purpose):

{
   "event":{
      "type":"message_create",
      "message_create":{
         "target":{
            "recipient_id":"105891411"
         },
         "message_data":{
            "text":"gogogo image attachment ggg",
            "attachment":{
               "type":"media",
               "media":{
                  "id":"920608989016133555"
               }
            }
         }
      }
   }
}

response: "{\"errors\":[{\"message\":\"Internal error\",\"code\":131}]}"
Have to remind that same request without attachment part works perfectly fine.
Could someone please help me here?

Many Thanks in advance

When you uploaded the media, were you able to confirm that it succeeded using the STATUS command, before moving on to send the Direct Message?

Thanks Andy, that’s a very good shout. I haven’t confirmed the status actually.
Will add STATUS ensuring block and come back here with some results!

Thanks again

1 Like

While performing STATUS check stumbled upon another blocker.

After performing the INIT phase as per above I got the media_id:

[4] pry(#<Attachment>)> media_id = JSON.parse(response.body)['media_id_string']
=> "920638646432272385"

when I submit the same id to status check:

    response = Faraday.get do |req|
      req.url("https://upload.twitter.com/1.1/media/upload.json?command=STATUS&media_id=#{media_id}")
      req.headers['Authorization'] = auth_header.oauth_auth_header.to_s
    end

I get {"request":"\/1.1\/media\/upload.json","error":"Invalid mediaId."}.

Please note that url is correct:

[1] pry(#<Attachment>)> response
=> #<Faraday::Response:0x007fd2b70db8a8
 @env=
  #<struct Faraday::Env
   method=:get,
   body="{\"request\":\"\\/1.1\\/media\\/upload.json\",\"error\":\"Invalid mediaId.\"}",
   url=#<URI::HTTPS https://upload.twitter.com/1.1/media/upload.json?command=STATUS&media_id=920638646432272385>,

Also upload hasn’t expired yet because STATUS request is made in just few seconds after INIT request.
I guess this is why I’m getting 131 on the first place, but how is this possible that media_id is missing seconds after INIT phase?

Going through this Invalid media Id for now

Ha, apparently I overthought all this stuff. Using plain old upload endpoint (not multi-chunk upload) works like a charm. And it doesn’t require all the INIT, APPEND, STATUS checks, will go with that for now.

Thanks again Andy.

1 Like