- 下記のスクリプトを記述した XML ファイルをダウンロードできます
// OAuth2 config sample at [OAuth 2.0 Setting]
// - Authorization Endpoint URL: https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force
// - Token Endpoint URL: https://oauth2.googleapis.com/token
// - Scope: https://www.googleapis.com/auth/gmail.modify
// - Consumer Key: (Get by Google Developer Console)
// - Consumer Secret: (Get by Google Developer Console)
main();
function main(){
//// == 工程コンフィグ・ワークフローデータの参照 / Config & Data Retrieving ==
const auth = configs.get("conf_auth");
const messageId = engine.findData( configs.getObject("conf_messageId") );
const labels = retrieveLabels();
//// == 演算 / Calculating ==
const apiUri = determineApiUri( messageId );
const labelIds = getLabelIds( auth, labels );
removeLabels( apiUri, auth, labelIds );
}
/**
* ワークフローデータからラベルを読み出す
* ラベルの設定がない場合はエラー
* @return {Array<String>} labels ラベル一覧
*/
function retrieveLabels() {
const labels = configs.get('conf_label').split('\n')
.map(label => label.trim())
.filter(label => label !== '');
// データ項目に改行のみ等、ラベルの設定がない場合はエラー
if (labels.length === 0) {
throw "No labels to remove are set.";
}
return labels;
}
/**
* Gmail のメール編集の URI を決定する
* メール ID が空であればエラーとする
* @param {String} messageId メール ID
* @return {String} apiUri API の URI
*/
function determineApiUri( messageId ) {
if ( messageId === "" || messageId === null ) {
throw "Message ID is empty.";
}
const apiUri = `https://gmail.googleapis.com/gmail/v1/users/me/messages/${encodeURI(messageId)}/modify`;
return apiUri;
}
/**
* ラベルからラベル ID を取得する
* @param {String} auth 認証設定
* @param {Array<String>} labels ラベル一覧
* @return {Array<String>} ラベル ID の一覧
*/
function getLabelIds( auth, labels ) {
const response = httpClient.begin()
.authSetting(auth)
.get('https://gmail.googleapis.com/gmail/v1/users/me/labels');
const responseJson = response.getResponseAsString();
const status = response.getStatusCode();
if (status >= 300) {
const accessLog = `--- users.labels.list --- ${status}\n${responseJson}\n`;
engine.log(accessLog);
throw `Failed to get labels. status: ${status}`;
}
const json = JSON.parse(responseJson);
let found = json.labels
.filter(label => labels.includes(label.name));
const foundLabels = found.map(label => label.name);
const notFoundLabels = labels.filter(label => !foundLabels.includes(label));
if (notFoundLabels.length > 0) {
const accessLog = `--- users.labels.list --- ${status}\n${responseJson}\n`;
engine.log(accessLog);
// id が見つからない label がある場合、エラーをスロー
throw `label ids of ${notFoundLabels.join(', ')} not found`;
}
return found.map(label => label.id);
}
/**
* Gmail REST API にメール編集の POST リクエストを送信し、ラベルを除去する
* @param {String} apiUri API の URI
* @param {String} auth 認証設定名
* @param {Array<String>} labelIds ラベル ID の一覧
*/
function removeLabels( apiUri, auth, labelIds ) {
const body = {
"removeLabelIds": labelIds
};
const response = httpClient.begin()
.authSetting( auth )
.body(JSON.stringify(body), "application/json; charset=UTF-8")
.post( apiUri );
// when error thrown
const responseJson = response.getResponseAsString();
const status = response.getStatusCode();
if (status >= 300) {
const accessLog = `---POST request--- ${status}\n${responseJson}\n`;
engine.log( accessLog );
throw `Failed to remove labels. status: ${status}`;
}
}