PLhery / node-twitter-api-v2

Strongly typed, full-featured, light, versatile yet powerful Twitter API v1.1 and v2 client for Node.js.
https://www.npmjs.com/package/twitter-api-v2
Apache License 2.0
1.25k stars 175 forks source link

fix: ReceivedMessageCreateDataV1 #407

Closed secchanu closed 1 year ago

secchanu commented 1 year ago

the changes this PR

Fixes errors due to differences between MediaEntityV1 and the actual response type

why it should be merged

When run as per DownloadamediaattachedinaDM

const eventsPaginator = await client.v1.listDmEvents();

for await (const event of eventsPaginator) {
  if (
    event.type === EDirectMessageEventTypeV1.Create &&
    event[EDirectMessageEventTypeV1.Create].message_data.attachment
  ) {
    console.dir(event, { depth: null }); //log
    const image = await client.v1.downloadDmImage(event);
  }
}

The actual response of the event (DirectMessageCreateV1) was as follows.

{
  type: 'message_create',
  id: '**********',
  created_timestamp: '1668707634771',
  message_create: {
    target: { recipient_id: '**********' },
    sender_id: '**********',
    message_data: {
      text: 'テスト https://t.co/**********',
      entities: {
        hashtags: [],
        symbols: [],
        user_mentions: [],
        urls: [
          {
            url: 'https://t.co/**********',
            expanded_url: 'https://twitter.com/messages/media/**********',
            display_url: 'pic.twitter.com/**********',
            indices: [ 4, 27 ]
          }
        ]
      },
      attachment: {
        type: 'media',
        media: {
          id: **********,
          id_str: '**********',
          indices: [ 4, 27 ],
          media_url: 'https://ton.twitter.com/1.1/ton/data/dm/**********/**********.jpg',
          media_url_https: 'https://ton.twitter.com/1.1/ton/data/dm/**********/**********/***.jpg',
          url: 'https://t.co/**********',
          display_url: 'pic.twitter.com/**********',
          expanded_url: 'https://twitter.com/messages/media/**********',
          type: 'photo',
          sizes: {
            thumb: { w: 150, h: 150, resize: 'crop' },
            small: { w: 680, h: 680, resize: 'fit' },
            medium: { w: 1080, h: 1080, resize: 'fit' },
            large: { w: 1080, h: 1080, resize: 'fit' }
          }
        }
      }
    }
  }
}

※attachment is the type of MediaEntityV1 So, urlOrDm = attachment.media_url_https in downloadDmImage becomes undefined

note

Tried with 3 types: photo, video, animated_gif Please let me know if I missed something

PLhery commented 1 year ago

Hello, sorry for the delay and thanks for your contribution!

I see a small mistake: MediaEntityV1 is used in TweetEntitiesV1, and TweetEntitiesV1 doesn't have the "type: media" https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/entities

So instead of changing MediaEntityV1, could you update ReceivedMessageCreateDataV1?

Thank you!

secchanu commented 1 year ago

@PLhery Thank you for your comment.

Instead of changing MediaEntityV1, I changed ReceivedMessageCreateDataV1.

PLhery commented 1 year ago

Thank you!

PLhery commented 1 year ago

(I'll create a new version tomorrow hopefully)

PLhery commented 1 year ago

Sorry I forgot to create the new version, now available in 1.12.10 :)