This project implements a simple Telegram bot that provides real-time notifications for new Gmail messages across multiple accounts per user. It's designed to help users efficiently manage their email inboxes without constantly checking multiple Gmail accounts.
- 🔗 Multi-account Support: Monitor multiple Gmail accounts for each user.
- ⚡ Real-time Notifications: Receive instant notifications on Telegram when new emails arrive.
- 🧠 AI-powered Email Analysis: Utilizes OpenAI's GPT-4 to categorize emails, summarize content, and suggest action steps.
- 🎚️ Importance Rating: Automatically rates email importance on a scale of 0-5.
- 📝 Message Preview: Get a concise preview of the email content in the Telegram notification.
- 🎯 Quick Actions: Perform basic actions like blacklisting senders or removing notifications directly from Telegram.
- 🔗 Smart Link Handling: Extracts and provides quick access to important URLs in emails.
- 🚫 Spam Protection: Automatically filters out spam and low-importance emails.
- ↩️ Unsubscribe Option: Easily unsubscribe from newsletters directly from the notification.
- 📊 Daily Summary: Receive a daily summary of email activity and important messages.
- 🔧 Easy Setup: Simple setup process with detailed instructions
- Backend: Node.js with TypeScript
- AI Integration: OpenAI API (GPT-4)
- Email Integration: Gmail API (OAuth2 for Gmail accounts)
- Bot Framework: Telegraf (Telegram Bot API)
- Database: MongoDB
- Deployment: Google Cloud Functions, Heroku, or any Node.js hosting service
- User Registration: Users register their Gmail accounts with the bot through a secure OAuth2 process.
- Email Monitoring: The bot uses Gmail's API to watch for new emails in registered accounts.
- Push Notifications: When a new email arrives, Google sends a push notification to the bot's endpoint.
- Email Retrieval: The bot fetches the new email details using the Gmail API.
- AI Analysis: The email is analyzed using OpenAI's GPT-4 model to categorize, summarize, and rate its importance.
- Notification Creation: Based on the AI analysis, a Telegram message is crafted with relevant information and action buttons.
- Message Delivery: The notification is sent to the user's designated Telegram chat(s).
- User Interaction: Users can interact with the notification to perform quick actions or view more details.
- Daily Summary: Users receive a daily summary of email activity and important messages at 8AM and 8PM local time.
-
Create a new project in the Google Cloud Console.
-
Enable the Gmail API for your project:
- Go to the API Library.
- Search for "Gmail API" and enable it.
-
Create credentials:
- Go to the Credentials page.
- Click "Create Credentials" and select "OAuth client ID".
- Choose "Web application" as the application type.
- Add authorized redirect URIs (include your bot's callback URL).
- Download the client configuration file (json).
-
Add JSON credentials to your project:
- Open .env
- Add the following environment variables:
GOOGLE_CREDENTIALS="{"installed":{"client_id":"....","project_id":"....","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"....","redirect_uris":["https://..."],"javascript_origins":["https://..."]}}"
-
Create a Pub/Sub topic:
- Go to the Pub/Sub topics page.
- Click "Create Topic".
- Name your topic (e.g., "gmail-notifications").
- Click "Create".
-
Grant Gmail permission to publish to the Pub/Sub topic:
- On the topic details page, click "Add Principal" in the Permissions tab.
- In the "New Principals" box, enter:
[email protected]
- In the "Assign Roles" section, search for and select: "Pub/Sub Publisher"
- Click "Save".
-
Update your
.env
file with the Pub/Sub topic name:PUB_SUB_TOPIC=projects/your-project-id/topics/gmail-notifications
Replace
your-project-id
with your Google Cloud project ID andgmail-notifications
with your topic name if different. -
Set up a Cloud Function to handle Gmail push notifications:
- Go to the Cloud Functions page.
- Click "Create Function".
- Set the trigger to Cloud Pub/Sub and select your created topic.
- Use the following code for the function:
const functions = require('@google-cloud/functions-framework'); functions.cloudEvent('helloPubSub', cloudEvent => { fetch("https://<YOUR_PRIVATE_HOST>/ggle", { method: "POST", body: JSON.stringify(cloudEvent.data) }) });
-
Replace
YOUR_PRIVATE_HOST
with your bot's API endpoint. -
Deploy the function and note the function's URL.
-
Clone this repository:
git clone https://github.com/hormold/gmail-notification-bot.git cd gmail-notification-bot
-
Install dependencies:
npm install
-
Create a
.env
file in the project root with the following content:BOT_TOKEN=your_telegram_bot_token SERVER_PATH=your_endpoint_domain!.com MONGODB_URI=your_mongodb_connection_string OPENAI_API_KEY=your_openai_api_key GOOGLE_CREDENTIALS={"installed":{"client_id":"....","project_id":"....","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"....","redirect_uris":["https://..."],"javascript_origins":["https://..."]}} PUB_SUB_TOPIC=projects/your-project-id/topics/gmail-notifications
Replace the placeholders with your actual credentials and configuration.
-
Set up the database:
- Create a MongoDB database and note the connection string.
- Update the
MONGODB_URI
in the.env
file.
-
Start the bot:
npm start
- Start a chat with your Telegram bot.
- Use the
/connect
command to link a Gmail account and then/new
to receive specific links to connect your Gmail account. - Follow the prompts to authorize the bot to access your Gmail.
- The bot will now notify you of new emails in the connected account(s).
/remove_account
: Remove a connected Gmail account./help
: Display available commands and usage instructions.
- If you're not receiving notifications, ensure that the Gmail Push API is properly set up and the Cloud Function is deployed correctly.
- Check the bot's logs for any error messages or issues with Gmail API authentication.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License.