Skip to content

sofianeoml/Instgram-Comment-Ai-Reply

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Instgram-Comment-Ai-Reply

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.

Project Overview

The bot's primary functions are as follows:

  1. 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.
  2. Persistent Login with Cookies:

    • Saves and loads cookies to maintain a persistent login state, eliminating the need for frequent re-authentication.
  3. 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.
  4. Scheduled Execution:

    • Operates in an infinite loop, checking for new comments on the latest Instagram post every 5 minutes.

How It Works

  • Instagram Authentication:

    • The bot authenticates to Instagram using instagrapi. It saves login cookies to avoid re-authentication in subsequent sessions, enhancing efficiency and reliability.
  • 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.

Example Workflow

  1. User Comment: "I love this new design! Will you be releasing more soon?"
  2. Bot Reply: "[REPLY-AI] Thanks for the support! 😊 Stay tuned; we have more exciting designs on the way!"

Configuration Overview

To get started with this bot, you need:

  1. 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.
  2. 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.
  3. 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).

Key Functions and Code Structure

  • load_cookies() and save_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.

Running the Bot

  1. Run the Reply Bot:

    • Start the bot using:
      python reply_bot.py
  2. Continuous Operation:

    • Once started, the bot will check for new comments every 5 minutes and reply accordingly, logging each reply to MongoDB for tracking.

Future Improvements

  • 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.

License

This project is licensed under the MIT License.

Contact

For questions, feedback, or contributions, feel free to reach out via Instagram or open an issue on this repository.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages