Action

Trello example using functions

Posted by moggy1972, Last update 2 months ago - Unlisted

An example of using action steps to set up multiple Javascript functions, which can then be used in subsequent script actions. In real life, I have a single action with a script step incliuding all of the Trello functions and one for all of the Drafts functions. I can bring them into actions using the “Include Action” step, when I need them.

Steps

  • script

    // Trello Functions for use with create new card
    
    // Action with list of Trello javascript functions
    
    // trello_credentials
    // trello_get_boards
    // trello_pick_main_boards
    // trello_get_lists_from_board
    // trello_select_list - picks list based on some text
    
    // Sort out credentials /////////////////////////////////////////
    function trello_credentials() {
    
    	var credential = Credential.create("Trello token", "Trello API");
    	credential.addTextField("token", "Token");
    	credential.addPasswordField("key", "Key");
    	credential.authorize();
    
    	//Get token
    	var token = credential.getValue("token")
    	var key = credential.getValue("key")
    
    	// Basic URL for Trello
    	base = "https://trello.com/1/"
    
    	arr = [token,key,base]
    	return arr
    
    }
    
    // Get list of boards ////////////////////////////////////////////
    function trello_get_boards() {
    
    	url = base + "members/me/boards"
    	url = url + "?&key="+key+"&token="+token
    	var http = HTTP.create(); //create HTML object
    	var response = http.request({
    	"url":url,
    	"method": "GET"
    		})
    
    	if (response.success) {
    		var text = response.responseText;
    		//console.log(text)
    		boards = JSON.parse(text)
    	}
    	else {
    		//console.log(response.statusCode);
    		//console.log(response.error);
    	}
    	return boards
    }
    
    // Select from list of boards (capturing name and id) ///////////////////////
    function trello_pick_main_boards(board_name) {
    	
    	if (board_name.length > 0) {
    		for (var i = 0; i < boards.length; i++) {
    			if (boards[i].name.includes(board_name)) {
    				boardID = boards[i].id 
    			}
    		}
    	}
    	else {	
    		var p = Prompt.create();
    		p.title = "Select board";
    
    		for (var i = 0; i < boards.length; i++) {
    				p.addButton(boards[i].name, boards[i].id);				// in my personal of this script, I only add buttons for boards whose name starts with "." 
    		}
    	
    		var boardSelect = p.show();
    		var boardID = p.buttonPressed;
    	}
    	return boardID
    	
    }
    
    // Get lists from board
    function trello_get_lists_from_board(boardID) {
    	
    	url = base + "boards/"+boardID+"/lists"
    	url = url + "?&key="+key+"&token="+token
    	var http = HTTP.create(); //create HTML object
    	var response = http.request({
    		"url":url,
    		"method": "GET"
    		})
    
    	if (response.success) {
    		var text = response.responseText;
    		console.log(text)
    		lists = JSON.parse(text)
    		}
    	else {
    		console.log(response.statusCode);
    		console.log(response.error);
    		}
    		
    	return lists
    
    }
    
    // Select List from Trello Board that includes specific text
    function trello_select_list(lists, list_text) {
    for (var j = 0; j < lists.length; j++) {
    	if (lists[j].name.includes(list_text)){
    		var listID = lists[j].id		}}
    		
    return listID
    
    }
    
    // Add card to Trello ////////////////////////////////
    function trello_add_card(listID) {
    
    	var url = 'https://api.trello.com/1/cards?idList='+listID
    	url = url + '&key='+key+'&token='+token
    
    	//Get Trello Data
    	var http = HTTP.create(); //create HTML object
    	var response = http.request({
    	"url":url,
    	"method": "POST",
    	"data": {"name":title,"desc":desc}})
    
    	if (response.success) {
    		var text = response.responseText;
    		console.log(text)
    		cards = JSON.parse(text)
    		}
    	else {
    		console.log(response.statusCode);
    		console.log(response.error);
    	}
    
    		cardID = cards.id
    		return cardID
    	
    }
    
    function trello_get_cards_from_list() {
    
    // Get cards from list
    	
    	url = base + "lists/"+listID+"/cards"
    	url = url + "?&key="+key+"&token="+token+"&limit=1000&fields=all"
    	var http = HTTP.create(); //create HTML object
    	var response = http.request({
    	"url":url,
    	"method": "GET"
    		})
    
    	if (response.success) {
    		var text = response.responseText;
    		//console.log(text)
    		cards = JSON.parse(text)
    		}
    	else {
    		//console.log(response.statusCode);
    		//console.log(response.error);
    	}
    	return cards
    }
  • script

    // Drafts Javascript functions
    
    function set_device() {
    // I have a daily note in my system which is where I keep my daily action list.  Because I can't sync to iPad, I need scripts to know the UUID on all my devices.
    	if (device.model == "iPad"){
    		log_uuid = "B92D4B2D-FE84-4941-959B-C9E543BB885B"
    	} else {
    		log_uuid = "B96FBFB4-F0B7-454F-8C4B-D61EE221918F"
    		}
    	return log_uuid
    	
    }
    
    function get_content() {
    	var content = draft.content.trim();
    	var title = draft.processTemplate("[[line|1]]")
    	var desc = draft.processTemplate("[[line|2..]]")
    
    	if (content.length == 0 || draft.uuid == log_uuid) {
       	var p = Prompt.create()
       	p.addTextField("text_input","Enter text:", "")
       	p.addButton("Select");
       	var didSelect =p.show();
       	var text_input = p.fieldValues["text_input"];
       	if (p.buttonPressed == "Select"){
       		content = text_input
       		title = text_input
       		desc = ""
       		}
       }
       return [title,desc]
    
       
    }
    
    /////////////////////////////
    // Return to inbox/log
    
    function return_to_inbox() {
    
    	if (draft.uuid != log_uuid) {
    		draft.isTrashed = true
    		draft.update()
    		}
    	
    	// Open Dashboard
    		var group = ActionGroup.find("Dashboard")
    		app.loadActionGroup(group)
    
    	// Open Inbox
    	var workspace = Workspace.find("Inbox")
    	app.applyWorkspace(workspace)
    	url = "drafts5://open?uuid=" + log_uuid
    	return(url)
    	
    }
    
    
  • script

    // Create new Trello card script using Trello and Drafts functions above
    // Needs a list called "Inbox" to work - although you can call it anything. A previous version just picked the leftmost list.
    
    log_uuid = set_device()
    
    arr = trello_credentials()
    token = arr[0]
    key = arr[1]
    base = arr[2]
    
    arr = get_content()
    title = arr[0]
    desc = arr[1]
    
    boards = trello_get_boards()
    boardID = trello_pick_main_boards("")
    lists = trello_get_lists_from_board(boardID)
    listID = trello_select_list(lists,"Inbox")
    new_card_id = trello_add_card(listID)
    
    //This function deletes the note and returns me to my inbox. It won't work unless you add the UUID into the drafts function "set device settings" in the note above.
    //url = return_to_inbox()
    //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.