llm メール返信bot

📨 受信メール

🧠 LLM(GPT / Gemini / Claude)
├── メールの要約
├── 返信意図の理解
└── 自動返信文の生成

✉️ 返信メール送信(Gmail API / Outlook API)

import os
import base64
from googleapiclient.discovery import build
from google.oauth2.credentials import Credentials
from openai import OpenAI

# --- LLM設定 ---
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

# --- Gmail設定 ---
creds = Credentials.from_authorized_user_file("token.json", ["https://www.googleapis.com/auth/gmail.modify"])
service = build("gmail", "v1", credentials=creds)

# 直近メール取得
results = service.users().messages().list(userId="me", maxResults=1, q="in:inbox").execute()
message = service.users().messages().get(userId="me", id=results["messages"][0]["id"], format="full").execute()
email_data = base64.urlsafe_b64decode(message["payload"]["parts"][0]["body"]["data"]).decode()

# LLMに返信文生成を依頼
prompt = f"""
以下のメールに対して、丁寧で簡潔な返信を日本語で作成してください。

--- メール本文 ---
{email_data}
"""
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": prompt}]
)

reply_text = response.choices[0].message.content
print("=== 返信案 ===")
print(reply_text)