2016-05-20 140 views
0

我有一个功能完整的twitter bot。当他们回复我时,它补充了某人,这是@myTwitterHandle是推文中的第一件事。下面的代码可以让我回应他们:如何使用Twitter API回应提及

function tweetEvent(tweet) { 

    // Who is this in reply to? 
    var reply_to = tweet.in_reply_to_screen_name; 
    // Who sent the tweet? 
    var name = tweet.user.screen_name; 
    // What is the text? 
    var txt = tweet.text; 

    // Ok, if this was in reply to me 
    // Replace myTwitterHandle with your own twitter handle 
    console.log(reply_to, name, txt); 
    if (reply_to === 'myTwitterHandle') { 

    ¦ // Get rid of the @ mention 
    ¦ txt = txt.replace(/@selftwitterhandle/g, ''); 

    ¦ // Start a reply back to the sender 
    ¦ var reply = "You mentioned me! @" + name + ' ' + 'You are super cool!'; 

    ¦ console.log(reply); 
    ¦ // Post that tweet! 
    ¦ T.post('statuses/update', { status: reply }, tweeted); 
    } 
} 

我只想每当有人@mentions我的地方,在他们的鸣叫的身体发出完全相同的答复。我正在使用Node.js和twit api client

回答

0

它看起来就像你可能会引用教程中here

我相信这是你可能会寻找

我只是想送完全相同的答复每当有人@mentions我 某处在他们的推文中。

这个脚本达到期望的结果:

var stream = T.stream('statuses/filter', { track: ['@myTwitterHandle'] }); 
stream.on('tweet', tweetEvent); 

function tweetEvent(tweet) { 

    // Who sent the tweet? 
    var name = tweet.user.screen_name; 
    // What is the text? 
    // var txt = tweet.text; 
    // the status update or tweet ID in which we will reply 
    var nameID = tweet.id_str; 

    // Get rid of the @ mention 
    // var txt = txt.replace(/@myTwitterHandle/g, ""); 

    // Start a reply back to the sender 
    var reply = "You mentioned me! @" + name + ' ' + 'You are super cool!'; 
    var params    = { 
           status: reply, 
           in_reply_to_status_id: nameID 
          }; 

    T.post('statuses/update', params, function(err, data, response) { 
     if (err !== undefined) { 
     console.log(err); 
     } else { 
     console.log('Tweeted: ' + params.status); 
     } 
    }) 
}; 
+0

完美工作,感谢你啊! – JHS

+0

答复已发送,但未通知给用户,也未显示在他们的推特线程中。看到这个问题:https://twittercommunity.com/t/automated-replies-are-not-being-notified-to-users-nor-in-reply-thread/79994 –

+0

@SantoshKumar不知道这是否有帮助,但我是使用Twit包与上面的代码在这里找到:https://github.com/ttezel/twit。另见这里:http://stackoverflow.com/a/39028879/5884189 – filmplane