Google Apps Scriptで割と簡単にGmailを監視することができたので、
【未読で かつ 件名に「勤怠」という文字列が含まれている】メールが来たら、そのメールを既読に変え、
内容をSlackに投げて、Garoonのスケジュールに登録するスクリプトを書きました!
GaroonのユーザIDがわかればGaroonAPI使ってスクリプトで自動的に色々できそうですね
全体のコード
//ユーザ情報(連想配列)
var user_info = {
'ka2ya30201@gmail.com' : '0',
};
//MAIN
function myFunction() {
try{
//任意のクエリで検索した結果を取得
var threads = GmailApp.search('is:unread AND subject:勤怠');//カッコの中は好きなように
//スレッドの数分ループ
for (var i = 0; i < threads.length; i++) {
var thd = threads[i];
var msgs = thd.getMessages();
thd.markRead();
for (var j = 0; j < msgs.length; j++) {
var msg = msgs[j];
var from = msg.getFrom();
var address = from.match(/\<.+?\>/)[0].replace(/<|>/g,"");
if(address in user_info){
var plain_body = msg.getPlainBody();
plain_body = plain_body.substring(0,plain_body.indexOf("--"));//署名を消す処理
var title = thd.getFirstMessageSubject();
var link = thd.getPermalink();
var m_date = msg.getDate();
var rm_date = m_date.toLocaleString().replace(/JST/g,"");
var m_date_y = m_date.getFullYear();
var m_date_m = m_date.getMonth() + 1;
var m_date_d = m_date.getDate();
var g_date = m_date_y +"-"+ m_date_m +"-"+ m_date_d;
//Slackにメール内容投げる
sendHttpPostSlack(
"```件名: " + title//ここから
+ rm_date
+ "\nFrom: " + from
+ "\nTo: " + msg.getTo()
+ "\n\n" + plain_body.replace(/[\n\r]/g,"")
+ "\n\nlink: " + link + "``` "//ここまでがmessage
,"きんたいれんらくん");//username
//Garoonにメール内容投げる
sendHttpPostGaroon(g_date,address,plain_body,title,link);
}
else
{
Logger.log("知らないアドレスからメールが来たよ");
return;
}
}
}
}
catch(e)
{
Logger.log(e);
}
}
/**
* Slackにメールの内容を投げる
*/
function sendHttpPostSlack(message, username) {
//slackのincoming webhooksのurl
var postUrl_slack = "▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲";
var postChannel = "#▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲";//メールの中身を流したいslackのチャンネル(incoming webhooksにも同じ名前で指定する)
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_slack, options);
}
/**
* Garoonにメールの内容を投げる
*/
function sendHttpPostGaroon(g_date,address,plain_body,title,link) {
var url = "https://▲▼▲▼▲▼▲▼▲.cybozu.com/g/cbpapi/schedule/api.csp?";
var user_id = user_info[address];
var payload = createXML(user_id,g_date,plain_body,title,link);
var options = {
"method": "post",
"contextType": "application/soap+xml",
"muteHttpExceptions" : true,
"payload": payload
};
var response = UrlFetchApp.fetch(url, options);
Logger.log(XmlService.parse(response.getContentText()));
}
function createXML(user_id,g_date,plain_body,title,link){
var postUrl_garoon =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://www.w3.org/2003/05/soap-envelope\""+
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""+
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding\""+
" xmlns:base_services=\"http://wsdl.cybozu.co.jp/base/2008\">\n"+
"<SOAP-ENV:Header>\n"+
"<Timestamp SOAP-ENV:mustUnderstand=\"1\" Id=\"id\""+
" xmlns=\"http://schemas.xmlsoap.org/ws/2002/07/utility\">\n"+
"<Created>2010-08-12T14:45:00Z</Created>\n"+
"<Expires>2037-08-12T14:45:00Z</Expires>\n"+
"</Timestamp>\n"+
"<Action SOAP-ENV:mustUnderstand=\"1\""+
" xmlns=\"http://schemas.xmlsoap.org/ws/2003/03/addressing\">"+
"ScheduleAddEvents"+
"</Action>\n"+
"<Security xmlns:wsu=\"http://schemas.xmlsoap.org/ws/2002/07/utility\""+
"SOAP-ENV:mustUnderstand=\"1\""+
" xmlns=\"http://schemas.xmlsoap.org/ws/2002/12/secext\">\n"+
"<UsernameToken wsu:Id=\"id\">\n"+
"<Username>▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲</Username>\n"+
"<Password>▲▼▲▼▲▼▲▼▲▼</Password>\n"+
"</UsernameToken>\n"+
"</Security>\n"+
"<Locale>jp</Locale>\n"+
"</SOAP-ENV:Header>\n"+
"<SOAP-ENV:Body>\n"+
"<ScheduleAddEvents>\n"+
"<parameters>\n"+
"<schedule_event\n"+
" xmlns=\"\""+
" id=\"dummy\""+
" event_type=\"normal\""+
" version=\"dummy\""+
" public_type=\"public\""+
" plan=\"休み\""+
" detail=\""+ title+ "\""+
" description=\"" + plain_body +"\""+
" timezone=\"Asia/Tokyo\""+
" end_timezone=\"Asia/Tokyo\""+
" allday=\"true\">\n"+
"<members>\n"+
"<member>\n"+
"<user id=\""+ user_id + "\"></user>\n"+
"</member>\n"+
"</members>\n"+
"<when>\n"+
"<date start=\"" + g_date + "\" end=\"" + g_date + "\"></date>\n"+
"</when>\n"+
"</schedule_event>\n"+
"</parameters>\n"+
"</ScheduleAddEvents>\n"+
"</SOAP-ENV:Body>\n"+
"</SOAP-ENV:Envelope>\n";
return postUrl_garoon;
}