michimani / gotwi

A library for using the Twitter API v2 in the Go language. (It is still under development).
MIT License
127 stars 26 forks source link

Add Support for Handling Long Tweets with `note_tweet` Field #312

Closed Nhypocrite closed 5 months ago

Nhypocrite commented 5 months ago

Title: Add Support for Handling Long Tweets using note_tweet Field

Description

For long tweets, the default Tweet.text field only contains the initial part of the tweet, with the rest being replaced by "...".

To handle the issue, twitter recently introduced a feature to obtain the full text of long tweets by including the note_tweet field in the request parameters when fetching tweets.

Our library should support this new API feature.

Steps to Reproduce

Make a request to the Twitter API for a tweet longer than 280 characters.

We can see in the response the tweet text is truncated, with the remainder replaced by "...".

Example Request:

curl -X GET "https://api.twitter.com/2/tweets/1729154466539266176" -H "Authorization: Bearer YOUR_ACCESS_TOKEN"

Response:

{
    "data": {
        "edit_history_tweet_ids": ["1729154466539266176"],
        "id": "1729154466539266176",
        "text": "A Big Uranium SHORTAGE is coming in the next decade\n\nUranium mining has significant long-term potential\n\nMiners have run up by almost 50% over the last few months\n\nWe JUST downgraded our Uranium Miners rating from Buy to Neutral\n\nIf you have recently accumulated URA, it may be… https://t.co/pFmyrGoyqv https://t.co/dw2PGshiQr"
    }
}

Expected Behavior

Twitter has introduced a new feature to obtain the full text of long tweets by including the note_tweet field in the request parameters. By adding note_tweet to twitter_field, the response will include the complete tweet in note_tweet.text.

Our library should support this new API feature.

Reference:

How do I get extra long tweet text via V2 api? - Twitter API / Twitter API v2 - X Developers

GET /2/tweets/:id | Docs | Twitter Developer Platform (x.com)

GET /2/tweets/search/recent | Docs | Twitter Developer Platform (x.com)

Example Request:

curl -X GET "https://api.twitter.com/2/tweets/1729154466539266176?tweet.fields=note_tweet" -H "Authorization: Bearer YOUR_ACCESS_TOKEN"

Response:

{
    "data": {
        "edit_history_tweet_ids": ["1729154466539266176"],
        "text": "A Big Uranium SHORTAGE is coming in the next decade\n\nUranium mining has significant long-term potential\n\nMiners have run up by almost 50% over the last few months\n\nWe JUST downgraded our Uranium Miners rating from Buy to Neutral\n\nIf you have recently accumulated URA, it may be… https://t.co/pFmyrGoyqv https://t.co/dw2PGshiQr",
        "note_tweet": {
            "text": "A Big Uranium SHORTAGE is coming in the next decade\n\nUranium mining has significant long-term potential\n\nMiners have run up by almost 50% over the last few months\n\nWe JUST downgraded our Uranium Miners rating from Buy to Neutral\n\nIf you have recently accumulated URA, it may be best to take some profit or hold on to your position instead of adding more\n\nWe're optimistic in the long term but not currently favorable to enter the trade\n\nGet a weekly update on Uranium Miners as part of our Asset Ratings\n\nEach week, we provide our members with an overview of the most attractive assets to long or short\n\nCyber Week is the best deal of the year to join Game of Trades\n\nGet 40% off our membership + a FREE 7-day trial\n\nDon't miss out on this opportunity!"
        },
        "id": "1729154466539266176"
    }
}

Solution Proposed:

To support the handling of long tweets, modify the TweetField structure and the Tweet structure in our library:

Add TweetFieldNoteTweet as an available TweetField when submitting a request:

TweetFieldNoteTweet TweetField = "note_tweet"

Update the Tweet structure to include the NoteTweet field:

type Tweet struct {
    // ...
    NoteTweet *TweetNoteTweet `json:"note_tweet,omitempty"`
}

type TweetNoteTweet struct {
    Entities struct {
        CashTags []TweetEntityTag `json:"cashtags"`
        HashTags []TweetEntityTag `json:"hashtags"`
        Mentions []TweetEntityTag `json:"mentions"`
        URLs     []URL            `json:"urls"`
    } `json:"entities"`
    Text *string `json:"text"`
}

Pull Request related:

I have made the necessary changes and completed testing to ensure the solution works as expected. The test code and results are provided in the linked pull request.

Pull Request: PR #313


As a newcomer to the open-source community, I am open to feedback and suggestions to improve this contribution. Thank you!

michimani commented 5 months ago

The relevant PRs have been merged and this issue is closed.

Thank you for your contribution! @NHypocrite