💬

n8nを使ってSwarm(Foursquare)のチェックインをXにポストする

に公開


完成形

流れ
Swarmのチェックインをn8nのwebhookに送信→詳細データを取得(webhookに詳細データが含まれてないので)→テキスト整形→Xに投稿です。

APIの準備

X API

https://qiita.com/neru-dev/items/857cc27fd69411496388
こちらなどを参考にX Developer Platformに登録
n8nでCreate Credentials→X OAuth2 APIを選択
OAuth Redirect URL(X API設定画面のRedirect URL欄に入力しておく)
Client ID(X API設定画面からコピペ)
Client Secret(X API設定画面からコピペ)
を登録

Foursquare API

https://foursquare.com/developer/
https://foursquare.com/pricing/#places_api
(月1万回のコールまで無料なので)チェックイン回数が多くないなら無料プランで賄えます
使い道浮かんでないですが、Developer登録時に200ドル分のクレジットがチャージされます。

適当なプロジェクトを作成して「Client Id」「Client Secret」をメモ
Push URL

https://n8nのサーバー/webhook/swarmchekin

にしておく

n8nでCreate Credentials→OAuth2 API
Grant Type:Authorization Code
Authorization URL:https://foursquare.com/oauth2/authenticate
Access Token URL:https://foursquare.com/oauth2/access_token
Client ID(Foursquare API設定画面からコピペ)
Client Secret(Foursquare API設定画面からコピペ)
OAuth Redirect URL(Foursquare API設定画面のRedirect URL欄に入力しておく)

n8nでワークフローの作成

n8nのワークフローは↓をコピペできます
4sq・XのCredentialsはそれぞれ設定してください

{
  "nodes": [
    {
      "parameters": {
        "text": "={{ $('TextFormatting').item.json.fullMessage }}",
        "additionalFields": {}
      },
      "id": "a4c6b340-64d7-43f4-a5fe-69dae29ff572",
      "name": "X",
      "type": "n8n-nodes-base.twitter",
      "typeVersion": 2,
      "position": [
        220,
        160
      ],
      "credentials": {
        "twitterOAuth2Api": {
          "id": "",
          "name": "X account"
        }
      }
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "swarmchekin",
        "options": {}
      },
      "id": "1fbe1b0a-32b8-4afa-86af-ef9f8121bd65",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -440,
        160
      ],
      "webhookId": "ddc39219-2d5c-4ea9-a841-67e32bcbd19b"
    },
    {
      "parameters": {
        "url": "=https://api.foursquare.com/v2/checkins/{{ $json.checkinId }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "oAuth2Api",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "v",
              "value": "20231024"
            }
          ]
        },
        "options": {}
      },
      "id": "d71bb116-a3a6-4653-b393-7804362db3c0",
      "name": "GetCheckinDetail",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -100,
        160
      ],
      "credentials": {
        "oAuth2Api": {
          "id": "",
          "name": "Foursquare(Swarm) API"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "\n// 投稿メッセージ\nconst state = $json.response.checkin.venue.location.state || null;\nconst city = $json.response.checkin.venue.location.city || null;\nconst name = $json.response.checkin.venue.name;\nconst url = $json.response.checkin.checkinShortUrl;\n\nconst message = (state ? state : \"\") + (city ? city : \"\") + \"\" + name + \"」でチェックインしました。\" ;\n\n// メッセージとURLを結合\nconst fullMessage = `${message}${url}`;\n\n// これを次のHTTPノードに渡す\nreturn {\n  json: {\n    fullMessage: fullMessage,\n    url: url\n  }\n};"
      },
      "id": "0a60b3d5-a04a-4528-8a71-befee3186f2c",
      "name": "TextFormatting",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        60,
        160
      ]
    },
    {
      "parameters": {
        "jsCode": "const checkinData = JSON.parse($json.body.checkin);\nreturn {\n  json: {\n    checkinId: checkinData.id\n  }\n};\n"
      },
      "id": "0e81c2d5-fbf1-4b2d-b9e7-1ee778479de2",
      "name": "GetCheckinID",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -260,
        160
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "GetCheckinID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GetCheckinDetail": {
      "main": [
        [
          {
            "node": "TextFormatting",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "TextFormatting": {
      "main": [
        [
          {
            "node": "X",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GetCheckinID": {
      "main": [
        [
          {
            "node": "GetCheckinDetail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": ""
  }
}

Discussion