Integrating Swarm Check-Ins with Micro.blog

(replying to @settee)

The 🗺️ emoji is supposed to tag the post as in the “Travel” category, which can then be selected/deselected in the Discover page.

As far as integration, I registered with Foursquare for a developer API key, and then in the project’s settings there is a “Push API” section to specify a URL to send a POST webhook (in this case, a Google Apps Script that processes the payload and creates the post on my micro.blog.

@aaronpk has also created ownyourswarm.com that does this automagically with some additional functions.

function doPost(e) {
  DriveApp.getFolderById('XXX').createFile(`Swarm Trigger ${new Date().valueOf()}.json`, JSON.stringify(e), 'application/json');
  var checkin = JSON.parse(e.parameter.checkin);
  if (checkin.visibility != 'private') {
    var checkinmap = Maps.newStaticMap().setSize(600, 600).setFormat(Maps.StaticMap.Format.PNG).addMarker(checkin.venue.location.lat, checkin.venue.location.lng);
    doMicroBlog(checkin, checkinmap);
  }
}

/* Micro.blog */
function doMicroBlog(checkin, checkinmap) {
  var checkinmapupload = JSON.parse(UrlFetchApp.fetch(
    "https://micro.blog/micropub/media", {
    "method": "POST",
    "headers": {
      "Authorization": "Bearer XXX"
    },
    "payload": {
      "file": checkinmap.getBlob()
    }
  }
  ).getContentText());
  var payload = {
    "type": ["h-entry"],
    "properties": {
      "published": [new Date(checkin.createdAt * 1000).toISOString()],
      "content": [
        { "html": `<p>🗺 ${checkin.venue.categories[0].name} in ${checkin.venue.location.city}, ${checkin.venue.location.state}</p><p>Checked in on <a href="https://swarmapp.com">Swarm</a> App.</p>` }
      ],
      "photo": [checkinmapupload.url],
      "checkin": [
        {
          "type": ["h-card"],
          "properties": {
            "name": [checkin.venue.name],
            "url": [`https://foursquare.com/v/${checkin.venue.id}`],
            "latitude": [checkin.venue.location.lat],
            "longitude": [checkin.venue.location.lng]
          }
        }
      ]
    }
  }
  var request = UrlFetchApp.fetch(
    "https://micro.blog/micropub", {
    "method": "POST",
    "headers": {
      "Authorization": "Bearer XXX"
    },
    "payload": JSON.stringify(payload)
  }
  );
  console.log(request.getContentText());
}