Skip to main content

Instagram DM bot powered by Google Gemini AI

Project description

Instagram Gemini Bot ๐Ÿค–

A Python package to build intelligent Instagram DM bots powered by Google Gemini AI. Automatically respond to messages with personalized, context-aware replies.

Features โœจ

  • ๐Ÿค– AI-Powered Responses: Uses Google Gemini for intelligent message generation
  • ๐Ÿ’ฌ Context Memory: Remembers conversation history for each user
  • ๐Ÿ”ง Easy Configuration: Simple CLI-based setup
  • ๐Ÿš€ Production Ready: Deploy to Azure, AWS, Heroku, or any server
  • ๐Ÿ“ฆ Installable Package: Install via pip, deploy anywhere
  • ๐ŸŽญ Customizable Personality: Define bot behavior with custom instructions
  • ๐Ÿ” Secure: Environment-based configuration
  • ๐Ÿ“Š SQLite Storage: Persistent conversation storage

Quick Start ๐Ÿš€

Installation

# From PyPI (when published)
pip install instachatdmbot

# Or from source
git clone https://github.com/23f2001706/insta
cd insta
pip install -e .

Setup

  1. Get your credentials:

  2. Initialize the bot (interactive setup):

    insta-bot init
    

    This creates a .env file with all required configuration.

  3. Verify configuration:

    insta-bot validate
    
  4. Run locally:

    python main.py
    

Configuration ๐Ÿ“‹

The bot uses environment variables stored in .env:

# Instagram Configuration
INSTAGRAM_APP_ID=your_app_id
INSTAGRAM_APP_SECRET=your_app_secret
INSTAGRAM_ACCESS_TOKEN=your_access_token
VERIFY_TOKEN=your_webhook_verify_token
BOT_PAGE_ID=your_page_id
BOT_INSTAGRAM_ID=your_instagram_id

# Gemini Configuration
GEMINI_API_KEY=your_gemini_key
GEMINI_MODEL=gemini-2.5-flash-lite

# Bot Personality
BOT_NAME=MyBot
BOT_INSTRUCTIONS=You are a helpful Instagram assistant...

# Database
DB_PATH=./conversations.db

Getting Credentials

Instagram Credentials

  1. Go to Meta App Dashboard
  2. Create or select your app
  3. Add Instagram as a product
  4. Go to Settings โ†’ Basic to find App ID and App Secret
  5. Create a Page Access Token in Messenger settings
  6. In your app, go to Messenger โ†’ Settings and set webhook URL to:
    https://yourdomain.com/webhook
    

Gemini API Key

  1. Visit Google AI Studio
  2. Click "Create API Key"
  3. Copy the key to your .env

Verify Token

Create your own arbitrary token (e.g., my_super_secret_token_123) and use the same value in:

  • .env file as VERIFY_TOKEN
  • Instagram webhook settings as "Verify Token"

Usage ๐Ÿ“–

As a Python Package

from insta_bot import InstagramBot

# Create bot with default configurations from .env
bot = InstagramBot()

# Run on port 8000
bot.run(host="0.0.0.0", port=8000)

Custom Configuration

from insta_bot import InstagramBot

# Create with custom instructions
custom_instructions = """
You are a friendly pizza shop assistant.
You help customers order pizza and answer questions.
Keep responses under 100 characters.
"""

bot = InstagramBot(
    custom_instructions=custom_instructions,
    gemini_model="gemini-2.5-flash-lite"
)

bot.run()

CLI Commands

# Interactive setup
instachatdmbot init

# Validate configuration
instachatdmbot validate

# Run the bot
instachatdmbot run --host 0.0.0.0 --port 8000

Webhook Setup ๐Ÿ”—

Local Testing with ngrok

# Install ngrok
pip install ngrok

# In another terminal
ngrok http 8000

# Get the HTTPS URL (e.g., https://abc123.ngrok.io)

Azure Deployment

  1. Create Azure App Service (Python 3.10)
  2. Configure environment variables in Azure portal
  3. Set startup command:
    gunicorn -w 4 -b 0.0.0.0:8000 main:bot.app
    
  4. In Instagram settings, set webhook URL to your Azure domain

AWS Deployment

  1. Create AWS Lambda with Python 3.10 runtime
  2. Use AWS API Gateway to create HTTP endpoint
  3. Set environment variables in Lambda configuration
  4. Deploy using sam or serverless framework

Project Structure ๐Ÿ“

insta-bot-gemini/
โ”œโ”€โ”€ insta_bot/                 # Main package
โ”‚   โ”œโ”€โ”€ __init__.py           # Package exports
โ”‚   โ”œโ”€โ”€ bot.py                # Main bot class
โ”‚   โ”œโ”€โ”€ config.py             # Configuration management
โ”‚   โ”œโ”€โ”€ instagram_api.py       # Instagram Graph API wrapper
โ”‚   โ”œโ”€โ”€ gemini_handler.py      # Gemini AI integration
โ”‚   โ”œโ”€โ”€ conversation_store.py  # SQLite conversation storage
โ”‚   โ””โ”€โ”€ cli.py                # CLI commands
โ”œโ”€โ”€ main.py                    # Entry point
โ”œโ”€โ”€ setup.py                   # Package setup
โ”œโ”€โ”€ pyproject.toml            # Modern Python packaging
โ”œโ”€โ”€ requirements.txt          # Python dependencies
โ”œโ”€โ”€ .env.example              # Example configuration
โ””โ”€โ”€ README.md                 # This file

How It Works ๐Ÿ”„

Instagram DM โ†’ Webhook โ†’ Bot receives message
  โ†“
