Todoist Task Parsing

Hi Drafts Community :smiley:

I used to be a Things3 user and have recently moved to Todoist and am trying to replicate the same Draft Actions I was using in Todoist.

This is my attempt at parsing out Markdown tasks into Todoist but for some reason I can’t work out how to add tags/labels to the tasks as my implementation doesn’t seem to work. I would be eternally grateful if someone could help me out with this :smiley:

var content = editor.getText();
var check = content.length;
var taskList = "";

if (content.length == 0) {
alert("Draft is blank");
context.cancel("Draft was blank");
}

// Call API for each line in a draft
// split draft and loop over lines
var lines = content.split("\n");

/* --- Todoist ---*/
let todoist = Todoist.create();
for (var line of lines) {
	if (line.includes("- [ ]")) {
		// Todoist Action
		var task = line.replace("- [ ]","");
		task.labels = "meetingTask🎙";
		task = task.trim();
		var credential = Credential.create("Todoist", "Todoist API");
		credential.addTextField("token", "Token");
		credential.authorize();
		let success = todoist.quickAdd(task);
		if (success) {
			console.log("Todoist task created: " + task);
		}
		else {
			ctErrors++;
			console.log("Todoist error: " + todoist.lastError);
		}
	}
	else {
	// do nothing with the line
	}
}

Another thing is that I am trying to make a shortcut that adds to a specific project, section and label, how would I go about referencing specific sections and what is the best way to add a task?

Thanks in advance for any advice :smiley:

In the Todoist API labels are more than just a string of text. they are a separate object with an ID.

The API documentation seems to suggest that you would reference a label in Quick Add by its ID.

I think that’s going to be where you are diverging as you are passing in some text, not an ID.

The support for Todoist actions in Shortcuts doesn’t look great, so you could take a look at utilising the URL scheme, or you could handoff to an action in Drafts and utilise the API integration as you are dong, but you would need to switch to using createTask() rather than quickAdd() as you want to access more of the functionality.

Thank you! :grin: do you happen to know the syntax for labelling things? I tried task.labels = [ID]; but it doesn’t seem to work

Maybe try the way Greg uses in this action?

You can use labels in quick add using a similar process to projects (e.g. to add a task to project work use #work) for labels you prefix with @ (e.g. @local) For both projects and labels any that you reference have to have already been created in todoist, quick add won’t create them for you.

Dave

Thanks Dave :smiley:

I just tried this but it returns an error:

todoist://addtask?content=[[title]]@MeetingTask​:studio_microphone:%20#:moneybag:%20WORK/Someday

Do you also happen to know if there’s any way to add comments?

I think you’re trying to do it slightly differently to the way I did it in my action (which is here https://actions.getdrafts.com/a/1EN).

Mine is designed to take multiple lines that look like this:

A new task Friday 10:00am #work @local --note Add this note --reminder Friday 9:00am

The action deals with the notes and reminders separately then sends the rest of the line using Quick Add.

Dave