Action

Post to Micro.blog (with Title)

Posted by @mattbirchler, Last update almost 6 years ago

This is the same as the built in micro.blog script, but you can add a post title by adding a line with “# ” (include the space) in the first line of the draft. The first line will be the title (minus the pound sign and space) and everything below that will be the content.

If there is no “# “ on the first line, the entire draft is assumed to be the content and no title will be added.

Steps

  • script

    /*
      post to Micro.blog hosted account
      using Micropub API
    */
    
    // First run, you will be prompted for
    // App Token. Generate tokens on the Micro.blog
    // Account page
    var credential = Credential.create("Micro.blog", "Insert Micro.blog app token generated on Micro.blog account page.");
    
    credential.addTextField("apptoken", "App Token");
    credential.authorize();
    
    var appToken = credential.getValue("apptoken");
    
    // Setup micro.blog API and content 
    var endpoint = "https://micro.blog/micropub"
    var content = draft.content
    var array_of_content = draft.content.split("\n");
    var post_title = "";
    if (array_of_content[0].charAt(0) == "#") {
    	post_title += array_of_content[0].slice(2);
    }
    
    var real_content = ""
    
    for (i = 1; i < array_of_content.length; i++) {
    	real_content += array_of_content[i] + "\n";
    }
    
    // create and post HTTP request
    var http = HTTP.create();
    
    if (post_title == "") {
    var response = http.request({
      "url": endpoint,
      "method": "POST",
      "encoding": "form",
      "data": {
      		"h": "entry",
      		"content": content
      },
      "headers": {
        "Authorization": "Bearer " + appToken
      }
    });
    } else {
    var response = http.request({
      "url": endpoint,
      "method": "POST",
      "encoding": "form",
      "data": {
        "h": "entry",
        "name": post_title,
        "content": real_content
      },
      "headers": {
        "Authorization": "Bearer " + appToken
      }
    });
    }
    
    console.log("Response: " + response.statusCode);
    
    if (response.statusCode != 200 && response.statusCode != 202) {
      context.fail();
    }

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.