How to use twitter-text - 10 common examples

To help you get started, we’ve selected a few twitter-text examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pixeldesu / surfbird / src / twitter / actions.js View on Github external
ipcMain.on('surfbird:send:tweet', function (e, tweet) {
  var tweetLength = twittxt.getTweetLength(tweet.text)
  if (tweetLength > 140 && tweet.media.length > 0) {
    e.sender.send('surfbird:hook:nosup:tweet')
  }
  else if (tweetLength > 140) {
    var tweetid = tweet.id
    var tweets = []

    // split the tweet into chunks
    var chunks = tweet.text.split(' ').filter(function (it) {
      return it.length > 0
    })

    // calculate mentions
    var mentionsLength = 0
    var mentions = [] // NOTE: Not using twitter-text function because we only want leading tweets.
    while (chunks.length) {
github sillygwailo / Slack-Twitter / slack-twitter.js View on Github external
require(__dirname + '/plugins/filter/' + file);
      });
      filters = require(__dirname + '/plugins/filters.js').filters;
      filters.forEach(function(filter) {
        text = filter.execute(text);
      });
      if (TwitterText.getTweetLength(text) <= 140) {
        T.post('statuses/update', { status: text }, function(error, data, response) {
          if (error) {
            console.log('Posting tweet error: ' + error);
          }
        });
      }
      else {
        channel = rtm.dataStore.getChannelById(message.channel);
        web.chat.postMessage(channel.id, "The tweet was too long! Character count: " + TwitterText.getTweetLength(message.text), function(err, res) {
          if (err) {
            console.log('Error:', err);
          } else {
            console.log('Message sent: ', res);
          }
        });
        channel = null;
      } // If message longer than 140 character
    });
  } // Message type.
