Search code examples
javascriptapitwittertwitter-oauth

Attach image to Twitter Status from Direct Message media


Hi guys i trying to post a status using Twitter https://api.twitter.com/1.1/statuses/update.json POST request API. I also want to update the status with attached image, but the image is already on direct message, so i get a direct message by using GET request and i get the message's object like this..

{
  type: 'message_create',
  id: 'xxxxxxxxxxxxx',
  created_timestamp: 'xxxxxxxxxxxxx',
  message_create: {
    target: { recipient_id: 'xxxxxxxxxxxxxxxx' },
    sender_id: 'xxxxxxxxxxxxxx',
    message_data: {
      text: 'This is trigger message with image,oy! https://x.xx/xxxxxxxxx',
      entities: [Object],
      attachment: {
        type: 'media',
        media: {
          id: xxxxxxxxxxxxxxx,
          id_str: 'xxxxxxxxxxxxxxx',
          indices: [ 39, 62 ],
          media_url: 'https://ton.twitter.com/1.1/ton/data/dm/xxxxx/xxxxx/gr_SQawQ.jpg',
          media_url_https: 'https://ton.twitter.com/1.1/ton/data/dm/xxxxx/xxxxx/gr_SQawQ.jpg',
          url: 'https://x.xx/xxxxx',
          display_url: 'pic.twitter.com/xxxxx',
          expanded_url: 'https://twitter.com/messages/media/xxxxxx',
          type: 'photo',
          sizes: {
            medium: [Object],
            thumb: [Object],
            large: [Object],
            small: [Object]
          }
        }
      }
    }
  }
}

My question is, how you post a status with an attached image from direct message's media image like above??

I have try using the attachment.media.id and attachment.media.id_str from that response as a value for media_ids param like this, but still get an error Invalid media.

const text = message.message_create.message_data.text;
const attachment = message.message_create.message_data.attachment;

const payload = {
    status: text
};
attachment && (payload.media_ids = [attachment.media.id_str]); //i've tried using id_str and id

T.post('statuses/update', payload, (error, data, response) => {
    if (!error) {
        resolve({
            message: `successfuly posting new status with DM id: ${message.id}`,
            data
        });
    } else {
        reject(error);
    };
})

Thank you


Solution

  • You cannot re-post the media from a Direct Message via the same media_id_string, as it has already been "used". You also cannot post the URL and have the image display, as it is private to the the sender and receiver.

    You will need to implement a three-stage process:

    Note that you should think about the privacy implications of reposting images sent via Direct Message, and be sure that the sender is aware that they may be reposted as publically-accessible.