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 insta-bot-gemini
# Or from source
git clone https://github.com/yourusername/insta-bot-gemini
cd insta-bot-gemini
pip install -e .
Setup
-
Get your credentials:
- Instagram: App ID, App Secret from Meta App Dashboard
- Gemini: API Key from Google AI Studio
-
Initialize the bot (interactive setup):
insta-bot initThis creates a
.envfile with all required configuration. -
Verify configuration:
insta-bot validate -
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
- Go to Meta App Dashboard
- Create or select your app
- Add Instagram as a product
- Go to Settings โ Basic to find App ID and App Secret
- Create a Page Access Token in Messenger settings
- In your app, go to Messenger โ Settings and set webhook URL to:
https://yourdomain.com/webhook
Gemini API Key
- Visit Google AI Studio
- Click "Create API Key"
- 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:
.envfile asVERIFY_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
insta-bot init
# Validate configuration
insta-bot validate
# Run the bot
insta-bot 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
- Create Azure App Service (Python 3.10)
- Configure environment variables in Azure portal
- Set startup command:
gunicorn -w 4 -b 0.0.0.0:8000 main:bot.app - In Instagram settings, set webhook URL to your Azure domain
AWS Deployment
- Create AWS Lambda with Python 3.10 runtime
- Use AWS API Gateway to create HTTP endpoint
- Set environment variables in Lambda configuration
- Deploy using
samorserverlessframework
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_TOKENmatches in both.envand Instagram settings - Ensure Flask is listening on port 8000
"Gemini API error"
- Check
GEMINI_API_KEYis 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.dbfile 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 ๐
- Use flash model:
GEMINI_MODEL=gemini-2.5-flash-lite(faster, cheaper) - Limit conversation history: Trim old messages in
conversation_store.py - Cache responses: Add Redis for frequently asked questions
- Batch responses: Use quick replies instead of text
Security โ ๏ธ
- Never commit
.envfile 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:
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Open a Pull Request
License ๐
MIT License - see LICENSE file for details
Support ๐ฌ
- ๐ Full Documentation
- ๐ Report Issues
- ๐ฌ Discussions
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
- Clone the repository
git clone <repository-url>
cd insta
- Install dependencies
pip install -r requirements.txt
- Configure environment variables
# Edit .env file with your credentials
cp .env.example .env
# Edit .env with your Instagram and Gemini API credentials
- Run the application
python app.py
Configuration
Edit the .env file with your settings:
INSTAGRAM_USERNAME: Your Instagram usernameINSTAGRAM_PASSWORD: Your Instagram passwordGEMINI_API_KEY: Your Google Gemini API keyDEBUG: Enable debug logging (True/False)LOG_LEVEL: Logging level (INFO, DEBUG, ERROR)
Project Structure
app.py: Main application entry pointconfig.py: Configuration managementinstagram_api.py: Instagram API wrappergemini_handler.py: Gemini AI handlerconversation_store.py: Conversation storage and retrievalrequirements.txt: Python dependencies.env: Environment variables (keep this private!)
Usage
The bot will:
- Listen for incoming Instagram DMs
- Process each message with Gemini AI
- Generate personalized responses
- Store conversation history for context
License
MIT
Security Notes
- Never commit
.envfile 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file insta_bot_gemini-1.0.0.tar.gz.
File metadata
- Download URL: insta_bot_gemini-1.0.0.tar.gz
- Upload date:
- Size: 22.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f5092aac6088709bf9e2c8ed04850be570124da4bd05fe835a77a1e8d5fc8ad
|
|
| MD5 |
ffe3acb4d0a9bdb0a427c5c22fc6dd66
|
|
| BLAKE2b-256 |
3f6d5cf33f3726df008da0eb1c79384ff1a1848c414efb81a2a3a066f4a2db51
|
File details
Details for the file insta_bot_gemini-1.0.0-py3-none-any.whl.
File metadata
- Download URL: insta_bot_gemini-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b8ddb83fb4261d37032643bd0d6f045177ecbe795771ad293bd9945415afe810
|
|
| MD5 |
2cb232ece250251c44e6567232e51db8
|
|
| BLAKE2b-256 |
16010f2be785f2ff44a46548bdbf76e069efee5fd6f5a814488a9ae84ebe4d26
|