【问题标题】:I would like to reply to a 'target' tweet(s) using Twit on Node.js: But I have no clue?我想在 Node.js 上使用 Twit 回复“目标”推文:但我不知道?
【发布时间】:2017-07-19 06:40:51
【问题描述】:

所以目前推特机器人是;


var fs = require('fs'),
    path = require('path'),
    Twit = require('twit'),
    config = require(path.join(__dirname, 'config.js'));
    var T = new Twit(config);

function pick_random_countermagic(){
  var countermagic = [
    'Force-of-Will.jpg',
    'Cryptic-Commad.jpg',
    'Counterspell.jpg',
  ];
  return countermagic[Math.floor(Math.random() * countermagic.length)];
}

function upload_random_image(){
  console.log('Opening an image...');
  var image_path = path.join(__dirname, '/countermagic/' + pick_random_countermagic()),
      b64content = fs.readFileSync(image_path, { encoding: 'base64' });

  console.log('Uploading an image...');

  T.post('media/upload', { media_data: b64content }, function (err, data, response) {
    if (err){
      console.log('ERROR');
      console.log(err);
    }
    else{
      console.log('Uploaded an image!');

      T.post('statuses/update', {
        media_ids: new Array(data.media_id_string)
      },
        function(err, data, response) {
          if (err){
            console.log('Error!');
            console.log(err);
          }
          else{
            console.log('Posted an image!');
          }
        }
      );
    }
  });
}

setInterval(
  upload_random_image,
  10000
);

它目前所做的一切都是随机发布一张图片,这正是我想要的,但我希望它在该机器人发布推文时,或者当机器人正在积极回复机器人已发布但我的机器人尚未回复的所有推文......如果你知道我的意思。

这是我第一次制作 twitter 机器人,从技术上讲,这是我第一次真正使用 javascript 和 node.js。所以,是的,我只是迷路了。所以是的,任何帮助都会非常有帮助。

【问题讨论】:

    标签: javascript node.js twitter


    【解决方案1】:

    您可以使用statuses/mentions_timeline API 检索提及并使用statuses/update API 回复它们,方法是在in_reply_to_status_id 中传递提及的ID。这是一个简单的例子:

    function replyMentions() {
        // check for mentions
        // you can add parameter 'since_id' to limit returns
        T.get('statuses/mentions_timeline', { count: 100 }, function(err, data, response) {
          if (data) {
              data.forEach(function(mention) {
                 // reply if mention.id_str is not yet replied
                 reply = '@' + mention.user.screen_name + ' thanks for reaching out!'
                 T.post('statuses/update', { status: reply, in_reply_to_status_id: mention.id_str }, function(err, data, response) {
                   console.log(data)
                   // mark data.id_str as replied
                 })
              })
          }
        })
    }
    

    您可以添加一些额外的逻辑来将回复的id_str 存储在数据库中,并使用它们来避免冗余回复和限制提及检索(使用since_id 参数)。

    【讨论】:

    • 太棒了,非常感谢,我今晚会尝试实现这个。回来你评论结果。
    • 所以这就是我想要的;但不完全是,我希望它回复机器人未回复的最新推文,具体用户未提及。 var nameID = tweet.id_str; var name = tweet.user.screen_name; T.post('statuses/update', {in_reply_to_status_id: nameID, status: reply + ' ' + number + '@' + name}, function(err, data, response) { 类似这样的东西;但是有图片。
    • 正如我所提到的,twitter 并没有给出一个简单的标志/标记来说明提及是否已被回复。因此,您可以在回复或致电statuses/user_timeline 以查看您自己的推文并查看in_reply_to_status_id_str 时进行跟踪。至于图片,您必须先使用media/upload 上传它,然后在statuses/updatemedia_ids 中包含media_id
    • 嗯,这很烦人。呃......无论如何,谢谢你和一些资源的方向。呃,我会想到一些事情。
    猜你喜欢
    • 2016-01-09
    • 2017-11-23
    • 2021-05-13
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    • 1970-01-01
    相关资源
    最近更新 更多