Action

Todoist Quick Add V3 - With Notes, Reminders, and default project

Posted by davenicholls, Last update over 1 year ago

UPDATES

over 1 year ago

Updated to use V9 of the Todoist API

Version 3 of my Todoist Quick Add API Action. Updated 14 Jan 2020 to use the V8 API

Updated 19/9/2020 to include the new auto_reminder parameter added to the API to ensure that auto reminder times are honoured. If you don’t want this then check the JS code, there is a comment that explains how to turn it off.
Updated to use the
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

You can also add notes and reminders. These should be added using the following parameters, 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

It is also possible to set a default project that will be used for subsequent tasks. Set the default project by putting #projectname at the beginning of a new line. Examples:

Write letter to Santa 1st December <- This will go to the inbox
Delete Facebook #personal tomorrow <- This will go into personal
#work <- This sets the default project to work
File Expenses friday 9am <- WIll be added to work
Clean Shoes #personal <- Will ignore default and go to personal
Organise planning meeting <- Added to work
#holiday <- Sets new default project
Book flights <- Added to holiday
Buy suitcase <- Added to holiday

Note: Unlike the Quick Add entries in the Todoist App, the quick add API doesn’t work with multi word project names. It also doesn’t automatically add new projects.

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 = "";
    			var defaultProject = "";
    
    			// 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 proj = line.match(/^#(\S+)\s*/m);
    
    					if (proj) {
    						defaultProject = proj[1];
    						newDraft+= line + "\n"
    					}
    					else {
    
    						var parts = line.split("--");
    
    						if (!parts[0].match(/#/m) && defaultProject != "") {
    							mainPart= parts[0]+' #'+defaultProject;
    						}
    						else {
    							mainPart=parts[0];
    						}
    						// Modified 19/09/2020 to add auto_reminder. If you don't want auto_reminders added change 'true' to 'false'
    						var params = {"token":credential.getValue("token"),"text":mainPart,"auto_reminder":true};
    
    
    						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/v9/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.