X の Community Notes AI Writer を、以下 2 つの実行経路に対応させた実装です。
- GitHub Actions から定期/手動実行
- デスクトップアプリ(Tkinter GUI)から実行・結果確認
参考:
- X API Community Notes: https://docs.x.com/x-api/community-notes/introduction
- Template API Note Writer: https://github.com/twitter/communitynotes/tree/main/template-api-note-writer
posts_eligible_for_notesの取得post_selection=feed_lang:jaで固定
- AI でノート下書き生成(OpenAI 互換エンドポイント)
evaluate_noteで事前評価(任意)notesへの投稿(任意)notes_written取得- 実行結果を JSON で保存(
outputs/)
- Python 3.10+
- X API/Community Notes AI Writer の利用資格
- X API キー(ユーザーコンテキスト)
X_API_KEYX_API_KEY_SECRETX_ACCESS_TOKENX_ACCESS_TOKEN_SECRET
- AI 実行環境(下書きを生成する場合)
- xAI API を使う場合:
AI_API_KEY - Claude Code プランを使う場合: VPS 上で Claude Code/Agent SDK が利用できる状態
- xAI API を使う場合:
python -m venv .venv
# Windows PowerShell
.\.venv\Scripts\Activate.ps1
pip install -r requirements.txt
pip install -e ..env.example を .env にコピーして値を設定してください。
AI_PROVIDER=xai
AI_API_KEY=...
AI_BASE_URL=https://api.x.ai/v1
AI_MODEL=grok-3-latestAI_PROVIDER=claude_agent
AI_MODEL=claude-sonnet-4-5
AI_TIMEOUT_SEC=120
CLAUDE_CLI_PATH=claude
CLAUDE_MAX_TURNS=4
CLAUDE_USE_CLI_FALLBACK=true- まず Python の
claude-code-sdkを使って実行します。 - SDK が使えない場合、
CLAUDE_USE_CLI_FALLBACK=trueならclaude --printに自動フォールバックします。 - Claude 経路では
AI_API_KEYは不要です(Claude Code 側の認証状態を利用)。
- VPS で Claude Code の認証を完了
claudeコマンドが実行できることを確認- 本プロジェクトの
.envをAI_PROVIDER=claude_agentに設定 ai-note-writer run ...で通常どおり実行
ai-note-writer run --num-posts 5 --test-mode true --submit-notes false --evaluate-before-submit trueai-note-writer run --num-posts 5 --test-mode true --submit-notes true --evaluate-before-submit trueai-note-writer notes --max-results 20実行ログ JSON は outputs/runs/ または outputs/notes/ に保存されます。
ai-note-writer-guiRun writer: 取得→生成→(評価)→(投稿) ※ 日本語フィード固定Fetch notes_written: 直近の投稿ノート情報取得Open last JSON: 最新の結果ファイルを開く
ワークフロー: .github/workflows/community_note_writer.yml
GitHub リポジトリの Settings > Secrets and variables > Actions で以下を設定:
X_API_KEYX_API_KEY_SECRETX_ACCESS_TOKENX_ACCESS_TOKEN_SECRETAI_API_KEY- 任意:
AI_PROVIDER(例:xai)AI_BASE_URL(例:https://api.x.ai/v1)AI_MODEL(例:grok-3-latest)CLAUDE_CLI_PATH(例:claude)CLAUDE_MAX_TURNS(例:4)CLAUDE_USE_CLI_FALLBACK(例:true)
Actions タブから Automated Community Note Writer を手動実行できます。
成果物は artifact (writer-outputs) として保存されます。
- 初期は
test_mode=trueで検証してください。 - 外部 AI API の利用料金は利用者負担です。
- コミュニティノートの品質向上のため、
evaluate_noteの閾値運用(claim_opinion_score)を推奨します。