Bot fetches conversation history from SQLite
  โ†“
Message sent to Gemini with system prompt
  โ†“
Gemini generates response
  โ†“
Response saved to conversation history
  โ†“
Response sent back via Instagram API

Example Responses ๐Ÿ’ฌ

Default Assistant

User: What's the weather?
Bot: I'm an Instagram assistant, not a weather bot! But I can help with other things. What else can I help with?

Custom: Pizza Shop Bot

User: Can I order pizza?
Bot: Absolutely! We have margherita, pepperoni, and more. What size would you like? ๐Ÿ•

Troubleshooting ๐Ÿ”ง

"403 Forbidden" on webhook verification

  • Check VERIFY_TOKEN matches in both .env and Instagram settings
  • Ensure Flask is listening on port 8000

"Gemini API error"

  • Check GEMINI_API_KEY is valid and active
  • Ensure API is enabled in Google Cloud Console

"No messages received"

  • Check webhook URL is publicly accessible (HTTPS)
  • Verify bot has permission to send messages in Meta dashboard
  • Check Instagram app ID and access token

"Messages not stored"

  • Check conversations.db file is writable
  • Ensure SQLite3 is installed: python -c "import sqlite3"

Deployment ๐Ÿš€

Heroku

git push heroku main

# Set environment variables
heroku config:set GEMINI_API_KEY=your_key
heroku config:set INSTAGRAM_ACCESS_TOKEN=your_token

Azure

az webapp create --name my-bot --resource-group mygroup --plan myplan --runtime python|3.10

# Configure in Azure portal:
# 1. Go to Configuration โ†’ Application Settings
# 2. Add all variables from .env
# 3. Save

Docker

FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:8000", "main:bot.app"]

Performance Tips ๐Ÿƒ

  1. Use flash model: GEMINI_MODEL=gemini-2.5-flash-lite (faster, cheaper)
  2. Limit conversation history: Trim old messages in conversation_store.py
  3. Cache responses: Add Redis for frequently asked questions
  4. Batch responses: Use quick replies instead of text

Security โš ๏ธ

  • Never commit .env file to git (already in .gitignore)
  • Keep API keys secret
  • Use strong VERIFY_TOKEN
  • Validate webhook signatures (todo)
  • Rate-limit responses to prevent abuse

Contributing ๐Ÿค

Contributions welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Open a Pull Request

License ๐Ÿ“„

MIT License - see LICENSE file for details

Support ๐Ÿ’ฌ

Roadmap ๐Ÿ—บ๏ธ

  • Rate limiting
  • Webhook signature validation
  • Redis caching
  • Multi-language support
  • Image recognition
  • Admin dashboard
  • Analytics and metrics

Made with โค๏ธ for Instagram automation

An automated chatbot for Instagram Direct Messages powered by Google Gemini AI.

Features

  • Instagram Integration: Automatically responds to DMs on Instagram
  • Gemini AI: Leverages Google's Gemini AI for intelligent responses
  • Conversation Memory: Stores conversation history with users
  • Configurable: Easy-to-use configuration system

Setup

Prerequisites

  • Python 3.8+
  • Instagram account
  • Google Gemini API key

Installation

  1. Clone the repository
git clone <repository-url>
cd insta
  1. Install dependencies
pip install -r requirements.txt
  1. Configure environment variables
# Edit .env file with your credentials
cp .env.example .env
# Edit .env with your Instagram and Gemini API credentials
  1. Run the application
python app.py

Configuration

Edit the .env file with your settings:

  • INSTAGRAM_USERNAME: Your Instagram username
  • INSTAGRAM_PASSWORD: Your Instagram password
  • GEMINI_API_KEY: Your Google Gemini API key
  • DEBUG: Enable debug logging (True/False)
  • LOG_LEVEL: Logging level (INFO, DEBUG, ERROR)

Project Structure

  • app.py: Main application entry point
  • config.py: Configuration management
  • instagram_api.py: Instagram API wrapper
  • gemini_handler.py: Gemini AI handler
  • conversation_store.py: Conversation storage and retrieval
  • requirements.txt: Python dependencies
  • .env: Environment variables (keep this private!)

Usage

The bot will:

  1. Listen for incoming Instagram DMs
  2. Process each message with Gemini AI
  3. Generate personalized responses
  4. Store conversation history for context

License

MIT

Security Notes

  • Never commit .env file to version control
  • Keep your API keys and passwords secure
  • Use environment variables for sensitive data
  • OAuth is recommended instead of password-based authentication

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

instachatdmbot-1.0.0.tar.gz (22.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

instachatdmbot-1.0.0-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file instachatdmbot-1.0.0.tar.gz.

File metadata

  • Download URL: instachatdmbot-1.0.0.tar.gz
  • Upload date:
  • Size: 22.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for instachatdmbot-1.0.0.tar.gz
Algorithm Hash digest
SHA256 36fd8858cea9401518976cad94954e8e9b371c4b66da362414e3f0d7ce68a493
MD5 83ff4849f41b95c38e2a67c74058982e
BLAKE2b-256 c8b1918ee05d403acc623a4a06a51a9578369336f43084ea7cadf2b41f31f4c8

See more details on using hashes here.

File details

Details for the file instachatdmbot-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: instachatdmbot-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 14.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for instachatdmbot-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4574ab93543cc1705ce426c8eba8c127278fad54b176a1058292b296de43e21f
MD5 16d78fbb2f930cafbe7ae88a453dfb2d
BLAKE2b-256 cf951ec991f228a8ebd995a7bc304dc28b9eaf55ab5b635867eee8c90b3990d2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page