0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GAS×Gemini APIで完全自動化!X投稿ボットの作り方と運用の現実

0
Posted at

GAS×Gemini APIで完全自動化!X投稿ボットの作り方と運用の現実

最近のAI業界では、Human Oversightの重要性が議論されていますが、実際のところ完全自動化できる領域はどこまでなのでしょうか。今回は、Google Apps Script(GAS)とGemini APIを組み合わせたX投稿ボットを構築し、その過程で見えてきた自動化の可能性と限界について深掘りします。

技術選定の背景と全体設計

なぜGAS×Geminiなのか

従来のX自動投稿ツールは定型文の組み合わせが主流でしたが、Gemini APIの登場により、文脈を理解した自然な投稿が可能になりました。GASを選んだ理由は以下の通りです:

  • コスト効率: サーバー不要でランニングコストがほぼゼロ
  • メンテナンス性: Googleアカウント内で完結する開発環境
  • スケーラビリティ: トリガー機能で柔軟な実行スケジュール

アーキテクチャ概要

[GAS Trigger] → [Gemini API] → [Content Generation] → [X API v2] → [投稿実行]
     ↓
[スプレッドシート] ← [ログ記録・状態管理]

事前準備:各種APIの取得と設定

X API v2の準備

X API v2のEssentialプラン(月額100ドル)が必要です。無料版では投稿APIが使用できません。

Developer Portalで以下の権限を設定:

  • Read and Write
  • OAuth 2.0 Bearer Token

Gemini API設定

Google AI Studioで無料のAPIキーを取得します。月間15リクエスト/分、100万トークン/月まで無料で利用可能です。

コア実装:投稿ボットの構築

1. 基本設定とライブラリ

// GASプロジェクトのプロパティに設定する値
const GEMINI_API_KEY = PropertiesService.getScriptProperties().getProperty('GEMINI_API_KEY');
const X_BEARER_TOKEN = PropertiesService.getScriptProperties().getProperty('X_BEARER_TOKEN');
const X_API_KEY = PropertiesService.getScriptProperties().getProperty('X_API_KEY');
const X_API_SECRET = PropertiesService.getScriptProperties().getProperty('X_API_SECRET');
const X_ACCESS_TOKEN = PropertiesService.getScriptProperties().getProperty('X_ACCESS_TOKEN');
const X_ACCESS_SECRET = PropertiesService.getScriptProperties().getProperty('X_ACCESS_SECRET');

// スプレッドシートID(ログ管理用)
const SHEET_ID = PropertiesService.getScriptProperties().getProperty('SHEET_ID');

2. Gemini APIとのやり取り

function generateTweetContent(topic, mood = 'informative') {
  const prompt = `
あなたは技術系のX(旧Twitter)アカウントを運営するAIです。
以下の条件で投稿文を作成してください:

トピック: ${topic}
口調: ${mood}
文字数: 200文字以内
ハッシュタグ: 2-3個程度

注意事項:
- 技術的な正確性を保つ
- 炎上しそうな表現は避ける
- エンゲージメントを促す疑問文を含める
- URLは含めない(文字数節約のため)

出力は投稿文のみで、前置きは不要です。
  `;

  try {
    const response = UrlFetchApp.fetch(
      `https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=${GEMINI_API_KEY}`,
      {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        payload: JSON.stringify({
          contents: [{
            parts: [{
              text: prompt
            }]
          }],
          generationConfig: {
            temperature: 0.7,
            topK: 40,
            topP: 0.95,
            maxOutputTokens: 200,
          }
        })
      }
    );
    
    const data = JSON.parse(response.getContentText());
    
    if (!data.candidates || data.candidates.length === 0) {
      throw new Error('Gemini API returned no candidates');
    }
    
    return data.candidates[0].content.parts[0].text.trim();
    
  } catch (error) {
    console.error('Gemini API Error:', error);
    logToSheet('ERROR', `Gemini API failed: ${error.message}`, topic);
    return null;
  }
}

3. X API v2での投稿実行

function postToX(tweetContent) {
  // OAuth 1.0a署名の生成(ライブラリを使用)
  const oauth = {
    consumer_key: X_API_KEY,
    consumer_secret: X_API_SECRET,
    token: X_ACCESS_TOKEN,
    token_secret: X_ACCESS_SECRET,
  };
  
  const url = 'https://api.twitter.com/2/tweets';
  const payload = {
    text: tweetContent
  };
  
  try {
    // OAuth署名を含むヘッダーを生成
    const oauthHeader = generateOAuthHeader('POST', url, payload, oauth);
    
    const response = UrlFetchApp.fetch(url, {
      method: 'POST',
      headers: {
        'Authorization': oauthHeader,
        'Content-Type': 'application/json',
      },
      payload: JSON.stringify(payload)
    });
    
    const result = JSON.parse(response.getContentText());
    
    if (response.getResponseCode() === 201) {
      logToSheet('SUCCESS', `Tweet posted: ${result.data.id}`, tweetContent);
      return result.data.id;
    } else {
      throw new Error(`API Error: ${response.getResponseCode()} - ${result.detail || result.error}`);
    }
    
  } catch (error) {
    console.error('X API Error:', error);
    logToSheet('ERROR', `Tweet failed: ${error.message}`, tweetContent);
    return null;
  }
}

