ttezel / twit

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

How to get tweet link #548

Open EnesKeremAYDIN opened 3 years ago

EnesKeremAYDIN commented 3 years ago

Hi, I want the bot to add the link of the tweet labeled to the link section in the code below, how do I do it?

const twit = require("twit");
var config = require('./config')

console.log('Bot online.');

var T = new twit(config);
var date = new Date();
date = `${date.getFullYear()}-` + `${date.getMonth()}-` + `${date.getDay()}`;

var stream = T.stream('statuses/filter', { track: `@UserName` })
stream.on('tweet', replyMentions)

function replyMentions() {
    T.get('statuses/mentions_timeline', { count: 100 }, function (err, data, response) {
        if (data) {
            data.forEach(function (mention) {
                replyMentions = `link`
                T.post('statuses/update', { status: replyMentions, in_reply_to_status_id: mention.id_str }, function (err, data, response) {
                })
            })
        }
    })
}
thebigfundamentals commented 3 years ago

You will need to extract the tweet id and the username from data, and create a tweet URL from it - it has a pattern: https://twitter.com/<USERNAME>/status/<TWEETID>

Create a function to generate the link you need:

const tweetURL = (user, id) => {
    const url = `https://twitter.com/${user}/status/${id}`;
    return url
}

Use this function inside your code:

  function replyMentions() {
    T.get('statuses/mentions_timeline', { count: 100 }, function (err, data, response) {
        if (data) {
            data.forEach(function (mention) {
                replyMentions = tweetURL(mention.user.screen_name, mention.id_str);
                T.post('statuses/update', { status: replyMentions, in_reply_to_status_id: mention.id_str }, function (err, data, response) {
                })
            })
        }
    })
}