github pixeldesu / surfbird / src / twitter / actions.js View on Github external
} else {
        chunks.unshift(chunk)
        break
      }
    }
    if (chunks.length == 0) { // only has mentions???
      mentions = chunks
      mentions = []
      mentionsLength = 0
    } else if (mentionsLength > 140) {
      e.sender.send('surfbird:hook:fail:tweet')
      return console.error('Too many mentions!')
    }

    // map urls
    var urls = twittxt.extractUrls(tweet.text, {extractUrlsWithoutProtocol: false}).map(function (it) {
      return it.toLowerCase()
    })

    while (chunks.length > 0) {
      var tweet = [].concat(mentions)
      var textLength = 0
      while (tweet.length + textLength + mentionsLength - 1 < 140 && chunks.length > 0) {
        var chunk = chunks.shift()
        var length = chunk.length
        // check if it's a url
        if (urls.indexOf(chunk.toLowerCase()) > -1) {
          length = chunk.match(twittxt.regexen.urlHasHttps) ? 23 : 23 // TODO: Use configuration.
        }
        // chunk if overflow
        if (tweet.length + textLength + mentionsLength + length > 140) {
          // word is too long, split it up.
github wejs / we-core / api / controllers / PostController.js View on Github external
// validation error is encountered, w/ validation info.
        if (err) return res.negotiate(err);


        // Because this should only update a single record and update
        // returns an array, just use the first item.  If more than one
        // record was returned, something is amiss.
        if (!records || !records.length || records.length > 1) {
          req._sails.log.warn(
          util.format('Unexpected output from `%s.update`.', Model.globalId)
          );
        }

        var updatedRecord = records[0];

        var mentions = twitter.extractMentions(values.body);
        // update post mentions
        Mention.updateModelMentions(req.user, 'body', mentions, 'post', pk, function(err, mentionedUsers) {
          if (err) {
            sails.log.error('Error on updateModelMentions', err);
            return res.serverError();
          }

          updatedRecord.mentions = mentionedUsers;

          // If we have the pubsub hook, use the Model's publish method
          // to notify all subscribers about the update.
          if (req._sails.hooks.pubsub) {
            if ( updatedRecord.creator ) {
              // send the change to others user connected devices
              sails.io.sockets.in('follow_user_' + updatedRecord.creator).emit(
                'post', {
github pixeldesu / surfbird / src / twitter / actions.js View on Github external
}

    // map urls
    var urls = twittxt.extractUrls(tweet.text, {extractUrlsWithoutProtocol: false}).map(function (it) {
      return it.toLowerCase()
    })

    while (chunks.length > 0) {
      var tweet = [].concat(mentions)
      var textLength = 0
      while (tweet.length + textLength + mentionsLength - 1 < 140 && chunks.length > 0) {
        var chunk = chunks.shift()
        var length = chunk.length
        // check if it's a url
        if (urls.indexOf(chunk.toLowerCase()) > -1) {
          length = chunk.match(twittxt.regexen.urlHasHttps) ? 23 : 23 // TODO: Use configuration.
        }
        // chunk if overflow
        if (tweet.length + textLength + mentionsLength + length > 140) {
          // word is too long, split it up.
          if (textLength == 0) {
            if (urls.indexOf(chunk.toLowerCase()) > -1) {
              e.sender.send('surfbird:hook:fail:tweet')
              return console.error('Error! Can\'t segment URL!')
            } else {
              length = 140 - (tweet.length + textLength + 1 + mentionsLength)
              chunks.unshift(chunk.substr(length))
              tweet.push(chunk.substr(0, length))
              textLength += length
              break
            }
          } else { // push it to the next tweet
github bvaughn / tweets / src / components / Tweet.js View on Github external
reduced.push(
            <a href="{`https://twitter.com/${mention.screenName}`}">
              @{mention.screenName}
            </a>
          );
          reduced.push(' ');
          return reduced;
        }, []);
      }
    }

    if (!isScrolling) {
      text = t.autoLink(text, AUTO_LINK_OPTIONS);
    }

    // Upscale user profile images; for some reason the API sends blurry low-res pictures.
    let profileImageSource = tweet.user.profile_image_url_https;
    if (profileImageSource.indexOf('_normal.') &gt;= 0) {
      profileImageSource = profileImageSource.replace('_normal.', '_bigger.');
    }

    return (
      <div>
        {retweeter &amp;&amp;
          <div>
            <i aria-hidden="true"></i></div></div>
github mubaris / emoji-tweeter / src / App.js View on Github external
render() {
    const len = parseTweet(this.state.tweet).weightedLength;
    const percent = Number((len * 100 / 280).toFixed(2));
    const pickerStyle = {
      position: 'absolute',
      right: '1em',
      bottom: '4em',
      zIndex: 9999
    }
    // const cap = this.state.capital;
    return (
      <div>
        
          
            <h1 style="{{">{`Emoji ${this.state.emoji} Tweeter`}</h1>
          
        
        </div>
github wejs / we-core / api / controllers / PostController.js View on Github external
.exec(function found(err, newInstance) {

        if (err) return res.serverError(err);
        if (!newInstance) return res.notFound();

        var mentions = twitter.extractMentions(newInstance.body);
        // update post mentions
        Mention.updateModelMentions(req.user, 'body', mentions, 'post', newInstance.id, function(err, mentionedUsers) {
          if (err) {
            sails.log.error('post:create:Error on updateModelMentions', err);
            return res.serverError();
          }

          newInstance.mentions = mentionedUsers;

          // If we have the pubsub hook, use the model class's publish method
          // to notify all subscribers about the created item
          if (req._sails.hooks.pubsub) {
            if (req.isSocket) {
              Model.subscribe(req, newInstance);
              Model.introduce(newInstance);
            }
github sokcuri / Kureha / app / tweet.js View on Github external
id: tweet.retweeted_status.id_str,
          timestamp: new Date(Date.parse(tweet.retweeted_status.created_at)).getTime(),
          isFavorited: tweet.retweeted_status.favorited,
          favoriteCount: tweet.retweeted_status.favorite_count,
          isRetweeted: tweet.retweeted_status.retweeted,
          retweetCount: tweet.retweeted_status.retweet_count
        };

      a.className = 'tweet_wrapper';

      a.setAttribute('data-tweet-id', tweet.id_str);
      a.setAttribute('data-tweet-timestamp', tweet.timestamp_ms);

      var mentioned_me = false;
      if (!tweet.retweeted_status)
        for (var name of Twitter_text.extractMentions(tweet.text))
          if (name == App.screen_name) mentioned_me = true;
      if (mentioned_me) className += ' tweet_emp blue';

      // retweeted / favorited
      var retweeted = '';
      var favorited = '';
      if (tweet.favorited)
        favorited = 'favorited';
      if (tweet.retweeted || tweet.retweeted_status && tweet.user.id_str == App.id_str)
        retweeted = 'retweeted';

      var id_str_org = tweet.id_str;

      var div = document.createElement('div');
      div.className = className;
github mitmedialab / gobo / client / app / components / Post / TweetText.js View on Github external
entities.media.forEach((e) =&gt; {
      text = text.replace(e.url, '');
    });
  }

  // remove any quote links
  if (entities &amp;&amp; data.quoted_status) {
    entities.urls.forEach((u) =&gt; {
      if (u.expanded_url.indexOf('/status/') &gt; -1) {
        text = text.replace(u.url, '');
      }
    });
  }

  // replace + style links and mentions
  text = twitterText.autoLinkWithJSON(text, (entities || {}), { usernameIncludeSymbol: true });
  text = text.replace(/href=/g, 'style="text-decoration: none;color:#6CCCF9;" href=');

  // replace + style emoji
  text = twemoji.parse(text);
  text = text.replace(/