Skip to content

Instantly share code, notes, and snippets.

@abfo
Last active December 7, 2022 05:22
Show Gist options
  • Select an option

  • Save abfo/4a24d356b1e2157aca0a70c2fb90bba5 to your computer and use it in GitHub Desktop.

Select an option

Save abfo/4a24d356b1e2157aca0a70c2fb90bba5 to your computer and use it in GitHub Desktop.
Apps Script using the Todoist API to set a due date of today on any tasks in the Alexa To-do List and Inbox projects that do not have a due date. For instructions on setting this up see https://ithoughthecamewithyou.com/post/using-the-todoist-api-to-set-a-due-date-on-the-alexa-integration-todo-list-with-apps-script
var ApiToken = '';
function checkForAlexaTasksWithNoDate() {
processProject('Alexa To-do List');
processProject('Inbox');
}
function processProject(projectName) {
var id = getAlexaToDoListId(projectName);
var tasks = getAlexaToDoListTasks(id);
var len = tasks.length;
for(var i = 0; i < len; i++) {
if (!tasks[i].due) {
setTaskDueToday(tasks[i].id)
}
}
}
function setTaskDueToday(taskId) {
var request = {
'due_string': 'Today'
};
UrlFetchApp.fetch('https://api.todoist.com/rest/v2/tasks/' + taskId, {
headers: {
Authorization: 'Bearer ' + ApiToken
},
'method' : 'post',
'contentType' : 'application/json',
'payload' : JSON.stringify(request, null, 2)
});
}
function getAlexaToDoListTasks(id) {
var response = UrlFetchApp.fetch('https://api.todoist.com/rest/v2/tasks?project_id=' + id, {
headers: {
Authorization: 'Bearer ' + ApiToken
}
});
return JSON.parse(response.getContentText());
}
function getAlexaToDoListId(projectName) {
var response = UrlFetchApp.fetch('https://api.todoist.com/rest/v2/projects', {
headers: {
Authorization: 'Bearer ' + ApiToken
}
});
var json = JSON.parse(response.getContentText());
var len = json.length;
for(var i = 0; i < len; i++) {
if (json[i].name == projectName) {
return json[i].id;
}
}
throw 'Could not find project';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment