[英]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 无法提供确切的代码解决方案,但方法是实施以下步骤
USER_ID
property from directMsg
object 从directMsg
对象获取USER_ID
属性 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
向用户发送直接消息 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_name
和recipient.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.