Action

Todoist completed tasks

Last update almost 5 years ago

Action to get completed tasks from Todoist and put a list by name in a new draft. Gives the option of the most recent 50, or filtering to the the most recent 50 by project.

Steps

  • script

    // Get Todoist Completed items
    
    // Set up Todoist credentials
    var credential = Credential.create("Todoist token", "Todoist API");
    credential.addTextField("username", "Username");
    credential.addPasswordField("password", "Password");
    credential.authorize();
    
    //Get token
    var token = credential.getValue("password")
    var clip = app.setClipboard(token)
    
    // Ask if most recent 50, or filtered by project (which will give up to 50 by project)
    
    var p = Prompt.create();
    p.title = "Filter by project";
    p.addButton("All tasks");
    p.addButton("By project");
    var pSelect = p.show();
    var groupSelected = p.buttonPressed;
    
    project_text = ''
    if (groupSelected == "By project") {
    	
    	var http = HTTP.create(); //create HTML object
    	var response = http.request({"url":"https://api.todoist.com/rest/v1/projects","method": "GET", "headers":{"Authorization": "Bearer "+token}})
    
    	var text = response.responseText;
    	projects = JSON.parse(text)
    	var p = Prompt.create();
    	p.title = "Select project";
    	for (var project of projects) {
    		p.addButton(project.name,project.id);}
    
    	var pSelect = p.show();
    	var project_id = p.buttonPressed;
    
    	project_text = '&project_id='+project_id;}
    
    url = "https://api.todoist.com/sync/v8/completed/get_all?token="+token+'&limit=50'+project_text
    method_type = "GET"
    
    var http = HTTP.create(); // create HTTP object
    var response = http.request({
      "url": url,
      "method": method_type
    })
    
    if (response.success) {
    	var text = response.responseText;
    	console.log(text)
    	tasks = JSON.parse(text)
    }
    else {
    console.log(response.statusCode);
    	console.log(response.error);
      console.log(url)
    }
    items = tasks.items
    item_list = ''
    
    for (var i = 0; i <  items.length; i++) {
     item_list += '- ' + items[i].content + '\n'}
     
    draft_text = '# Completed Actions\n\n'
    draft_text += item_list
    
    // create a new draft, assign content and save it
    let d = Draft.create();
    d.content = draft_text;
    draft_uuid = d.uuid
    d.update();
    
    url = 'drafts5://x-callback-url/open?uuid='+draft_uuid
    var result = app.openURL(url)

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.