Hi,
I’m working on replying to a direct message.
I believe there’s an infinite loop occurring because the stream object is always listening for new messages so as soon as the first message arrives, a reply is posted and the cycle begins.
Do I need to kill the stream then start it back up again once the post has posted?
I’m using the twit package.
Please let me know if you need any other information.
var T = new twitter(keys);
var stream = T.stream('user');
stream.on('direct_message', function(tweet) {
var sender = tweet.direct_message.sender.id_str;
console.log(sender);
tweetIt('Response!', sender);
});
stream.on('error', function(error) {
console.log(error);
});
// post a tweet
function tweetIt(str, id) {
var tweet = {
event: {
type: 'message_create',
message_create: {
target: {
recipient_id: id
},
message_data: {
text: str
}
}
}
};
T.post('direct_messages/events/new', tweet, function(
error,
tweet,
response
) {
if (!error) {
// console.log(response);
console.log('success!');
return;
} else {
console.log('Something went wrong!\n');
console.log(error);
}
});
}