| メソッド | パス | 説明 |
|---|---|---|
| POST | /relay/webhook | Discord にメッセージを転送 |
| POST | /relay/webhook/message | webhook が送信したメッセージ(リアクション含む)を取得 |
| GET | /relay/webhook/status | 設定状態を確認 |
DISCORD_WEBHOOK_URLS を使う場合
curl -X POST https://internal.highsto.net/relay/webhook \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $RELAY_TOKEN" \
-d '{"content": "Hello!", "username": "Bot"}'
送信元で宛先を指定する場合
curl -X POST https://internal.highsto.net/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!"}'
GAS 側で宛先 webhook URL を指定する場合
function sendDiscordRelayWithWebhookUrl_(message, url) {
const endpoint = 'https://internal.highsto.net/relay/webhook';
const relayToken = PropertiesService
.getScriptProperties()
.getProperty('RELAY_TOKEN');
const payload = {
webhook_url: url,
content: message,
username: "売上通知bot"
};
const response = UrlFetchApp.fetch(endpoint, {
method: 'post',
contentType: 'application/json',
headers: {
Authorization: `Bearer ${relayToken}`
},
payload: JSON.stringify(payload),
muteHttpExceptions: true
});
console.log(response.getResponseCode());
console.log(response.getContentText());
}
DISCORD_WEBHOOK_URLS をサーバー側で設定している場合
function sendDiscordRelay() {
const endpoint = 'https://internal.highsto.net/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
});
console.log(response.getResponseCode());
console.log(response.getContentText());
}
wait: true で送信するとメッセージ ID が返ります。同じ webhook URL と ID で後からリアクションを取得できます。
const RELAY_BASE = 'https://internal.highsto.net/relay';
function relayPost_(path, body) {
const relayToken = PropertiesService
.getScriptProperties()
.getProperty('RELAY_TOKEN');
const response = UrlFetchApp.fetch(RELAY_BASE + path, {
method: 'post',
contentType: 'application/json',
headers: { Authorization: `Bearer ${relayToken}` },
payload: JSON.stringify(body),
muteHttpExceptions: true
});
return {
code: response.getResponseCode(),
body: JSON.parse(response.getContentText())
};
}
// 送信して message ID を受け取る
function sendAndGetMessage_(webhookUrl, content) {
const res = relayPost_('/webhook', { webhook_url: webhookUrl, content, wait: true });
const msg = res.body.messages[0]; // { id, channel_id, guild_id, timestamp }
const link = `https://discord.com/channels/${msg.guild_id}/${msg.channel_id}/${msg.id}`;
return { id: msg.id, link };
}
// 指定のカスタム絵文字(例: done:1007626622129680404)が付いているか
function hasReaction_(webhookUrl, messageId, emojiId) {
const res = relayPost_('/webhook/message', { webhook_url: webhookUrl, message_id: messageId });
if (res.code !== 200) throw new Error(JSON.stringify(res.body));
return (res.body.message.reactions || [])
.some(r => r.emoji.id === emojiId && r.count > 0);
}
RELAY_TOKEN は GAS の「プロジェクトの設定」→「スクリプト プロパティ」に保存してください。
{
"webhook_url": "https://discord.com/api/webhooks/...", // 省略可
"content": "テキスト", // Discord webhook に渡す payload
"username": "表示名", // 省略可
"avatar_url": "https://...", // 省略可
"embeds": [{ "title": "..." }], // 省略可
"wait": true // 省略可。true でメッセージ ID を返す
}
wait: true の時のレスポンス
{
"ok": true,
"sent": 1,
"messages": [
{ "destination": 1, "id": "...", "channel_id": "...", "guild_id": "...", "timestamp": "..." }
]
}
POST /relay/webhook/message のリクエスト / レスポンス
// request
{ "webhook_url": "https://discord.com/api/webhooks/...", "message_id": "123..." }
// response(Discord の message object をそのまま返す)
{ "ok": true, "message": { "id": "...", "reactions": [{ "emoji": { "id": "...", "name": "done" }, "count": 1 }] } }
| 項目 | 内容 |
|---|---|
| 認証 | RELAY_TOKEN が一致する場合のみ送信 |
| 宛先指定 | RELAY_TOKEN が一致する場合のみ有効 |
| 許可 URL | https://discord.com/api/webhooks/... または https://discordapp.com/api/webhooks/... |
| 転送内容 | webhook_url / webhook_urls / relay_token / wait は Discord に送信しません |