Automates drafting and replying to source request emails (HARO and Help a B2B Writer) with a Gemini-generated draft and a Telegram approval step. Approved drafts are sent via Gmail with threading headers. Includes robust logging and SQLite persistence.
- Gmail OAuth2 (token.json) and label-based polling (default
HARO/HelpAB2BWriter) - Parses sender, deadline, requirements, and full request text
- Draft generation via Google Gemini with reusable template
- Telegram review with inline buttons: Approve & Send, Edit Draft, Reject
- Proper Gmail threading (
In-Reply-To,References,threadId) - SQLite storage, structured logs, exponential backoff retries
- Python 3.10+
- Create and activate a virtual environment.
- Install dependencies:
pip install -r requirements.txt
- Copy env template and fill values:
cp env.example .env
- In Google Cloud Console, enable the Gmail API.
- Create OAuth 2.0 Client ID (Desktop app).
- Download JSON as
credentials.jsonto the project root (or setGMAIL_CREDENTIALS_FILE). - First run will open a browser for consent and write
token.json. - In Gmail, create/apply a label (default
HARO/HelpAB2BWriter) to relevant emails.
- Get an API key and set
GEMINI_API_KEYin.env. - Optional: customize
templates/gemini_prompt_template.md.
- Create a bot via BotFather, copy the token to
TELEGRAM_BOT_TOKEN. - Get your chat id (e.g., message the bot and use
getUpdates, or use @userinfobot). - Set
TELEGRAM_CHAT_IDto the chat where you want reviews.
python main.pyThe Telegram bot starts; every POLL_INTERVAL_SECONDS it checks Gmail for messages with the configured label. New requests are parsed, drafted, and sent to Telegram for review.
- Approve & Send: Sends the reply via Gmail and marks the request as
sent. - Edit Draft: Reply in this format to update the draft:
Subject: <your subject> Body: <your body> - Reject: Marks as
rejectedand does not send.
GMAIL_CREDENTIALS_FILE: Path to OAuth client secrets (defaultcredentials.json).GMAIL_TOKEN_FILE: Path to saved token (defaulttoken.json).GMAIL_LABEL_NAME: Gmail label to monitor (defaultHARO/HelpAB2BWriter).GEMINI_API_KEY: Google Gemini API key.GEMINI_MODEL: Gemini model (defaultgemini-1.5-pro).GEMINI_PROMPT_TEMPLATE_PATH: Prompt template path.TELEGRAM_BOT_TOKEN: Telegram bot token.TELEGRAM_CHAT_ID: Chat ID for reviews.DB_PATH: SQLite db path (defaultdata/app.db).LOG_DIR: Log directory (defaultlogs).POLL_INTERVAL_SECONDS: Gmail poll interval (default120).
- SQLite tables:
requests,drafts,telegram_messages,actions_log,pending_edits.
- Missing Gmail permissions: delete
token.jsonand re-run to re-consent. - Telegram edits not applied: ensure the message format includes both
Subject:andBody:. - No new emails: verify the Gmail label exists and is applied to messages.
- Keep
credentials.jsonand.envoutside of VCS. - The script uses least-privilege Gmail scope
gmail.modifyand stores tokens intoken.json.