A .NET 8 console application that integrates Gmail with Telegram using OAuth 2.0 authentication. Each user authenticates their own Gmail account through the bot, which then forwards new inbox messages to their Telegram chat with interactive action buttons.
- π OAuth 2.0 Authentication - Per-user Gmail authentication via web flow
- π¨ Email Forwarding - Automatically forwards new inbox messages to Telegram
- ποΈ Interactive Buttons - Delete, Archive, Star, and Forward actions
- π·οΈ Label Management - View and modify Gmail labels
- πΎ SQLite Persistence - Stores messages, actions, and user credentials
- π Automatic Token Refresh - Handles expired access tokens seamlessly
- π Direct Links - Quick access to open emails in Gmail
- π Action History - Tracks all user actions on messages
- π‘οΈ CSRF Protection - Secure OAuth state validation
- β‘ Resilient Polling - Retries with exponential backoff
- .NET 8 SDK
- Telegram Bot Token (from @BotFather)
- Google Cloud Project with Gmail API enabled
# Clone the repository
git clone https://github.com/guberm/gmail-telegram-bot-csharp.git
cd gmail-telegram-bot-csharp
# Restore dependencies
dotnet restore
# Copy settings template
cp settings.json.template settings.json # Linux/Mac
copy settings.json.template settings.json # Windows
-
Create Google OAuth Credentials
- Go to Google Cloud Console
- Enable Gmail API
- Create OAuth 2.0 Client ID (Web application)
- Add redirect URI:
http://localhost:8080/oauth/callback
- Copy the Client ID and Client Secret
-
Get Telegram Bot Token
- Message @BotFather on Telegram
- Create a new bot with
/newbot
- Copy the bot token
-
Update
settings.json
{
"telegram_bot_token": "YOUR_BOT_TOKEN_FROM_BOTFATHER",
"google_client_id": "YOUR_GOOGLE_CLIENT_ID",
"google_client_secret": "YOUR_GOOGLE_CLIENT_SECRET",
"polling_interval_seconds": 60,
"database_path": "telegram_gmail.db",
"gmail_scopes": [
"https://www.googleapis.com/auth/gmail.readonly",
"https://www.googleapis.com/auth/gmail.modify"
],
"oauth_callback_url": "http://localhost:8080/oauth/callback",
"oauth_callback_port": 8080,
"application_name": "Telegram Gmail Bot"
}
Important:
β οΈ Never commitsettings.json
to version control- π Each user authenticates individually - no shared credentials
- π See OAUTH_SETUP.md for detailed setup instructions
dotnet run
The bot will:
- β Validate configuration (Telegram token + Google OAuth credentials)
- ποΈ Initialize SQLite database
- π Start OAuth callback server on port 8080
- π€ Start Telegram bot
- β³ Wait for users to authenticate via
/start
command
- Start the bot - Send
/start
to your bot on Telegram - Click the OAuth link - Bot responds with "π Connect Gmail Account" button
- Authorize on Google - Grant Gmail permissions in your browser
- Success! - You'll be redirected and the bot confirms connection
- Receive emails - New Gmail messages appear in your Telegram chat
Command | Description |
---|---|
/start |
Connect your Gmail account via OAuth authentication |
/status |
Check your Gmail connection status and details |
/disconnect |
Revoke access and delete stored credentials |
/help |
Show available commands and detailed usage guide |
Each forwarded email includes inline buttons:
- ποΈ Delete - Move email to trash
- π¦ Archive - Remove from inbox (keep in All Mail)
- β Star - Add star to email
- β‘οΈ Forward - Forward email to another address (coming soon)
βββββββββββ OAuth Link ββββββββββββ
β Telegram βββββββββββββββββββββΊβ Google OAuth β
β Bot β β Server β
βββββββββββ ββββββββββββ
β β
β Token Callback β Auth Code
βββββββββββββββββββββββββββββββββββββ
β
β Store Credentials (SQLite)
β
β Fetch Emails (Gmail API)
β
β Forward to User (Telegram)
Stores OAuth tokens per user
chat_id, access_token, refresh_token, expires_at, created_at, updated_at, email_address
CSRF protection for OAuth flow
state, chat_id, created_at, expires_at
Email message cache
message_id, subject, sender, received_datetime, content, attachments, labels, direct_link, is_read, telegram_message_id
User action history
id, message_id, action_type, action_timestamp, user_id, new_label_values
- π€ Per-user OAuth - No shared credentials
- π‘οΈ CSRF Protection - Random state validation
- π Token Encryption - Secure storage in SQLite
- π Automatic Refresh - Expired tokens handled transparently
- π« User Revocation - Users can disconnect anytime
- β° State Expiration - OAuth states expire after 10 minutes
- OAuth Setup Guide - Detailed OAuth configuration and troubleshooting
- Contributing Guidelines - How to contribute to the project
- License - MIT License terms
dotnet build -c Release
dotnet test
gmail-telegram-bot-csharp/
βββ Models/ # Data models
β βββ AppSettings.cs
β βββ UserCredentials.cs
β βββ OAuthState.cs
βββ Services/ # Business logic
β βββ OAuthService.cs
β βββ OAuthCallbackServer.cs
β βββ GmailClient.cs
β βββ TelegramBotService.cs
βββ DatabaseService.cs # SQLite persistence
βββ Program.cs # Entry point
βββ tests/ # Unit tests
- Complete Forward implementation - Send emails to other addresses
- Multi-account support - Multiple Gmail accounts per user
- Enhanced label management - Advanced label operations
- Attachment handling - Download and send attachments via Telegram
- Search functionality - Search emails via bot commands
- Email composition - Send new emails from Telegram
- Filters & Rules - Custom email filtering rules
- Notification customization - Per-user notification preferences
- Web dashboard - Optional web interface for management
Contributions are welcome! Please see CONTRIBUTING.md for:
- Code style guidelines
- Pull request process
- Development setup
- Testing requirements
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with .NET 8
- Uses Google Gmail API
- Powered by Telegram.Bot
- Data persistence with SQLite
- Issues: GitHub Issues
- Discussions: GitHub Discussions
settings.json
, telegram_gmail.db
, or any files containing credentials to version control!