Action

Trello Add Card_

Last update about 5 years ago

An action to add card to Trello board. Asks user to select the board and then adds card to the bottom of the leftmost list. Title of card is first line of draft - any subsequent lines go in the description.

Steps

  • script

    // Add new card to Trello inbox - prompts for board and then adds to leftmost list.
    
    // Add credentials - prompt on first time only
    var credential = Credential.create("Trello token", "Trello API");
    credential.addTextField("token", "Token");
    credential.addPasswordField("key", "Key");
    credential.authorize();
    
    // Get token and key from credentials
    var token = credential.getValue("token")
    var key = credential.getValue("key")
    
    // Get text from draft - first line as title, rest as description field
    var title = draft.processTemplate("[[line|1]]")
    var desc = draft.processTemplate("[[line|2..]]")
    
    // Get list of boards
    base = "https://trello.com/1/"
    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);
    }
    
    var p = Prompt.create();
    p.title = "Select destination";
    
    for (var i = 0; i < boards.length; i++) {
    	p.addButton(boards[i].name, boards[i].id);
    }
    
    
    var boardSelect = p.show();
    var boardID = p.buttonPressed;
    
    // Get leftmost list from board - in my boards, this is always the inbox.
    	
    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);
    	}
    
    for (var i = 0; i < 1; i++) {
    	var inboxID = lists[i].id	}
    
    // Add card to Trello
    
    var url = 'https://api.trello.com/1/cards?idList='+inboxID
    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);
    }
    
    for (var i = 0; i < cards.length; i++) {
    	console.log(cards[i].name);
    }

Options

  • After Success Nothing
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.