i am facing a problem relating to twitter bot. i was simply trying to get the tweeets with '#topi' mentioned in their status and then send them a reply and also retweet the tweet. this is how i expect to execute the bot.
- wait for tweets
- receives the tweet
- write the tweet to a local file (just to examine)
- reply the tweet with their user_id mentioned
- retweet their tweet
- wait for next tweet
but the problem i am facing is, up to replying and retweeting it's working well, but since it retweeted the received tweet with the same hashtag in the status it repeats the same procedure for the hashtag as before and receives 'duplicate content error'. is there anything quick solution for this.
require("dotenv").config();
const twit = require("twit");
const fs = require("fs");
const path = require("path");
const { resolve } = require("path");
const twitter = new twit({
consumer_key: process.env.TWITTER_API_KEY,
consumer_secret: process.env.TWITTER_API_SECRET,
access_token: process.env.TWITTER_ACCESS_TOKEN,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
});
console.log("starting bot...");
// function getTweets() {
// const getTweetParams = {
// q: "#birkhe",
// count: 10,
// };
// twitter.get("search/tweets", getTweetParams, (err, data, response) => {
// return console.log(data);
// postTweet();
// });
// }
// var Nepal = ["84.1240", "28.3949"];
var stream = twitter.stream("statuses/filter", {
track: "#topi"
// geo: Nepal
});
console.log("waiting for tweets...");
console.log('----------------------');
stream.on("tweet", async (tweet) => {
console.log("writing received tweets to a file...");
fs.writeFileSync(path.join(__dirname, "tweets.json"), JSON.stringify(tweet));
console.log("writing tweet to a file is completed...");
console.log('----------------------');
const user_name = tweet.user.screen_name;
const name = tweet.user.name;
const tweetId = tweet.id_str;
const receivedTewwtText = tweet.text;
console.log("user Name id:", user_name);
console.log("name id:", name);
console.log("tweetId:", tweetId);
console.log("received text:", receivedTewwtText);
const tweetText = `@${user_name} have a good day!`;
try{
await replyTweet(tweetText);
await retweet(tweetId);
}catch(e){
console.log("the Error is ", e);
}
});
function retweet(id) {
return new Promise((reject, resolve) => {
console.log("posting retweet...");
let postRetweetParams = {
id,
};
twitter.post("statuses/retweet/:id", postRetweetParams, (err, data) => {
if (err) {
console.log("there is an error while posting retweet..");
return reject(err);
}
console.log("retweeted successfully...");
console.log('----------------------');
return resolve(data);
});
});
}
function replyTweet(text) {
return new Promise((resolve, reject) => {
console.log("replting tweet...");
let postTweetParams = {
status: text,
};
twitter.post("statuses/update", postTweetParams, (err, data) => {
if (err) {
console.log("there is an error while replting to a tweet...");
return reject(err);
}
console.log("reply tweet ID is, ", data.id_str);
console.log("replting twit completed...");
console.log('----------------------');
return resolve(data);
});
});
}
starting bot...
waiting for tweets...
----------------------
writing received tweets to a file...
writing tweet to a file is completed...
----------------------
user Name id: LaxmanP71300671
name id: Laxman Pokhrel
tweetId: 1410595913265938445
received text: Hello there #topi
replting tweet...
reply tweet ID is, 1410595936984731648
replting twit completed...
----------------------
posting retweet...
retweeted successfully...
----------------------
the Error is {
created_at: 'Thu Jul 01 13:47:34 +0000 2021',
id: 1410595939673206800,
id_str: '1410595939673206792',
text: 'RT @LaxmanP71300671: Hello there #topi',
truncated: false,
entities: {
hashtags: [ [Object] ],
symbols: [],
user_mentions: [ [Object] ],
urls: []
},
source: '<a href="https://help.twitter.com/en/using-twitter/how-to-tweet#source-labels" rel="nofollow">बिर्खे</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,
user: {
id: 1407231034958094300,
id_str: '1407231034958094337',
name: 'बिर्खे',
screen_name: 'webtron_birkhe',
location: 'Kathmandu, Nepal',
description: 'म बिर्खे हुँ । म हजुरको सेवामा चौबिसै घण्टा उपलब्ध छु ।',
url: '/*some url*/',
entities: { url: [Object], description: [Object] },
protected: false,
followers_count: 2,
friends_count: 25,
listed_count: 0,
created_at: 'Tue Jun 22 06:57:05 +0000 2021',
favourites_count: 0,
utc_offset: null,
time_zone: null,
geo_enabled: false,
verified: false,
statuses_count: 9,
lang: null,
contributors_enabled: false,
is_translator: false,
is_translation_enabled: false,
profile_background_color: 'F5F8FA',
profile_background_image_url: null,
profile_background_image_url_https: null,
profile_background_tile: false,
profile_image_url: 'http://pbs.twimg.com/profile_images/1407232001204129793/47DUsEQN_normal.jpg',
profile_image_url_https: 'https://pbs.twimg.com/profile_images/1407232001204129793/47DUsEQN_normal.jpg',
profile_banner_url: 'https://pbs.twimg.com/profile_banners/1407231034958094337/1624973880',
profile_link_color: '1DA1F2',
profile_sidebar_border_color: 'C0DEED',
profile_sidebar_fill_color: 'DDEEF6',
profile_text_color: '333333',
profile_use_background_image: true,
has_extended_profile: true,
default_profile: true,
default_profile_image: false,
following: false,
follow_request_sent: false,
notifications: false,
translator_type: 'none',
withheld_in_countries: []
},
geo: null,
coordinates: null,
place: null,
contributors: null,
retweeted_status: {
created_at: 'Thu Jul 01 13:47:27 +0000 2021',
id: 1410595913265938400,
id_str: '1410595913265938445',
text: 'Hello there #topi',
truncated: false,
entities: { hashtags: [Array], symbols: [], user_mentions: [], urls: [] },
source: '<a href="http://twitter.com/download/android" rel="nofollow">Twitter for Android</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,
user: {
id: 1348101598157840400,
id_str: '1348101598157840384',
name: 'Laxman Pokhrel',
screen_name: 'LaxmanP71300671',
location: 'Nepal',
description: 'rock',
url: null,
entities: [Object],
protected: false,
followers_count: 2,
friends_count: 18,
listed_count: 0,
created_at: 'Sun Jan 10 02:57:39 +0000 2021',
favourites_count: 4,
utc_offset: null,
time_zone: null,
geo_enabled: false,
verified: false,
statuses_count: 6,
lang: null,
contributors_enabled: false,
is_translator: false,
is_translation_enabled: false,
profile_background_color: 'F5F8FA',
profile_background_image_url: null,
profile_background_image_url_https: null,
profile_background_tile: false,
profile_image_url: 'http://pbs.twimg.com/profile_images/1348102053713727489/xpMY6o-d_normal.jpg',
profile_image_url_https: 'https://pbs.twimg.com/profile_images/1348102053713727489/xpMY6o-d_normal.jpg',
profile_banner_url: 'https://pbs.twimg.com/profile_banners/1348101598157840384/1610247852',
profile_link_color: '1DA1F2',
profile_sidebar_border_color: 'C0DEED',
profile_sidebar_fill_color: 'DDEEF6',
profile_text_color: '333333',
profile_use_background_image: true,
has_extended_profile: true,
default_profile: true,
default_profile_image: false,
following: false,
follow_request_sent: false,
notifications: false,
translator_type: 'none',
withheld_in_countries: []
},
geo: null,
coordinates: null,
place: null,
contributors: null,
is_quote_status: false,
retweet_count: 1,
favorite_count: 0,
favorited: false,
retweeted: true,
lang: 'en'
},
is_quote_status: false,
retweet_count: 1,
favorite_count: 0,
favorited: false,
retweeted: true,
lang: 'en'
}
writing received tweets to a file...
writing tweet to a file is completed...
----------------------
user Name id: webtron_birkhe
name id: बिर्खे
tweetId: 1410595939673206792
received text: RT @LaxmanP71300671: Hello there #topi
replting tweet...
reply tweet ID is, 1410595963484270594
replting twit completed...
----------------------
posting retweet...
there is an error while posting retweet..
please help.