A Flask-based LINE Bot that integrates with Google's Gemini AI to create an intelligent chatbot. This bot can handle text messages and images, maintaining conversation context and supporting multiple languages.
- Integration with LINE Messaging API
- Powered by Google's Gemini AI
- Multi-language support
- Image processing capabilities
- Conversation context management
- Redis-based caching (optional)
- Easy deployment to Heroku
- Python 3.8+
- LINE Developer Account
- Google AI API Key
- Heroku Account (for deployment)
-
LINE Channel Setup
- Go to the LINE Developers site and create a LINE developer account.
- Create a new Messaging API channel.
- Go to the channel settings and get the Channel Secret and Channel Access Token.
- How to get the Channel Secret and Channel Access Token:
- Log in to the LINE Developers Console.
- Select the Messaging API channel you created.
- You can find the Channel Secret in the "Basic settings" section of your channel settings.
- You can find the Channel Access Token in the "Messaging API" section of your channel settings. In the "Channel access token" section, click the "Issue" button to issue a token.
- Keep the issued tokens safe. These tokens are used by the bot to communicate with the LINE API.
- How to get the Channel Secret and Channel Access Token:
-
Google AI Setup
- Go to Google AI Studio to create a Google AI project and generate an API key.
-
Local Development
# Clone the repository git clone https://github.com/planetarium/line-gemini-bot.git cd line-gemini-bot # Create and activate virtual environment python -m venv venv source venv/bin/activate # For Windows: venv\Scripts\activate # Install dependencies pip install -r requirements.txt # Set up environment variables cp .env.example .env
-
Configure Environment Variables Edit the
.env
file with your credentials:LINE_CHANNEL_ACCESS_TOKEN=your_line_channel_access_token LINE_CHANNEL_SECRET=your_line_channel_secret GOOGLE_API_KEY=your_google_ai_api_key REDIS_URL=your_redis_url (optional)
-
Configure Application Settings Copy the example configuration files:
cp config.py.example config.py cp system_instruction.txt.example system_instruction.txt
Customize
config.py
for your needs:- Adjust
MAX_FILE_SIZE
if needed - Modify
FIXED_MESSAGES
for different languages - Add or remove language support
Customize
system_instruction.txt
to define your AI assistant's behavior and personality. - Adjust
-
Running the Server Locally
python app.py
This will start the Flask development server. You can access the bot locally, typically at
http://localhost:5000
.
- Click the "Deploy to Heroku" button above
- Fill in the required environment variables
- Deploy the application
- Set the webhook URL in your LINE Channel settings to:
https://your-app-name.herokuapp.com/callback
-
Create a new Heroku app
heroku create your-app-name
-
Set environment variables
heroku config:set LINE_CHANNEL_ACCESS_TOKEN=your_token heroku config:set LINE_CHANNEL_SECRET=your_secret heroku config:set GOOGLE_API_KEY=your_key
-
Deploy the application
git push heroku main
Create a system_instruction.txt
file to customize the AI's behavior: