GAS (google app script)로 지정된 라벨의 Gmail을 slack에 알립니다.
12358 단어 GoogleAppsScriptgas슬랙gmailGooglePlay
업데이트했습니다.
바삭바삭하게 간단하게 할 수 있습니다만, 이 GAS의 설정으로 자동으로 읽을 수 있으니 주의를.
준비하는 것
이런

이런

slack의 webhook URL은 여기에서 발행 가능 →
https://api.slack.com/apps/{あなたの Slack App ID}/incoming-webhooks
그래서,
gmail2slack.gs
// slackのwebhook URLはこちらから発行可能 → https://api.slack.com/apps/{あなたの Slack App ID}/incoming-webhooks
var postUrl = "https://hooks.slack.com/services/xxxxxxxxxxx";
// 通知したいslackのチャネル(部屋)
var postChannel = "#n_app_review";
// 抽出元のGmailのラベル
var checkLabel = "TO_ME";
function myFunction() {
// 未読の指定ラベル を検索
var threads = GmailApp.search('is:unread label:' + checkLabel);
var count = threads.length;
Logger.log("対象件数:" + threads.length);
for(var i = 0; i < count; i++) {
var lastDate = threads[i].getLastMessageDate();
var datetime = lastDate.getFullYear() + "/" + (lastDate.getMonth() + 1) + "/" + lastDate.getDate()
+ " " + lastDate.getHours() + ":" + lastDate.getMinutes() + ":" + lastDate.getSeconds();
var messages = threads[i].getMessages();
//同じ件名で、ネストされたメッセージを個別に
if(messages.length > 0){
for(var j = 0; j < messages.length ; j++){
// 簡易的に、brタグを改行に
var brTagStripped = messages[j].getBody().replace(/(<br>|<br \/>)/gi, '\n');
// 簡易的に、HTMLメールのタグを解除
var stripped = brTagStripped.replace(/(<([^>]+)>)/ig,"");
var postMessage = " 件名:[" + threads[i].getFirstMessageSubject() + "]" + "本文```" + stripped + "```";
Logger.log(datetime + postMessage);
// 対象のメールを1通を既読に。
messages[j].markRead();
//slackに通知
sendHttpPost(postMessage);
}
}
// 対象のメールを既読にする
// NOTE: 同じ件名でネストされているものはすべて既読になります。
// threads[i].markRead();
}
}
function sendHttpPost(message, username)
{
var jsonData =
{
"channel" : postChannel,
"username" : username,
"text" : message
};
var payload = JSON.stringify(jsonData);
var options =
{
"method" : "post",
"contentType" : "application/json",
"payload" : payload
};
UrlFetchApp.fetch(postUrl, options);
}
그 후,
이 스크립트를 GAS 타이머 설정 로 5분마다 등으로 설정해, 메일을 주워 대상 라벨의 것이 있으면 Slack의 지정의 방에 통지합니다.

포인트 & 과제
threads[i].markRead();
보다는, messages[j].markRead();
로 1통씩 이미 읽도록 하고 있습니다. 
나는 안드로이드 앱의 새로운 리뷰 알림을 슬랙에 흘리기 때문에,
이용하고 있습니다.
slack에 리뷰를 통지하는 연계 앱은 상당히 있지만,
무료로 계정에 연결하는 모든 앱에 대한 리뷰 알림을 찾을 수 없습니다.
메일에서 픽업하는 방법으로 대응해 보았습니다.
앱 수가 많으면 앱 ID를 하나씩 취득하여 설정하는 것도 힘들기 때문에,
아무리 편리합니다.
iOS는 AppStoreConnect에서 새로운 리뷰 알림 설정 (리뷰 편집을 알릴 수 있음)을 찾을 수 없으므로,
AppStoreConnectAPI에서 최선을 다하고 앱 ID를 얻는 방법과 잡으려고합니다.
(아직 하지 않았다. 땀)
Reference
이 문제에 관하여(GAS (google app script)로 지정된 라벨의 Gmail을 slack에 알립니다.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/matsuyoro/items/187214f664148282a7af텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)