0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Slack】無料プランでも特定チャンネルにメールを転送する方法(ロボヨメ様のコードをちょっと修正)

Posted at

ロボットのヨメになりたい(ロボヨメ)様のこちらの投稿を使わせていただきまして、
非常に助かりました!

しかしGASの単体動作確認時に次の事象が発生してしまいました。。

  • ゴミ箱に捨てた該当ラベルのメールも再送してしまう。
  • 単体動作の度に、該当ラベルのメールを全て再送してしまう。

つきまして、これら修正をchatGPTに追加してもらいました。

(修正後のGASコード)

// 初期設定
const HOOK_URL = 'https://hooks.slack.com/services/*******/*****************'; // 取得したWebHooks URL

function main() {
  // 検索条件(該当ラベルのメール, ゴミ箱は除外)
  var searchTarget = 'label:***** is:unread -in:trash';

  GmailApp
    .search(searchTarget)
    .forEach(function (thread) {
      thread.getMessages().forEach(function (message) {
        if (!message.isUnread()) return; // 新着メールかどうか確認
        send(message);
      });
      thread.markRead();
    });  
}

function send(message) {
  var sendText = '【件名】\n' + message.getSubject()
               + '\n【本文】\n' + message.getPlainBody();
  
  var jsonData = {
    "username" : message.getFrom(),
    "text" : sendText
  };

  var options = {
    "method" : "post",
    "contentType" : "application/json",
    "payload" : JSON.stringify(jsonData)
  };

  UrlFetchApp.fetch(HOOK_URL, options);
}

以上です。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?