Telegram trigger - split command

Hello,

I am trying to setup a bot that is triggered in group chat by “!p {input}” , {input} is user defined, I can’t find a way to separate “!p” from {input} so I can use that input to query database.
Let me give you an example, user wants to know the price of bitcoin, user types !p btc, boot looks up price of btc. but if user changes btc to something else I want the bot to understand new variable.

Thank you

Hey @marcondy!

You can use the replace() method to replace !p in incoming text and pass it on the IF node or the Switch node to check for the conditions.

You can use the replace() method either in the Function node or in the Expression Editor.

Hope this helps :slightly_smiling_face:

@marcondy below is the workflow that might help

{
  "nodes": [
    {
      "parameters": {
        "dataType": "string",
        "value1": "={{$node[\"Telegram Trigger\"].json[\"message\"][\"text\"].replace('!p ','')}}",
        "rules": {
          "rules": [
            {
              "value2": "btc"
            }
          ]
        },
        "fallbackOutput": 3
      },
      "name": "Switch",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 1,
      "position": [
        630,
        350
      ]
    },
    {
      "parameters": {
        "updates": [
          "*"
        ],
        "additionalFields": {}
      },
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1,
      "position": [
        430,
        350
      ],
      "webhookId": "",
      "credentials": {
        "telegramApi": ""
      }
    }
  ],
  "connections": {
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

The expression {{$node["Telegram Trigger"].json["message"]["text"].replace('!p ','')}} replaces !p with an empty string. So if a user sends !p btc, in the Switch node it will only check for btc.

1 Like

Thank you !

1 Like