見出し画像

【なんでも自動化してみたい④】 GAS ✖️ Discord bot

前回

【なんでも自動化してみたい🤖③】 ブログやニュースを自動で収集!!!     LINE + Heroku + GAS

Discord(GASコード)

function newDiscord(webhookUrl) {
   return new Discord(webhookUrl);
}

class Discord {
 constructor(webhookUrl) {
   this.webhookUrl = webhookUrl;
 }

 publishTextMessage(message) {
   Http.post(this.webhookUrl, {
     headers : { 'Content-type': "application/json" },
     payload : JSON.stringify({
       "content": message,
       "tts": false,
     }),
   });
 }
}

実行コード

function interval_article() {
 const heroku  = Core.newHeroku(HEROKU_URL, HEROKU_API_KEY);
 const articles = heroku.searchArticles(["自動化","javascript","Google Apps Script","個人開発", "WebAPI", "APEX", "RiotGames", "オープンデータ"]).sort(() => Math.random() - 0.5);
 const discord = Core.newDiscord(DISCORD_URL);
 for (let i = 0; i < 10; i++) {
   discord.publishTextMessage(articles[i].link + "\n" + articles[i].source + " / " + articles[i].word);
 }
}


参考

https://support.discord.com/hc/ja/articles/228383668-%E3%82%BF%E3%82%A4%E3%83%88%E3%83%AB-Webhooks%E3%81%B8%E3%81%AE%E5%BA%8F%E7%AB%A0


この記事が気に入ったらサポートをしてみませんか?