Action

Post to Micro.blog w/location

Posted by carlesbellver, Last update over 4 years ago

Post to a Micro.blog hosted account including device location coordinates. After installing, generate an app token on your Micro.blog account page. The first time you run the action, you will be prompted to add the token.

Steps

  • script

    /*
      post to Micro.blog hosted account
      with location coordinates
      using the JSON version of the Micropub API
      https://help.micro.blog/2019/api-location/
      https://www.manton.org/2019/10/28/swarm-checkins-and.html
    
      based on 'Post to Micro.blog' action
      by agiletortoise
      https://actions.getdrafts.com/a/1Dj
    */
    
    // A category for posts
    // Empty by default: no category set
    var category = '';
    
    // 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 latitude = draft.createdLatitude;
    var longitude = draft.createdLongitude;
    var now = new Date(); // now.toISOString()
    
    postLocData = { "type": [ "h-entry" ], "properties": { "published": [ "" ], "category": [ category ], "content": [ content ], "location": [ { "type": [ "h-adr" ], "properties": { "latitude": [ latitude ], "longitude": [ longitude ] } } ] } };
    
    // create and post HTTP request
    var http = HTTP.create();
    var response = http.request({
      "url" : endpoint,
      "method" : "POST",
      "encoding" : "json",
      "data" : postLocData,
      "headers" : {
        "Content-type" : "application/json",
        "Authorization" : "Bearer " + appToken
      }
    });
    
    console.log("Response: " + response.statusCode);
    
    if (response.statusCode != 200 && response.statusCode != 202) {
      context.fail();
    }

Options

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