curltelegramtelegram-botwebhookstelegram-webhook

HTTP POST using Telegram Webhook


I have an endpoint that I run using curl command

curl -X POST https://example.com/control -d "channel=X&turn=XXX&id=XXXXXX&auth_key=XXXXX"

I want to bind between Telegram Webhook and that POST , so each time my Telegram bot will get a message it POST to that URL

I tried to register a Telegram webhook:

https://api.telegram.org/bot<telegram_token>/setWebhook?url=https://example.com/control?channel=X&turn=XXX&id=XXXXXX&auth_key=XXXXX

But then I see that the POST sent only for

https://example.com/control?channel=X

Is there a way to attach more data using Telegram webhook?


Solution

  • The problem seems to be that curl understands additional query parameters (turn, id, auth_key) to be parameters for https://api.telegram.org, not for the url that you pass in the url parameter. You can check this with https://httpbin.org :

    curl -X POST https://httpbin.org/anything -d "url=https://example.com/control?channel=X&turn=XXX&id=XXXXXX&auth_key=XXXXX"
    

    The returned object contains:

    "form": {
        "auth_key": "XXXXX",
        "id": "XXXXXX",
        "turn": "XXX",
        "url": "https://example.com/control?channel=X"
    },
    

    Solution is to use --data-urlencode flag instead of -d (which is --data):

    curl -X POST "https://httpbin.org/anything" --data-urlencode "url=https://example.com/control?channel=X&turn=XXX&id=XXXXXX&auth_key=XXXXX"
    

    Response:

    "form": {
        "url": "https://example.com/control?channel=X&turn=XXX&id=XXXXXX&auth_key=XXXXX"
    },
    

    Telegram request:

    curl -X POST https://api.telegram.org/bot<telegram_token>/setWebhook --data-urlencode "url=https://example.com/control?channel=X&turn=XXX&id=XXXXXX&auth_key=XXXXX"