Skip to content

Instantly share code, notes, and snippets.

@mistymagich
Last active October 4, 2021 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mistymagich/2f1f3e55ade29998c7066b0c643100fa to your computer and use it in GitHub Desktop.
Save mistymagich/2f1f3e55ade29998c7066b0c643100fa to your computer and use it in GitHub Desktop.
Gmailに来たメールをChatworkに通知する
const DROPBOX_TOKEN =
PropertiesService.getScriptProperties().getProperty("DROPBOX_TOKEN");
const CHATWORK_TOKEN =
PropertiesService.getScriptProperties().getProperty("CHATWORK_TOKEN");
const ROOM_ID = PropertiesService.getScriptProperties().getProperty("ROOM_ID");
// チャットワークへ送信
function sendChatwork_(record) {
const [title, date, from, subject, plainBody, id, shareURLs] = record;
// 送信文
let body = "";
body += `[info][title]${title}にメールが来ました[/title]`;
body += `件名:${subject}\n`;
body += `From: ${from}\n`;
body += "[hr]";
body += plainBody;
body += "[hr]";
if (shareURLs.length > 0) {
body += "添付ファイル:\n";
body += shareURLs.join("\n");
}
body += `[/info]`;
const params = {
headers: { "X-ChatWorkToken": CHATWORK_TOKEN },
method: "post",
payload: {
body: body,
},
};
const url = "https://api.chatwork.com/v2/rooms/" + ROOM_ID + "/messages";
UrlFetchApp.fetch(url, params);
}
// Dropboxへ1つのファイルをアップロード
function dropboxUpload_(messageID, attach) {
const fileName = attach.getName();
const src = attach;
const dest = `/${messageID}/${fileName}`;
const apiUrl = "https://content.dropboxapi.com/2/files/upload";
const options = {
method: "post",
headers: {
Authorization: "Bearer " + DROPBOX_TOKEN,
"Content-Type": "application/octet-stream",
"Dropbox-API-Arg":
'{"path":"' + dest + '","mode":{".tag":"overwrite"},"autorename":true}',
},
payload: src,
muteHttpExceptions: false,
};
// APIレスポンスそのまま
return JSON.parse(UrlFetchApp.fetch(apiUrl, options).getContentText());
}
// Dropbox上のファイルの共有リンクの取得
function dropboxShare_(path) {
const apiUrl =
"https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings";
// 公開リンク
const payload = {
path: path,
settings: {
requested_visibility: {
".tag": "public",
},
},
};
const res = callDropboxAPI_(apiUrl, payload);
// 共有リンクURL
return res.url;
}
// Dropboxへファイルをアップロードして共有URLを取得する
function dropboxUploadAndShare_(messageID, attach) {
//添付ファイルのファイル名取得
fileName = attach.getName();
// Logger.log(fileName);
//添付ファイル保存
const uploadResponse = dropboxUpload_(messageID, attach);
// Logger.log(uploadResponse);
//共有
const shareURL = dropboxShare_(uploadResponse.path_lower);
// Logger.log(shareURL);
return shareURL;
}
// メイン:エントリポイント
function searchContactMail() {
// Gmailの検索条件 未読メール
const query = "is:unread";
// メール検索
const threads = GmailApp.search(query);
// スレッド取り出し
const messagesForThreads = GmailApp.getMessagesForThreads(threads);
// 各スレッド内の最初のメールに対して処理
for (const messages of messagesForThreads) {
const message = messages[0];
const messageID = message.getId();
// 添付ファイル処理
const attach = message.getAttachments();
let fileurls = [];
for (let k = 0; k < attach.length; k++) {
const shareURL = dropboxUploadAndShare_(messageID, attach[k]);
fileurls.push(shareURL);
}
const record = [
"info",
message.getDate(),
message.getFrom(),
message.getSubject(),
message.getPlainBody(),
messageID,
fileurls,
];
// チャットワーク送信
sendChatwork_(record);
// 既読化
message.markRead();
}
}
// Dropbox API呼び出し (RPC endpoints形式)
function callDropboxAPI_(apiURL, payload) {
const options = {
method: "POST",
headers: {
Authorization: "Bearer " + DROPBOX_TOKEN,
"Content-Type": "application/octet-stream",
},
contentType: "application/json",
payload: JSON.stringify(payload),
muteHttpExceptions: true,
};
const res = JSON.parse(UrlFetchApp.fetch(apiURL, options).getContentText());
// Logger.log(res);
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment