{
  "id": "HSaX5xivT2KAYNcX",
  "meta": {
    "instanceId": "e9707613f506bdbcb79075f3617d27a010060d5fd1f76550283efabc82686f47",
    "templateCredsSetupCompleted": true
  },
  "name": "Telegram Command Center",
  "tags": [],
  "nodes": [
    {
      "id": "1f10be35-1365-41cf-a310-d3b3a4a6f2d0",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        -224,
        -768
      ],
      "webhookId": "ee3d0945-bb90-4edb-aeec-a12981bf91c2",
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "id": "credential-id",
          "name": "telegramApi Credential"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "5c258bb2-3ba3-44cf-bad0-7ed9c83754e0",
      "name": "format_output_as_json",
      "type": "n8n-nodes-base.code",
      "onError": "continueRegularOutput",
      "disabled": true,
      "position": [
        1456,
        -384
      ],
      "parameters": {
        "jsCode": "// Loop over input items and add a new field called 'myNewField' to the JSON of each one\nfor (const item of $input.all()) {\n// The input is an array of items. We'll work with the first item.\n\n\n// Access the string value from the 'output' field in the input JSON.\nconst rawStringWithMarkdown = item.json.output;\nconsole.log(item.json.output)\n// The JSON we want is nested inside a markdown code block (```json ... ```).\n// We need to extract just the JSON part.\n\n// Find the first occurrence of '{' to locate the start of the JSON object.\nconst jsonStartIndex = rawStringWithMarkdown.indexOf('{');\n\n// Find the last occurrence of '}' to locate the end of the JSON object.\nconst jsonEndIndex = rawStringWithMarkdown.lastIndexOf('}');\n\n// Slice the string from the start to the end to get only the clean JSON string.\n// We add +1 to jsonEndIndex because substring's second argument is exclusive.\nconst cleanJsonString = rawStringWithMarkdown.substring(jsonStartIndex, jsonEndIndex + 1);\n\n// Parse the cleaned string into a proper, usable JSON object.\nconst parsedJson = JSON.parse(cleanJsonString);\n\n// Return the parsed JSON object. The keys (symbol, sentiment_score, etc.)\n// will become individual fields in the n8n output for the next node to use.\nreturn parsedJson;}\n\n"
      },
      "executeOnce": false,
      "retryOnFail": false,
      "typeVersion": 2,
      "alwaysOutputData": false
    },
    {
      "id": "c2d15090-96f3-46f3-a39a-3d6b880feeaa",
      "name": "Generic Output",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1680,
        -816
      ],
      "webhookId": "9d8bc712-a61a-4508-a7c3-3c6ba6d723ab",
      "parameters": {
        "text": "={{ $json.output ?? \"\"  }}\n{{ $json.text ?? \"\" }}\n{{ $json.html ?? \"\" }}\n---\nCommand:{{ $node[\"Seperate command and Parameter\"].json[\"command\"] }}\nParameter: {{ $node[\"Seperate command and Parameter\"].json[\"rest\"] }}\n",
        "chatId": "={{ $node[\"Telegram Trigger\"].json[\"message\"][\"from\"][\"id\"] }}\n",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "id": "credential-id",
          "name": "telegramApi Credential"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "10b63b94-03f5-4728-9d9d-36b333eaa1fa",
      "name": "Processing has finished",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1680,
        -1040
      ],
      "webhookId": "e34df91c-d4bd-4773-b2a2-6643fc55979f",
      "parameters": {
        "text": "=Processing has finished.\n\nCommand:{{ $node[\"Seperate command and Parameter\"].json[\"command\"] }}\nParameter: {{ $node[\"Seperate command and Parameter\"].json[\"rest\"] }}\n",
        "chatId": "={{ $node[\"Telegram Trigger\"].json[\"message\"][\"from\"][\"id\"] }}\n",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "id": "credential-id",
          "name": "telegramApi Credential"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "2ddf08dd-759d-4dad-81fd-4e7fad69a628",
      "name": "Instagram post",
      "type": "n8n-nodes-base.executeWorkflow",
      "disabled": true,
      "position": [
        1248,
        -816
      ],
      "parameters": {
        "options": {},
        "workflowId": {
          "__rl": true,
          "mode": "id",
          "value": ""
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "1b33a651-c6a9-4f4a-aa69-0a123866c0e7",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -272,
        -1024
      ],
      "parameters": {
        "width": 448,
        "height": 736,
        "content": "1. Telegram message Trigger\n2. If permission granted workflow proceed unless send permission denid message to the user with his ID."
      },
      "typeVersion": 1
    },
    {
      "id": "079c8a6b-0f36-4439-baf2-6e2e5fde033a",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        208,
        -1024
      ],
      "parameters": {
        "width": 656,
        "height": 736,
        "content": "1. Seperate Command and Paremeter\n1. Set list command reply\n1. Route the flow based on command"
      },
      "typeVersion": 1
    },
    {
      "id": "57fdb67c-d3ce-4620-b9a7-1de296f15534",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        896,
        -1296
      ],
      "parameters": {
        "width": 1120,
        "height": 1088,
        "content": "Define behaviour for each command.\n1. Switch will route command to the right flow,\n1. you can call the sub workflow for relevant action, by useing sub-workflow node (just update the node)\n1. you have access to \ncommand by: $node[\"Seperate command and Parameter\"].json[\"command\"]\nand parameter by: $node[\"Seperate command and Parameter\"].json[\"rest\"]\n1. based on your result you will reply back to Telegram by relevant message"
      },
      "typeVersion": 1
    },
    {
      "id": "52b5bcbc-75a8-4aba-9686-1b1d7cfb02f2",
      "name": "Command not found",
      "type": "n8n-nodes-base.telegram",
      "position": [
        640,
        -464
      ],
      "webhookId": "7eef257a-b05d-4b1c-a653-ab7705005f4a",
      "parameters": {
        "text": "=Error: Command is not valid!\n\n{{ $('Valid Commands').first().json.validCommands }}\n\n----\nCommand:{{ $node[\"Seperate command and Parameter\"].json[\"command\"] }}\nParameter: {{ $node[\"Seperate command and Parameter\"].json[\"rest\"] }}\n",
        "chatId": "={{ $node[\"Telegram Trigger\"].json[\"message\"][\"from\"][\"id\"] }}\n",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "id": "credential-id",
          "name": "telegramApi Credential"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "663ebfab-5033-4483-a6e4-ab6d38be4413",
      "name": "Access Denied",
      "type": "n8n-nodes-base.telegram",
      "position": [
        -16,
        -496
      ],
      "webhookId": "2fd699ad-d69c-495f-b1a8-ca6783835a3d",
      "parameters": {
        "text": "=You don't have permission to use this bot!\nContact the administrator to obtain permission access.\n\nAccount ID:{{ $json.message.from.id }}",
        "chatId": "={{ $node[\"Telegram Trigger\"].json[\"message\"][\"from\"][\"id\"] }}\n",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "id": "credential-id",
          "name": "telegramApi Credential"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "86c74341-8166-48bd-8e86-d4a694fe7ed4",
      "name": "Access Control",
      "type": "n8n-nodes-base.if",
      "position": [
        -16,
        -768
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "6139880f-ae4a-43a0-836d-a1638795662d",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.message.from.id.toString() }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "317f4da6-707f-48c3-910a-4e50693b636a",
      "name": "Seperate command and Parameter",
      "type": "n8n-nodes-base.code",
      "position": [
        448,
        -784
      ],
      "parameters": {
        "jsCode": "const input = $('Telegram Trigger').first().json.message.text\nlet command, rest;\n\nif (input.startsWith('/')) {\n  const splitIndex = input.indexOf(' ');\n  if (splitIndex === -1) {\n    // No space found, entire input is the command\n    command = input;\n    rest = \"\";\n  } else {\n    command = input.substring(0, splitIndex);\n    rest = input.substring(splitIndex + 1);\n  }\n} else {\n  command = \"\";\n  rest = input;\n}\n\nreturn [{\n  json: {\n    command: command,\n    rest: rest\n  }\n}];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "a9a338a4-2814-422c-b529-1d74edb1ecef",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -64,
        -864
      ],
      "parameters": {
        "color": 3,
        "width": 224,
        "height": 80,
        "content": "1.Add Account ID here enable access for the user"
      },
      "typeVersion": 1
    },
    {
      "id": "1f9f977c-3c06-404f-8c72-347afbd5255a",
      "name": "Valid Commands",
      "type": "n8n-nodes-base.set",
      "position": [
        240,
        -784
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "4a5ded62-ca69-44fa-bce4-00ed3bf41ade",
              "name": "validCommands",
              "type": "string",
              "value": "Valid Commands:\n  /list (list all available commands)\n  /instagram (generate instagram post - parameter: quote (optional))\n  /social (check social media for the stock ticker - parameter: stock ticker)\n  /sentiment: (do sentiment analysis for the stock ticker - parameter: stock ticker)\n"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "9aa0c015-27a3-4911-8c2e-3d863c031171",
      "name": "Switch to the Command",
      "type": "n8n-nodes-base.switch",
      "position": [
        640,
        -832
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "sentiment",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "eb82524d-1fcd-4a19-a816-055de5c92e2e",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.command }}",
                    "rightValue": "/sentiment"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "instagram",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "30940d5e-bdf9-471e-9684-b66244db9112",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.command }}",
                    "rightValue": "/instagram"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "list",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "7f424524-1eee-4d02-9c9b-6c5be91defbe",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.command }}",
                    "rightValue": "/list"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "social",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "7c466b2b-7759-493b-9a2f-8965e3d7224c",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.command }}",
                    "rightValue": "/social"
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {
          "fallbackOutput": "extra"
        }
      },
      "typeVersion": 3.2
    },
    {
      "id": "91779d6a-b7ac-4d77-8211-0528614147c9",
      "name": "Social Analysis",
      "type": "n8n-nodes-base.executeWorkflow",
      "disabled": true,
      "position": [
        1248,
        -384
      ],
      "parameters": {
        "options": {},
        "workflowId": {
          "__rl": true,
          "mode": "id",
          "value": "="
        },
        "workflowInputs": {
          "value": {
            "ticker": "={{ $json.rest }}"
          },
          "schema": [
            {
              "id": "ticker",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "ticker",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "ticker"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": true
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "0f762bb5-6a18-42eb-8528-ab39bbde3686",
      "name": "Social Analysis Output",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1680,
        -384
      ],
      "webhookId": "0a425c4d-1693-4fc6-b1de-cba1d741d62d",
      "parameters": {
        "text": "=Symbol: {{ $json.symbol }}\n\nSentiment Score: {{ $json.sentiment_score }}\n\nRational: {{ $json.rationale }}\n---\nCommand:{{ $node[\"Seperate command and Parameter\"].json[\"command\"] }}\nParameter: {{ $node[\"Seperate command and Parameter\"].json[\"rest\"] }}\n",
        "chatId": "={{ $node[\"Telegram Trigger\"].json[\"message\"][\"from\"][\"id\"] }}\n",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "id": "credential-id",
          "name": "telegramApi Credential"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "aa73de3b-23e0-4f78-8cc6-3e79bd836294",
      "name": "sentimental_analysis",
      "type": "n8n-nodes-base.executeWorkflow",
      "disabled": true,
      "position": [
        1248,
        -1040
      ],
      "parameters": {
        "options": {},
        "workflowId": {
          "__rl": true,
          "mode": "id",
          "value": ""
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "06fa30d0-7d38-41da-9edd-1c685ef8cc97",
      "name": "Generic Output1",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1680,
        -592
      ],
      "webhookId": "9d8bc712-a61a-4508-a7c3-3c6ba6d723ab",
      "parameters": {
        "text": "={{ $('Valid Commands').first().json.validCommands }}\n\n---\nCommand:{{ $node[\"Seperate command and Parameter\"].json[\"command\"] }}\nParameter: {{ $node[\"Seperate command and Parameter\"].json[\"rest\"] }}\n",
        "chatId": "={{ $node[\"Telegram Trigger\"].json[\"message\"][\"from\"][\"id\"] }}\n",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "id": "credential-id",
          "name": "telegramApi Credential"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "541b48b2-1b86-4535-858c-5d4de3bde7ab",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -928,
        -1136
      ],
      "parameters": {
        "width": 624,
        "height": 848,
        "content": "# Telegram Command Center\n\n## ℹ️ What is this workflow\nThis workflow is using Telegram chat as a command center. Simple interface for user to run wokflows and receive answers by Telgram message or other mediums like email etc.\n\n## 💼 Business Cases\n- User want to initiate a wokflows by different commands\n\n## ⚙️ How Does It Work\n1. wokflow listen to telgram command from user\n1. check if user has authorize and proceed, or send access denied message with uer Account ID. Account ID can use for authorize user in the workflow\n1. Route to the relevant workflow based on command. If command doesnt exist, it will return with the list of commands\n1. If command valid, it will call the Sub-workflow with input parameter and reply back with the relevant message.\n\n## 🔗 Integrated Services\n- Telegram bot API key\n- Sub-Workflow for the command\n\n## 🛠 How to Set Up\n1. Install the workflow template.  \n2. Configure Telegram credential\n3. Update Access Control by Authorize Account ID\n4. Update Valie Commands\n5. Update List of command to switch\n6. Connect to right SUbworkflows\n7. Update/Select Telgram reply"
      },
      "typeVersion": 1
    },
    {
      "id": "e4c26f0f-deac-4f71-958a-15a4d44dc02e",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        224,
        -624
      ],
      "parameters": {
        "color": 3,
        "width": 224,
        "height": 80,
        "content": "2.Update you available command here"
      },
      "typeVersion": 1
    },
    {
      "id": "5fb4ea46-0f49-4b7f-be52-c7323a556337",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        608,
        -928
      ],
      "parameters": {
        "color": 3,
        "width": 224,
        "height": 80,
        "content": "3.Update list of commands here"
      },
      "typeVersion": 1
    },
    {
      "id": "911c5ead-a29b-4c72-8f40-e12ab10ce3f4",
      "name": "Sticky Note7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1008,
        -624
      ],
      "parameters": {
        "color": 3,
        "width": 224,
        "height": 80,
        "content": "4.Connect to relevant Sub-Wokflow and use message to reply back"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "callerPolicy": "workflowsFromSameOwner",
    "executionOrder": "v1"
  },
  "versionId": "9b90b18a-1fa1-4104-b613-46c69ace0f04",
  "connections": {
    "Access Control": {
      "main": [
        [
          {
            "node": "Valid Commands",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Access Denied",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Instagram post": {
      "main": [
        [
          {
            "node": "Generic Output",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Valid Commands": {
      "main": [
        [
          {
            "node": "Seperate command and Parameter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Social Analysis": {
      "main": [
        [
          {
            "node": "format_output_as_json",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Access Control",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "sentimental_analysis": {
      "main": [
        [
          {
            "node": "Processing has finished",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch to the Command": {
      "main": [
        [
          {
            "node": "sentimental_analysis",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Instagram post",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Generic Output1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Social Analysis",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Command not found",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "format_output_as_json": {
      "main": [
        [
          {
            "node": "Social Analysis Output",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Processing has finished": {
      "main": [
        []
      ]
    },
    "Seperate command and Parameter": {
      "main": [
        [
          {
            "node": "Switch to the Command",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}