Discord Relay running

エンドポイント

メソッドパス説明
POST/relay/webhookDiscord にメッセージを転送
GET/relay/webhook/status設定状態を確認

使い方

DISCORD_WEBHOOK_URLS を使う場合

curl -X POST http://localhost:3000/relay/webhook \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $RELAY_TOKEN" \
  -d '{"content": "Hello!", "username": "Bot"}'

送信元で宛先を指定する場合

curl -X POST http://localhost:3000/relay/webhook \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $RELAY_TOKEN" \
  -d '{"webhook_url": "https://discord.com/api/webhooks/YOUR_ID/YOUR_TOKEN", "content": "Hello!"}'

Google Apps Script から送信する例

DISCORD_WEBHOOK_URLS をサーバー側で設定している場合

function sendDiscordRelay() {
  const endpoint = 'https://YOUR_INTERNAL_WEB_URL/relay/webhook';
  const relayToken = PropertiesService
    .getScriptProperties()
    .getProperty('RELAY_TOKEN');
  const payload = {
    content: 'GAS からの通知です',
    username: 'GAS Bot'
  };

  const response = UrlFetchApp.fetch(endpoint, {
    method: 'post',
    contentType: 'application/json',
    headers: {
      Authorization: `Bearer ${relayToken}`
    },
    payload: JSON.stringify(payload),
    muteHttpExceptions: true
  });

  Logger.log(response.getResponseCode());
  Logger.log(response.getContentText());
}

GAS 側で宛先 webhook URL を指定する場合

function sendDiscordRelayWithWebhookUrl() {
  const endpoint = 'https://YOUR_INTERNAL_WEB_URL/relay/webhook';
  const relayToken = PropertiesService
    .getScriptProperties()
    .getProperty('RELAY_TOKEN');

  const payload = {
    webhook_url: 'https://discord.com/api/webhooks/YOUR_ID/YOUR_TOKEN',
    content: 'GAS から宛先を指定して送信します',
    username: 'GAS Bot'
  };

  const response = UrlFetchApp.fetch(endpoint, {
    method: 'post',
    contentType: 'application/json',
    headers: {
      Authorization: `Bearer ${relayToken}`
    },
    payload: JSON.stringify(payload),
    muteHttpExceptions: true
  });

  Logger.log(response.getResponseCode());
  Logger.log(response.getContentText());
}

RELAY_TOKEN は GAS の「プロジェクトの設定」→「スクリプト プロパティ」に保存してください。

リクエスト形式

{
  "webhook_url": "https://discord.com/api/webhooks/...", // 省略可
  "content":    "テキスト",             // Discord webhook に渡す payload
  "username":   "表示名",               // 省略可
  "avatar_url": "https://...",         // 省略可
  "embeds":     [{ "title": "..." }]   // 省略可
}

セキュリティ

項目内容
認証RELAY_TOKEN が一致する場合のみ送信
宛先指定RELAY_TOKEN が一致する場合のみ有効
許可 URLhttps://discord.com/api/webhooks/... または https://discordapp.com/api/webhooks/...
転送内容webhook_url / webhook_urls / relay_token は Discord に送信しません