Search code examples
twittergoogle-apps-script

Twitter text retrieval with new 280 character system


Using Google Scripts, I have a program that retrieves the text from one account's tweets and uses it for various other things. It's been running for over a year with minimal issues, but now the tweets are being adjusted to 280 characters and I can't retrieve the second half of the tweet. I have:

 function refreshing_v2() {


  var service = getTwitterService();
  if (service.hasAccess()) {

var url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=(redacted)&count=1&include_rts=0&exclude_replies=1';

var response = service.fetch(url);
var tweets = JSON.parse(response.getContentText());
for (var i = 0; i < tweets.length; i++) {

  //Parse the tweet
  var latest = new String(tweets[i].text); 
  var maxi_id = tweets[i].id;
  var startpos = latest.indexOf("1: ");

etc, etc, continues doing stuff with what it's found.

This allows me to get the FIRST half of the text. What's retrieved looks something like "[Content of first half]...(link to tweet)"

How do I get the full text?


Solution

  • You need to add tweet_mode=extended to get the full text in the response. You may need to check the entities you receive to see if it is what you are expecting.

    Documentation link - https://developer.twitter.com/en/docs/tweets/tweet-updates

    Additionally, you will need to use full_text rather than just text

    So:

    //Parse the tweet
    var latest = new String(tweets[i].full_text); 
    

    See the sample tweet at https://github.com/twitterdev/tweet-updates/blob/master/samples/initial/compatibilityplus_extended_13997.json