DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

Cover image for GMAIL to Discord Channel Webhook Tutorial
soom
soom

Posted on

GMAIL to Discord Channel Webhook Tutorial

Abstract

ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ Slack, Microsoft Teams, Discord ๋“ฑ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ํˆด์€ ์›ํ™œํ•œ ํ”„๋กœ์ ํŠธ ์ง„ํ–‰์„ ์œ„ํ•ด ํ•„์ˆ˜์ ์ธ ์š”์†Œ ์ค‘์— ํ•˜๋‚˜๋‹ค.

ํŠนํžˆ, ๊ฒŒ์ž„ ๋ณด์กฐ ๋„๊ตฌ๋กœ ์•Œ๋ ค์ง„ Discord์˜ ๊ฒฝ์šฐ ํผํฌ๋จผ์Šค์— ๋ฏผ๊ฐํ•œ ๊ฒŒ์ด๋จธ๋“ค์„ ๋งŒ์กฑ์‹œํ‚ค๋Š” ๊ฐ•๋ ฅํ•œ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋ณด๋‹ˆ ์ด๋ฅผ ํ”„๋กœ์ ํŠธ ๋ฉ”์ธ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ํˆด๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์ ์  ์ฆ๊ฐ€ํ•˜๊ณ  ์žˆ๋‹ค.

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ํŒ€ ๋ฉ”์ผ์ด๋‚˜ ๊ณต์‹ ๋ฉ”์ผ์ด ์˜ฌ๋•Œ๋งˆ๋‹ค ๋ฉ”์ผ์„ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ Discord์˜ ํ•˜๋‚˜์˜ ์ฑ„๋„์—์„œ ์•Œ๋ฆผ์ด ์˜ฌ์ˆ˜ ์žˆ๋„๋ก ์›นํ›… ์„ค์ •์„ ํ•˜๋Š” ๊ณผ์ •์„ ์†Œ๊ฐœํ•˜๊ณ ์ž ํ•œ๋‹ค.

Discord DOCs ๋งํฌ: https://discord.com/developers/applications


Prerequisite

  • ์—ฌ๊ธฐ์„œ๋Š” Discord ๊ณ„์ •์ด ์žˆ๊ณ  ์„œ๋ฒ„๋ฅผ ์ƒ์„ฑํ•œ ์ƒํƒœ๋ฅผ ๊ฐ€์ •ํ•œ๋‹ค
  • ์„œ๋ฒ„์— ํ•ด๋‹นํ•˜๋Š” ์›นํ›… ๊ถŒํ•œ์ด ํ•„์š”ํ•˜๋‹ค
์›นํ›… ๊ถŒํ•œ ์„ค์ •

Image description


Getting Started

Integrations ๋ฉ”๋‰ด์—์„œ Webhooks ์ƒ์„ฑ์„ ํด๋ฆญ

Integrations ๋ฉ”๋‰ด

Image description


New Webhook์„ ์„ ํƒํ•ด์„œ ์ƒˆ๋กœ์šด Webhook์„ ์ƒ์„ฑํ•œ ๋’ค ํ•„์š”ํ•œ ์ •๋ณด(Bot Name, Channel)๋ฅผ ์ž…๋ ฅํ•ด์„œ Noti-Bot์„ ์ƒ์„ฑ

Discord ์„ธํŒ…์€ ์—ฌ๊ธฐ๊นŒ์ง€ ์™„๋ฃŒ

Webhook ์ƒ์„ฑ

Image description


๊ตฌ๊ธ€ ์•ฑ ์Šคํฌ๋ฆฝํŠธ ํŽ˜์ด์ง€ ์ ‘์†:
https://www.google.com/script/start/

์ƒˆ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

๊ตฌ๊ธ€ ์•ฑ ์Šคํฌ๋ฆฝํŠธ ๋ฉ”์ธ ํ™”๋ฉด

Image description

๋ฉ”์ธ ๋Œ€์‰ฌ๋ณด๋“œ

Image description


์ƒˆ๋กœ์šด ํ”„๋กœ์ ํŠธ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑ

Note

  • webhooks ๋ณ€์ˆ˜์—๋Š” Discord์˜ Webhook URL์„ ์ž…๋ ฅ
Webhook ์ƒ์„ฑ ํ™”๋ฉด

Image description

  • channel ๋ณ€์ˆ˜์—๋Š” ์ฑ„๋„ ์ด๋ฆ„์„ ๋“ฑ๋ก
Discord ์ฑ„๋„๋ช…

Image description

  • searchQuery ์—๋Š” ๊ตฌ๊ธ€ ๋ฉ”์ผ์—์„œ ์“ฐ๋Š” ๊ฒ€์ƒ‰ ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. (ํ•˜๊ธฐ ์ด๋ฏธ์ง€ ์ฐธ์กฐ)
Gmail ์ฟผ๋ฆฌ ๊ฒ€์ƒ‰

Image description

  • Gmail์—์„œ๋Š” Discord์— 2000 ์ž ์ด์ƒ๋˜๋Š” ๋‚ด์šฉ์„ ๊ฒŒ์‹œํ• ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— 2000 ์ž ๋„˜๋Š”๊ฒฝ์šฐ ์ œํ•œํ•˜๋Š” ๋กœ์ง์ด ํ•„์š”ํ•˜๋‹ค.

code.gs
function postDiscord(postMessage) {
    const webhooks = 'https://discord.com/api/webhooks/webhooks/{webhook.id}/{webhook.token}';
    const channel = '{channel name}';
    const parse = 'full';
    const method = 'post';

    const payload = {
        channel: channel,
        content: postMessage,
        parse: parse,
    };

    const params = {
        method: method,
        payload: payload,
        muteHttpExceptions: true,
    };

    response = UrlFetchApp.fetch(webhooks, params);
}