// OAuth 1.0a署名生成(簡略版)
function generateOAuthHeader(method, url, params, oauth) {
  // 実際の実装では、crypto-jsライブラリまたは
  // GASのUtilitiesクラスを使用してHMAC-SHA1署名を生成
  const timestamp = Math.floor(Date.now() / 1000);
  const nonce = Utilities.getUuid().replace(/-/g, '');
  
  // OAuth署名ベース文字列の生成とHMAC-SHA1署名
  // 詳細な実装はOAuth 1.0a仕様に準拠
  
  return `OAuth oauth_consumer_key="${oauth.consumer_key}",oauth_token="${oauth.token}",oauth_signature_method="HMAC-SHA1",oauth_timestamp="${timestamp}",oauth_nonce="${nonce}",oauth_version="1.0",oauth_signature="${signature}"`;
}

4. ログ管理とモニタリング

function logToSheet(status, message, content = '') {
  try {
    const sheet = SpreadsheetApp.openById(SHEET_ID).getActiveSheet();
    const timestamp = new Date();
    
    sheet.appendRow([
      timestamp,
      status,
      message,
      content.substring(0, 500), // 長すぎる場合は切り詰め
    ]);
    
    // 古いログの削除(1000行を超えたら古い分を削除)
    if (sheet.getLastRow() > 1000) {
      sheet.deleteRows(2, 100);
    }
    
  } catch (error) {
    console.error('Logging failed:', error);
  }
}

function checkSystemHealth() {
  const sheet = SpreadsheetApp.openById(SHEET_ID).getActiveSheet();
  const lastRow = sheet.getLastRow();
  
  if (lastRow > 1) {
    const lastStatus = sheet.getRange(lastRow, 2).getValue();
    const lastTime = sheet.getRange(lastRow, 1).getValue();
    
    // 24時間以上エラーが続いている場合はアラート
    if (lastStatus === 'ERROR' && (Date.now() - lastTime.getTime()) > 24 * 60 * 60 * 1000) {
      sendAlert('Bot has been failing for over 24 hours');
    }
  }
}

function sendAlert(message) {
  // Gmail APIまたはSlack Webhookでアラート送信
  GmailApp.sendEmail(
    'your-email@gmail.com',
    'X Bot Alert',
    message
  );
}

メイン実行フローと自動化設定

5. オーケストレーション

function mainExecutor() {
  console.log('Starting X bot execution...');
  
  try {
    // システムヘルスチェック
    checkSystemHealth();
    
    // トピック生成(時事ネタ、技術トレンド、曜日など考慮)
    const topic = generateTopicBasedOnContext();
    
    // コンテンツ生成
    const tweetContent = generateTweetContent(topic);
    
    if (!tweetContent) {
      throw new Error('Failed to generate tweet content');
    }
    
    // 重複チェック
    if (isDuplicate(tweetContent)) {
      console.log('Duplicate content detected, skipping...');
      return;
    }
    
    // 投稿実行
    const tweetId = postToX(tweetContent);
    
    if (tweetId) {
      console.log(`Successfully posted tweet: ${tweetId}`);
      storeTweetHistory(tweetContent, tweetId);
    }
    
  } catch (error) {
    console.error('Main execution failed:', error);
    logToSheet('FATAL', `Main execution failed: ${error.message}`, '');
  }
}

function generateTopicBasedOnContext() {
  const dayOfWeek = new Date().getDay();
  const hour = new Date().getHours();
  
  // 曜日・時間帯に応じたトピック選択
  const topics = {
    monday: ['週の始まり', 'プロダクティビティ', '新技術学習'],
    friday: ['週末プロジェクト', 'ハッカソン', 'オープンソース'],
    // ... 他の曜日
  };
  
  const weekday = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'][dayOfWeek];
  const dayTopics = topics[weekday] || ['一般的な技術話題'];
  
  return dayTopics[Math.floor(Math.random() * dayTopics.length)];
}

6. GASトリガー設定

function setupTriggers() {
  // 既存のトリガーを削除
  ScriptApp.getProjectTriggers().forEach(trigger => {
    ScriptApp.deleteTrigger(trigger);
  });
  
  // メイン実行トリガー(1日3回)
  ScriptApp.newTrigger('mainExecutor')
    .timeBased()
    .everyHours(8)
    .create();
  
  // ヘルスチェックトリガー(1日1回)
  ScriptApp.newTrigger('checkSystemHealth')
    .timeBased()
    .everyDays(1)
    .atHour(9)
    .create();
}

運用で見えてきた課題と対策

コンテンツ品質の担保

実際に運用して分かったのは、Geminiが生成する文章は自然ですが、時として事実と異なる情報を含むことです。対策として以下を実装しました:

function validateTechnicalContent(content) {
  // 危険なキーワードチェック
  const dangerousWords = ['確実に', '絶対に', '100%', '間違いなく'];
  const hasRiskyExpressions = dangerousWords.some(word => content.includes(word));
  
  if (hasRiskyExpressions) {
    return false;
  }
  
  // 技術的事実の簡単な検証(APIを使用)
  return true;
}

レート制限と費用管理

X APIのレート制限(15分間に300ツイート)は余裕がありますが、Gemini APIは15リクエスト/分の制限があります。連続実行時は注意が必要です。

炎上リスクの最小化

技術系アカウントでも炎上リスクはあります。以下のような安全装置を組み込みました:

  • 政治・宗教・社会問題に関するキーワード除外
  • エンゲージメント異常値の検知とアラート
  • 手動承認フローの緊急時切り替え

まとめと今後の展望

GAS×Gemini APIによるX自動投稿ボットは、技術的には十分実用レベルに達しています。月間運用コストも200円程度と非常に経済的です。しかし、完全自動化の限界も見えてきました。特にAI生成コンテンツの品質管理と責任の所在は、今後も重要な課題として残ります。

次のステップとしては、マルチモーダル対応(画像付き投稿)やユーザーエンゲージメント分析に基づく投稿最適化を検討しています。AIが人間の創造性を補完する形での「Human-in-the-loop」アプローチが、当面の現実的な解となりそうです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?