Instgram-Comment-Ai-Reply is an AI-powered bot that automatically responds to comments on Instagram posts. Using Google Generative AI for dynamic, context-aware replies, the bot offers an efficient way to interact with followers while ensuring no duplicate responses. It also uses MongoDB to log replied comments and instagrapi
to handle Instagram API interactions.
The bot's primary functions are as follows:
-
Automated Replies:
- Uses Google Generative AI to generate responses based on the content and context of each comment.
- Replies are prefixed with
[REPLY-AI]
to clearly indicate they are generated by the bot.
-
Persistent Login with Cookies:
- Saves and loads cookies to maintain a persistent login state, eliminating the need for frequent re-authentication.
-
Comment Tracking with MongoDB:
- Uses a MongoDB database to log each replied comment's ID, response text, and timestamp, preventing the bot from replying to the same comment multiple times.
-
Scheduled Execution:
- Operates in an infinite loop, checking for new comments on the latest Instagram post every 5 minutes.
-
Instagram Authentication:
- The bot authenticates to Instagram using
instagrapi
. It saves login cookies to avoid re-authentication in subsequent sessions, enhancing efficiency and reliability.
- The bot authenticates to Instagram using
-
Reply Generation:
- For each new comment, the bot generates a custom response using Google Generative AI, designed to match the tone and content of the comment. The response generation is tailored to be friendly, engaging, and informative, adding value to the interaction.
-
MongoDB Logging:
- All replied comments are logged in a MongoDB database, ensuring the bot doesn't respond to the same comment twice. This database includes fields for comment ID, reply text, original comment, and reply timestamp.
-
Scheduling:
- The bot runs continuously, checking the most recent Instagram post for new comments every 5 minutes. This scheduling allows it to provide timely responses without spamming or violating Instagram's rate limits.
- User Comment: "I love this new design! Will you be releasing more soon?"
- Bot Reply: "[REPLY-AI] Thanks for the support! 😊 Stay tuned; we have more exciting designs on the way!"
To get started with this bot, you need:
-
Instagram Login Credentials:
- An Instagram username and password are required for authentication through
instagrapi
. These credentials allow the bot to access the latest post and comments.
- An Instagram username and password are required for authentication through
-
Google Generative AI API Key:
- An API key from Google Generative AI is used to power the reply generation feature. This allows the bot to craft thoughtful responses based on comment content.
-
MongoDB Setup:
- MongoDB is used to store comment logs, enabling persistent tracking of replied comments. Ensure MongoDB is installed and running locally (or adjust the database connection if using a remote MongoDB instance).
load_cookies()
andsave_cookies()
: Functions to load and save cookies for maintaining a logged-in session on Instagram.has_replied()
: Checks if a comment has already been replied to by querying the MongoDB database.generate_reply()
: Calls Google Generative AI to produce a suitable reply for each comment.reply_to_latest_post_comments()
: Main function that retrieves comments on the latest post, filters out already-replied comments, and posts AI-generated replies.
-
Run the Reply Bot:
- Start the bot using:
python reply_bot.py
- Start the bot using:
-
Continuous Operation:
- Once started, the bot will check for new comments every 5 minutes and reply accordingly, logging each reply to MongoDB for tracking.
- Additional Customization: Adding user-configurable reply templates for different types of comments.
- Enhanced Sentiment Analysis: Incorporating sentiment analysis for tone adjustment based on comment positivity or negativity.
- Error Handling: Improved exception handling to cover Instagram API limitations and MongoDB connectivity issues.
This project is licensed under the MIT License.
For questions, feedback, or contributions, feel free to reach out via Instagram or open an issue on this repository.