2016-05-20 1 views
0

У меня есть твитер-бот, который является функциональным. Он дополняет кого-то, когда они отвечают мне, а это когда @myTwitterHandle является первым делом в чириканье. Следующий код позволяет мне реагировать на них:Как отреагировать на упоминания с помощью API Twitter

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

Смежные вопросы