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?

Gmailの連絡をTeamsに投稿

Posted at

筆者について

name → Koki Teramoto
age → 19
language → ja_JP, en_US

本編

ということで久しぶりの投稿です。なんか最近通知がうるさいなと思ってたら Contribution が 200 超えました、ありがとうございます。今回は題名にもあるとおりにどうにかこうにか Gmail の連絡を Teams に投稿しようという会です。めっちゃ久しぶりのが GAS や、、、、。

Webhock の URL を取得

Teams のアプリで Incoming Webhock が廃止されていました!おわり~、とはならなくてですね、どうやら後継の Workflows が公開されているのでそれを設定していきます。「Send webhook alerts from tenant users to a channel」と検索することで出てきます(日本語は知らないですごめんなさい)。そのまま Workflow の編集画面より、Webhock 用の URL を取得してください。また、認証がめんどくさいので僕は投稿可能な人を Anyone にしています。

Screenshot 2025-07-30 135711.png

GAS を書こう!

ということで、GAS を書きます。大した量でもないので Clasp は使わず GAS をべた書きしていきます。 WEBHOCK_URIとしてスクリプトプロパティに URL を保存しています。

main.js
function main(){
  // Terms : Sender
  const termsSender = "to:E-MAIL"
  // Terms : Time
  const unixTime = (new Date()).getTime();
  const termTimeNow = Math.floor(unixTime/1000);
  const termTimeStart = termTimeNow - 1800;
  const termTimeStr = termTimeStart.toString();
  const termsTime = 'after:'+ termTimeStr
  const termsAll = termsTime + " " + termsSender;
  var threads = GmailApp.search(query);
  for (var i = 0; i < threads.length; i++) {
    var messages = threads[i].getMessages();
    for (var j = 0; j < messages.length; j++) {
      var message = messages[j];
      var subject = message.getSubject();
      var body = message.getBody()
      var date = message.getDate();
      sendToTeams(subject, body);
    }
  }
}

function sendToTeams(sub, body) {
  const TEAMS_WebhockURL = PropertiesService.getScriptProperties().getProperty("WEBHOCK_URI")
  const TEAMS_SendContent = {
   "type":"message",
   "attachments":[
      {
         "contentType":"application/vnd.microsoft.card.adaptive",
         "content":{
            "$schema":"http://adaptivecards.io/schemas/adaptive-card.json",
            "type":"AdaptiveCard",
            "version":"1.2",
            "body":[
               {
                  "type":"TextBlock",
                  "text":`${sub}`,
                  "color":"default",
                  "weight": "bolder",
                  "size": "large"
               },
               {
                  "type":"TextBlock",
                  "text":body,
                  "color":"default",
                  "wrap": true,
                  "size": "small"
               }
            ]
         }
      }
   ]
  };
  const options = {
    'method': 'post',
    'headers' : {
      'Content-Type': 'application/json; charset=UTF-8'
    },
    'body' :JSON.stringify(TEAMS_SendContent),
    'payload': JSON.stringify(TEAMS_SendContent)
  };
  var response = UrlFetchApp.fetch(TEAMS_WebhockURL, options);
}

Gmail の検索条件は UNIX 時間で指定する必要があります。詳しくは以下の記事を。

また、データは Adaptive Cards で設定する必要があります。

最後に

ということで久しぶりの投稿でした。これからもぼちぼち投稿していきたいと思います。

0
0
0

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?