Action

Todoist Quick Add V2 - With Notes and Reminders

Posted by davenicholls, Last update over 6 years ago

Steps

  • script

    // check to see if draft is blank
    var content = draft.content;
    var check = content.length;
    
    var todoistToken = "Insert Your Todoist API Token Here";
    
    if (content.length == 0) {
    	alert("Draft is blank");
    	context.cancel("Draft was blank");
    }
    
    // Call API for each line in a draft
    
    var newDraft = "";
    
    // split draft and loop over lines
    var lines = draft.content.split("\n");
    
    var http = HTTP.create(); // create HTTP object
    
    for (var line of lines) {
    
    	if (line.length == 0) {
    
    		newDraft+="\n"
    	}
    	else {
    
    		var parts = line.split("--")
    
    		var params = {"token":todoistToken,"text":parts[0]}
    		
    		if (parts.length != 1) {
    
    			for (i = 1; i < parts.length; i++) {
    
    				var opt=parts[i].split(" ",1);
    
    				switch (String(opt)) {
    
    					case 'note':
    						params['note']=parts[i].slice(String(opt).length);
    						break;
    						
    					case "reminder":
    						params['reminder']=parts[i].slice(String(opt).length);
    						break;
    	
    					default:
    					  	console.log("Bad parameter - "+ opt);
    						break;								}
    			}
    		}
    
    		var response = http.request({
    		  	"url": "https://todoist.com/api/v7/quick/add",
    	  		"method": "POST",
    		  	"data": params
    		});
    
    		if (response.success) {
    			newDraft+= line + " - OK\n"
    		}
    		else {
    			newDraft+= line + " - Failed\n"
    		  	console.log(response.statusCode);
    		  	console.log(response.error);
    		}
    	}
    }
    
    editor.setText(newDraft)
    

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.