|
| 1 | +import { Application } from './application'; |
| 2 | +import { getConfig } from './config'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Discord Webhookのペイロード型 |
| 6 | + */ |
| 7 | +interface DiscordWebhookPayload { |
| 8 | + content: string; |
| 9 | + username?: string; |
| 10 | + avatar_url?: string; |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Discordに通知を送信 |
| 15 | + */ |
| 16 | +export function sendDiscordNotification(application: Application): void { |
| 17 | + const config = getConfig(); |
| 18 | + |
| 19 | + // メッセージ内容を構築 |
| 20 | + const message = createNotificationMessage( |
| 21 | + config.DISCORD_MENTION_ID, |
| 22 | + config.APPLICATION_FORM_URL, |
| 23 | + application |
| 24 | + ); |
| 25 | + |
| 26 | + const payload: DiscordWebhookPayload = { |
| 27 | + content: message, |
| 28 | + username: '申請通知Bot', |
| 29 | + }; |
| 30 | + |
| 31 | + const options: GoogleAppsScript.URL_Fetch.URLFetchRequestOptions = { |
| 32 | + method: 'post', |
| 33 | + contentType: 'application/json', |
| 34 | + payload: JSON.stringify(payload), |
| 35 | + muteHttpExceptions: true, |
| 36 | + }; |
| 37 | + |
| 38 | + try { |
| 39 | + const response = UrlFetchApp.fetch(config.DISCORD_WEBHOOK_URL, options); |
| 40 | + const responseCode = response.getResponseCode(); |
| 41 | + |
| 42 | + if (responseCode !== 204 && responseCode !== 200) { |
| 43 | + throw new Error(`Discord API error: ${responseCode} - ${response.getContentText()}`); |
| 44 | + } |
| 45 | + |
| 46 | + Logger.log(`Discord通知送信成功: 行${application.rowNumber}`); |
| 47 | + } catch (error) { |
| 48 | + Logger.log(`Discord通知送信失敗: ${error}`); |
| 49 | + throw error; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * 通知メッセージを作成 |
| 55 | + */ |
| 56 | +function createNotificationMessage( |
| 57 | + mentionId: string, |
| 58 | + formUrl: string, |
| 59 | + application: Application |
| 60 | +): string { |
| 61 | + return `<@${mentionId}> |
| 62 | +
|
| 63 | +📝 **新しいエクスプレッション申請が追加されました** |
| 64 | +
|
| 65 | +**申請種類:** ${application.applicationType} |
| 66 | +**申請者:** ${application.applicantName} |
| 67 | +**行番号:** ${application.rowNumber} |
| 68 | +
|
| 69 | +🔗 **【Discord】絵文字/スタンプ/サウンドボード申請フォーム:** ${formUrl}`; |
| 70 | +} |
0 commit comments