简体   繁体   English

在Twitter中使用node.js API回复Twitter的直接消息

[英]Reply to direct messages of twitter using node.js API in Twitter

I am using twit npm package to read direct messages 我正在使用twit npm包读取直接消息

var stream = T.stream('user', { stringify_friend_ids: true })
stream.on('direct_message', function (directMsg) {
console.log(directMsg)
}

I want to reply the directMsg received , is there any method call of twit which can be used to achieve or any other nodejs package recommended to achieve the same. 我想回复收到的directMsg ,是否可以使用directMsg任何方法调用来实现,或者建议使用任何其他的nodejs包来实现该方法。

Can't provide the exact code solution, but the way is to implement the following steps 无法提供确切的代码解决方案,但方法是实施以下步骤

  1. Get USER_ID property from directMsg object directMsg对象获取USER_ID属性
  2. Use Twit Rest API method T.post(path, [params], callback) to send direct message to the user with USER_ID 使用Twit Rest API方法T.post(path, [params], callback)通过USER_ID向用户发送直接消息
  3. Use Twitter API documentation to understand what you need to send direct messages, you need to properly provide parameters like in documentation 使用Twitter API文档来了解发送直接消息所需的内容,您需要像文档中那样正确提供参数

在此处输入图片说明

So the code will look like this 所以代码看起来像这样

T.post("direct_messages/new", {
    user_id: USER_ID, // USER_ID is parameter from directMsg object
    text: 'YOUR_REPLY'
});

Hope it will help you. 希望对您有帮助。

Even i ran into it, here is what you have to do. 即使我遇到了,这也是您要做的。 Listen to the user stream for direct_message and retrieve the sender.screen_name and recipient.screen_name and verify it's not equal + you need the sender.id as stated by @RashadIbrahimov above. 侦听user流中的direct_message并检索sender.screen_namerecipient.screen_name sender.screen_name并确认它不相等+您需要上面sender.id所述的sender.id。

  // Reply to Twitter messages
 function replyToDirectMessage(){

  //get the user stream
  var stream = T.stream('user');

stream.on('direct_message', function (eventMsg) {
  var msg = eventMsg.direct_message.text;
  var screenName = eventMsg.direct_message.sender.screen_name;
  var userId = eventMsg.direct_message.sender.id;

  // reply object
  var replyTo = { user_id: userId,
    text: "Thanks for your message :)", 
    screen_name: screenName };

  console.log(screenName + " says: " + msg );

  // avoid replying to yourself when the recipient is you
  if(screenName != eventMsg.direct_message.recipient_screen_name){

    //post reply
    T.post("direct_messages/new",replyTo, function(err,data,response){
            console.info(data);
        });
    }
});
}

and call it replyToDirectMessage(); 并将其称为replyToDirectMessage();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM