ttezel / twit

Twitter API Client for node (REST & Streaming API)
4.32k stars 573 forks source link

So i have a code and it send every reply but i want it to only send a tweet is that possibile? #545

Open TiMyTi11 opened 3 years ago

TiMyTi11 commented 3 years ago

This is my code: var T = new Twit({ consumer_key: process.env.TWITTER_CONSUMER_KEY, consumer_secret: process.env.TWITTER_CONSUMER_SECRET, access_token: process.env.TWITTER_ACCESS_TOKEN, access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET, timeout_ms: 60*1000, // optional HTTP request timeout to apply to all requests. strictSSL: true, // optional - requires SSL certificates to be valid. }) client.once('ready', () => { var stream = T.stream('statuses/filter', { follow: [process.env.TWITTER_USER_ID] })

stream.on('tweet', function (tweet) { //... var url = "https://twitter.com/" + tweet.user.screen_name + "/status/" + tweet.id_str; try { let channel = client.channels.fetch(process.env.DISCORD_CHANNEL_ID2).then(channel => { channel.send(**everyone Elon Musk just tweeted: ${url}**) }).catch(err => { console.log(err) }) } catch (error) { console.error(error); } }) }) client.login(TOKEN)

there is more code above it but its from anonther thing

TiMyTi11 commented 3 years ago

And i added this to try to make it work but it doesn't work:

function isReply(tweet) { if ( tweet.retweeted_status || tweet.in_reply_to_status_id || tweet.in_reply_to_status_id_str || tweet.in_reply_to_user_id || tweet.in_reply_to_user_id_str || tweet.in_reply_to_screen_name ) return }

siendomiguel commented 3 years ago

I am the same, send all tweets and retweets, try to apply some filters but they seem not to work

siendomiguel commented 3 years ago

Ready, I got the solution to the problem, I was applying the filter incorrectly, it is not necessary to create a function if it is only going to be used once, as is my case, example:

const twitterClient = new Twitter(twitterConf);
// Specify destination channel ID below
const dest = 'config.channelId'; 

// Create a stream to follow tweets
const stream = twitterClient.stream('statuses/filter', {
  follow: 'config.userId', // user_id
});

stream.on('tweet', tweet => {
    if(tweet.retweeted || tweet.retweeted_status || tweet.in_reply_to_status_id || tweet.in_reply_to_user_id || tweet.delete) {
        // skip retweets and replies ------- new
        return;
    }
  const twitterMessage = `NEWS!! @here **${tweet.user.name}** acaba de realizar un nuevo tweet :grin: \n\n https://twitter.com/${tweet.user.screen_name}/status/${tweet.id_str}`;
  try {
      let channel =  client.channels.fetch(dest).then(channel => { channel.send(twitterMessage)
      }).catch(err => {
          console.log(err)
      })

  } catch (error) {
      console.error(error);
}
});