function sendMailsToDiscord() {
    const searchQuery = '{gmail query}';
    const date = new Date();
    const checkSpan = 30;
    date.setMinutes(date.getMinutes() - checkSpan);

    const threads = GmailApp.search(searchQuery);
    const msgs = GmailApp.getMessagesForThreads(threads);

    for (let i = 0; i < msgs.length; i++) {
        const lastMsgDate = threads[i].getLastMessageDate();

        if (lastMsgDate.getTime() < date.getTime()) break;

        for (let j = 0; j < msgs[i].length; j++) {
            const msgDate = msgs[i][j].getDate();
            const msgBody = msgs[i][j].getPlainBody();
            const subject = msgs[i][j].getSubject();

            const postMessage =
                'From mailing list' +
                '\n' +
                Utilities.formatDate(msgDate, 'America/New_York', 'MM/DD/yyyy hh:mm:ss') +
                '\n' +
                'Title:' +
                subject +
                '\n' +
                '[hr]' +
                msgBody;

            // 2000 Characters limits
            if (postMessage.length > 2000) {
                const stopPos = 1900; //
                const msg = '"Exceeded 2000 character limit."';
                postMessage = postMessage.substring(0, stopPos) + '\n' + msg;
            }

            postDiscord(postMessage);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

trigger ๋ฅผ ์„ค์ •

์ดํ›„ ์ƒˆ๋กœ์šด ๋ฉ”์ผ์ด ์˜ฌ ๊ฒฝ์šฐ ์ง€์ •ํ•œ ์ฑ„๋„์— ์ƒˆ๋กœ์šด ๋ฉ”์„ธ์ง€๊ฐ€ ํ‘ธ์‹œ๋  ๊ฒƒ์ด๋‹ค.

Note

  • ์‹คํ–‰ํ•  ํ•จ์ˆ˜๋ฅผ ์„ ํƒ: sendMailsToDiscord
  • ์‹คํ–‰ํ•  ๋ฐฐํฌ: Head
  • ์ด๋ฒคํŠธ ์†Œ์Šค ์„ ํƒ: ์‹œ๊ฐ„ ๊ธฐ๋ฐ˜
  • ํŠธ๋ฆฌ๊ฑฐ ๊ธฐ๋ฐ˜ ์‹œ๊ฐ„ ์œ ํ˜• ์„ ํƒ: ๋ถ„ ๋‹จ์œ„ ํƒ€์ด๋จธ
  • ๋ถ„ ๊ฐ„๊ฒฉ ์„ ํƒ: 30๋ถ„๋งˆ๋‹ค
  • ํŠธ๋ฆฌ๊ฑฐ ์‹คํŒจ ์•Œ๋ฆผ ์„ค์ •: ์ฆ‰์‹œ ์•Œ๋ฆผ
trigger ํ™”๋ฉด

Image description


Conclusion


๋ณธ ํฌ์ŠคํŒ…์—์„œ๋Š” GMAIL์—์„œ Discord ์ฑ„๋„์— ์ƒˆ๋กœ์šด ๋ฉ”์ผ์ด ์˜ฌ๋•Œ๋งˆ๋‹ค ํ‘ธ์‹œํ•˜๋Š” webhook์„ Google App Script๋ฅผ ํ†ตํ•ด Set upํ•ด ๋ณด์•˜๋‹ค.

Discord๋Š” ํผํฌ๋จผ์Šค์— ๋ฏผ๊ฐํ•œ ๊ฒŒ์ด๋จธ๋ฅผ ๋งŒ์กฑ์‹œํ‚ฌ์ˆ˜ ์žˆ์„์ •๋„์˜ ์ˆ˜์ค€์˜ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ๊ณ  ์•ž์œผ๋กœ ์—ฌ๋Ÿฌ ํ”„๋กœ์ ํŠธ์—์„œ๋„ ๋งŽ์ด ์“ฐ์ผ ๊ฒƒ์ด๋‹ค. ์—ฌ๊ธฐ์— ์ „ ์„ธ๊ณ„ ๋ฉ”์ผ์˜ ํ‘œ์ค€์ด๋ผ๊ณ ๋„ ํ• ์ˆ˜ ์žˆ๋Š” GMAIL์„ ์—ฐ๋™ํ•˜๋Š” ๊ฒƒ์€ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” Webhook ์ค‘ ํ•˜๋‚˜๊ฐ€ ๋ ๊ฒƒ์ด๋ฉฐ ๋ณธ ํฌ์ŠคํŒ…์ด ๊ทธ๋Ÿฐ ๋ถ€๋ถ„์— ๋งŽ์€ ๋„์›€์ด ๋˜์—ˆ์œผ๋ฉด ํ•˜๋Š” ๋ฐ”๋žจ์ด๋‹ค.

๋ณธ ํฌ์ŠคํŒ…์— ์“ฐ์ธ Method๊ฐ€ ๊ถ๊ธˆํ•˜๋‹ค๋ฉด ํ•˜๊ธฐ์˜ Google App Script ๋งํฌ์—์„œ ์ž์„ธํ•œ ๋‚ด์šฉ์„ ์ฐธ๊ณ 

Link: https://developers.google.com/apps-script/reference/document

Top comments (0)

๐ŸŒ–๐ŸŒ—๐ŸŒ˜ Turn on dark mode in Settings