Replying to a tweet using twit and Node.js

允我心安 提交于 2020-12-06 07:26:26

问题


I am using the Twit Node.js API. What I am trying to do is reply to a tweet that matches a certain keyword. I want my reply tweet to show up within the other tweet underneath. Like when you reply via app or website.

My code to do that is here:

var reply = function(tweet) {
  var res = {
    status: 'This is a tweet',
    in_reply_to_status_id: tweet.id_str
  };

  twitter.post('statuses/update', res,
    function(err, data, response) {
      console.log(data);
    }
  );
}

The status is written correctly to the reply JSON but in_reply_to_status_id remains null. The tweet is posted to the bots account but not in reply to the tweet is is supposed to be replying to.

Why dos it not work?

And yes I have tried to write to in_reply_to_status_id_str and I have tryed to make the tweet.id_str a string.

I there anyone who knows what I am missing?

Thank you!

My response JSON is here:

{ created_at: 'Wed Jun 21 07:44:22 +0000 2017',
  id: 877431986071142400,
  id_str: '877431986071142400',
  text: 'This is a tweet',
  truncated: false,
  entities: { hashtags: [], symbols: [], user_mentions: [], urls: [] },
  source: '<a href="https://example.com" rel="nofollow">Spatinator</a>',
  in_reply_to_status_id: null,
  in_reply_to_status_id_str: null,
  in_reply_to_user_id: null,
  in_reply_to_user_id_str: null,
  in_reply_to_screen_name: null,

If you need more of the respone JSON let me know.


回答1:


The solution is to include a mention to tweet.user.screen_name into the status of the response json. Like this it works:

var reply = function(tweet) {
  var res = {
    status: 'This is a tweet @' + tweet.user.screen_name,
    in_reply_to_status_id: '' + tweet.id_str
  };

  twitter.post('statuses/update', res,
    function(err, data, response) {
      console.log(data);
    }
  );
}


来源:https://stackoverflow.com/questions/44669859/replying-to-a-tweet-using-twit-and-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!