{
  "id": "d0e1NBA4VBYDxN8n",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "MS_BEST_RESTURANTS",
  "tags": [],
  "nodes": [
    {
      "id": "f8ce8570-13f0-4a6f-9b45-6ae95d011e0a",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        -640,
        -80
      ],
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {
          "chatIds": "={{ $env.telegram_channel_id }}"
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "03d40b05-8552-43d5-8daa-bb7bf0755e31",
      "name": "Parse Area",
      "type": "n8n-nodes-base.function",
      "position": [
        -260,
        -80
      ],
      "parameters": {
        "functionCode": "// expects a message like \u201c/rest Tahrir\u201d\nconst txt = $json.message?.text || '';\nconst area = txt.replace(/^(\\/rest\\s*)/i, '').trim() || 'Cairo';\nreturn [{ area }];"
      },
      "typeVersion": 1
    },
    {
      "id": "91bf1506-aa76-4db4-8cbf-aa7cc17e3c91",
      "name": "Find Restaurants (SerpAPI)",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        420,
        -80
      ],
      "parameters": {
        "url": "https://serpapi.com/search.json",
        "options": {
          "response": {
            "response": {
              "responseFormat": "json"
            }
          }
        },
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "engine",
              "value": "Maps"
            },
            {
              "name": "q",
              "value": "={{ $env.country_name }}+{{$node[\"Parse Area\"].json.area}}+restaurants"
            },
            {
              "name": "hl",
              "value": "=en"
            },
            {
              "name": "type",
              "value": "search"
            },
            {
              "name": "api_key",
              "value": "{{ $env.serp_api_key }}"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "0f1dd436-0472-47fa-af80-6ba706c6668f",
      "name": "Geocode (Nominatim)",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        80,
        -80
      ],
      "parameters": {
        "url": "https://nominatim.openstreetmap.org/search",
        "options": {
          "response": {
            "response": {
              "responseFormat": "json"
            }
          }
        },
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "format",
              "value": "json"
            },
            {
              "name": "limit",
              "value": "1"
            },
            {
              "name": "q",
              "value": "={{$node['Parse Area'].json.area}}"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "b201467a-20ad-465a-87d2-3040ca7f8c18",
      "name": "Format Reply",
      "type": "n8n-nodes-base.function",
      "position": [
        760,
        -80
      ],
      "parameters": {
        "functionCode": "/********************************************************************\n * Build a slick Telegram message from SerpAPI \u2192 Google Maps results *\n ********************************************************************/\n\nconst area = $node['Parse Area'].json.area;\n\n// helper for \u2705 / \u274c\nconst icon = v => v ? '\u2705' : '\u274c';\n\nconst list = ($json.local_results || [])\n  .sort((a, b) => (b.rating || 0) - (a.rating || 0))\n  .slice(0, 5)\n  .map((p, i) => {\n\n    // Build a Maps link that *doesn\u2019t* hit SerpAPI again\n    const { latitude: lat, longitude: lng } = p.gps_coordinates || {};\n    const mapUrl = lat && lng\n      ? `https://maps.google.com/?q=${lat},${lng}`\n      : (p.link || '');\n\n    // Service-options (English keys from Google)\n    const dineIn   = icon(p.service_options?.dine_in);\n    const takeAway = icon(p.service_options?.takeaway);\n\n    // Compose one restaurant block\n    return `${i + 1}. *${p.title}* \u2b50${p.rating || '-'}\\n`\n         + `${p.type || ''} | ${p.open_state || ''}\\n`\n         + `\u260e\ufe0f ${p.phone || '\u2014'} ${p.website ? `| \ud83c\udf10 ${p.website}` : ''}\\n`\n         + `\ud83c\udf7d\ufe0f Dine-in: ${dineIn} | Takeaway: ${takeAway}\\n`\n         + `\ud83d\udccd [On the Map](${mapUrl})`;\n  })\n  .join('\\n\\n');\n\nreturn [{\n  chat_id: $node['Telegram Trigger'].json.message.chat.id,\n  text: `\ud83c\udf7d *Best Restaurants in ${area} \u2013 Top 5*\\n\\n${list}`,\n  parse_mode: 'Markdown'\n}];"
      },
      "typeVersion": 1
    },
    {
      "id": "7eeb9f6c-b01e-4201-a50e-9879d5808577",
      "name": "Send to Telegram",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1100,
        -80
      ],
      "parameters": {
        "text": "={{$json.text}}",
        "chatId": "={{ $json.chat_id }}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "694181f1-fa81-491e-9961-39a6714a2d4f",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -320,
        -160
      ],
      "parameters": {
        "color": 4,
        "width": 220,
        "height": 260,
        "content": "## Parse Area\nProcess Input"
      },
      "typeVersion": 1
    },
    {
      "id": "599a57fb-977c-42c6-867d-d9fe96665095",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        20,
        -160
      ],
      "parameters": {
        "color": 5,
        "width": 220,
        "height": 260,
        "content": "## Geocode\nConvert area to map point"
      },
      "typeVersion": 1
    },
    {
      "id": "20c03a86-19fe-4202-ae35-3fdd3c6cad20",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        360,
        -160
      ],
      "parameters": {
        "color": 6,
        "width": 220,
        "height": 260,
        "content": "## Search\nFind Top 5 rated resturants"
      },
      "typeVersion": 1
    },
    {
      "id": "b4e32a1d-fbb7-4e8b-ab1f-7c8cbeb82987",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        700,
        -160
      ],
      "parameters": {
        "color": 7,
        "width": 220,
        "height": 260,
        "content": "## Response\nPrepare response"
      },
      "typeVersion": 1
    },
    {
      "id": "57a0f6ff-af36-4620-ba1c-a0ae0c6bdb0a",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1040,
        -160
      ],
      "parameters": {
        "color": 2,
        "width": 220,
        "height": 260,
        "content": "## Respond\nSend report to telegram"
      },
      "typeVersion": 1
    },
    {
      "id": "bbe6544a-0f7e-41a0-86ae-5a9cfed7947d",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -700,
        -300
      ],
      "parameters": {
        "color": 3,
        "width": 320,
        "height": 120,
        "content": "## Prerequisites \n**SerpAPI**: api key\n**Countery Name**: eg.. Egypt"
      },
      "typeVersion": 1
    }
  ],
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "cce0fe1d-4849-42c9-8932-6b1044ff0cd3",
  "connections": {
    "Parse Area": {
      "main": [
        [
          {
            "node": "Geocode (Nominatim)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Reply": {
      "main": [
        [
          {
            "node": "Send to Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Parse Area",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Geocode (Nominatim)": {
      "main": [
        [
          {
            "node": "Find Restaurants (SerpAPI)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find Restaurants (SerpAPI)": {
      "main": [
        [
          {
            "node": "Format Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}