Skip to content

Instantly share code, notes, and snippets.

@ironah2o
Last active April 9, 2016 08:47
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ironah2o/8f2897be3eb960f5b59d361c065aad81 to your computer and use it in GitHub Desktop.
google apps script for pushing a tasks list to slack
// 参考:
// http://ma13.hateblo.jp/entry/2015/11/29/151235
// http://tech.camph.net/slack-bot-with-gas/
var todoistApiURL = "https://todoist.com/API/v6/sync";
var slackToken = PropertiesService.getScriptProperties().getProperty('SLACK_ACCESS_TOKEN');
var todoistToken = PropertiesService.getScriptProperties().getProperty('TODOIST_ACCESS_TOKEN')
var bot_name = "todoist";
var bot_icon = <icon URL of Todoist>;
var embedding_date = [
[-365, 0, 'スタックされた'],
[0, 1, '今日の'],
[1, 3, '三日以内の'],
[3, 7, '今週の']
]
function getUncompletedItems() {
var payload =
{
"token" : todoistToken,
"seq_no" : 0,
"resource_types" : '["all"]'
};
var options =
{
method : "post",
payload : payload,
contentType: 'application/x-www-form-urlencoded',
muteHttpExceptions : true
};
var response = UrlFetchApp.fetch(todoistApiURL, options);
var content = JSON.parse(response.getContentText());
// Logger.log(JSON.stringify(content));
return content;
}
function post() {
var content = getUncompletedItems();
var str = getTaksText(content);
var slackApp = SlackApp.create(slackToken); //SlackApp インスタンスの取得
var usr = {
username: bot_name,
icon_url: bot_icon
};
slackApp.postMessage("#todo", str, usr);
}
function getTaksText(content) {
var items = content.Items;
var user = content.User;
if (!items){
Logger.log("failed to fetch tasks from todoist");
return "failed to fetch tasks from todoist";
} else {
var content = [];
var not_dated_task = [];
items.sort(
function(a,b){
var dateA = new Date(a.due_date);
var dateB = new Date(b.due_date);
return dateA - dateB;
}
);
var now = new Date();
for each (var e in embedding_date) {
var emb_content = [];
var start = new Date(now.getFullYear(),now.getMonth(),now.getDate() + e[0]);
var end = new Date(now.getFullYear(),now.getMonth(),now.getDate() + e[1]);
for each (var t in items) {
var date = new Date(t.due_date); // Task due_date
if(start.getTime() <= date.getTime() && date.getTime() < end.getTime()){
emb_content[emb_content.length] = '> ・'+t.date_string+'\t'+t.content;
}
}
if (emb_content.length > 0) {
emb_content.unshift('>*#' + e[2] + 'Todoist:*')
Array.prototype.push.apply(content, emb_content);
}
}
if (content.length === 0) {
content.unshift('>*直近1週間でのTodoはありません。\n>Excellent work!!!*')
}
// not dated tasks
for each (var t in items) {
if (t.due_date === null) {
not_dated_task[not_dated_task.length] = '> ・' + t.content;
}
}
if (not_dated_task.length > 0) {
not_dated_task.unshift('>──────────────────────────────\n>*#日付指定の無いTodoist:*');
Array.prototype.push.apply(content, not_dated_task);
}
content[content.length] = '>──────────────────────────────\n>*#Karma: '+ user.karma +' ' + user.karma_trend + "*" ;
var str = content.join('\n');
Logger.log(str);
return str;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment