Drafts Action Directory

Todoist Quick Add V2 - With Notes and Reminders

Posted by @davenicholls, Last update 5 months ago

Version 2 of my Todoist Quick Add API Action.

This version adds the ability to include an optional Note and/or Reminder with the task.

Because the Quick Add API is used tasks can be added to specific projects and with specific due dates/times e.g.:

Buy beer tomorrow 1pm #Party p1
Call Pete #Work friday 3pm

The new functionality adds two new parameters, which should be added after the main task text:

–note : Allows a note to be included
–reminder : Allows a reminder time to be included

Because ‘–‘ is used to indicate the options it cannot be used as part of any regular text.

Each task can include either or both as required e.g.:

Call Fred tomorrow 9pm
Call Joe Wednesday 4pm –note This is a note
Call Pete Thursday 3pm –reminder Thursday 2:30pm
Call Sue Friday 6pm –note Add a note –reminder Friday 5:45pm
Call Helen Saturday 2pm –reminder Saturday 1pm –note One more note

You will need to add your Todoist API Token into the script where indicated.

Updated with better error handling for credentials

Install

Steps

  • script

    var credential = Credential.create("Todoist", "Todoist API");
    
    credential.addTextField("token", "Token");
    
    var result = credential.authorize();
    
    if (!result) {
    
    	alert("Failed to obtain credentials. Please check and retry");
    	context.cancel("Failed to obtain credentials");
    
    }
    else {
    
    	if (typeof(credential.getValue("token")) === 'undefined' || String(credential.getValue("token")).length == 0) {
    		alert("Todoist token is blank. Please rerun action and enter token again");
    		credential.forget();
    		context.cancel("Todoist token was blank");
    
    	}
    	else {
    
    		// check to see if draft is blank
    		var content = draft.content;
    		var check = content.length;
    
    		if (content.length == 0) {
    			alert("Draft is blank");
    			context.cancel("Draft was blank");
    		}
    		else {
    
    			// 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":credential.getValue("token"),"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

Comments

Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.