DEV Community

Sh Raj
Sh Raj

Posted on • Edited on

Manage Telegram Webhooks Using curl

Managing Telegram Webhooks Using curl

Telegram bot webhooks allow you to receive updates and interact with users through a specified URL. This article covers how to set up, get, and delete webhooks for your Telegram bot using curl, along with explanations of all possible webhook-related operations.

Prerequisites

  • A Telegram bot token, which you can obtain from BotFather.
  • A publicly accessible server or a tool like ngrok to expose your local server.

1. Setting a Webhook

To set a webhook for your Telegram bot, use the setWebhook method. This method tells Telegram where to send updates for your bot.

Example Command

curl -F "url=https://yourdomain.com/uibot" https://api.telegram.org/bot<your-bot-token>/setWebhook
Enter fullscreen mode Exit fullscreen mode
curl https://api.telegram.org/bot<your-bot-token>/setWebhook?url=https://yourdomain.com/uibot
Enter fullscreen mode Exit fullscreen mode

Replace <your-bot-token> with your bot's token and https://yourdomain.com/uibot with your server's URL.

Response

A successful response should look like this:

{
  "ok": true,
  "result": true,
  "description": "Webhook was set"
}
Enter fullscreen mode Exit fullscreen mode

Detailed Command Example

For a bot token 123456789:ABCdefGHIjklMNO_pqrSTUvwxYZ and a URL https://58a1-223-176-63-126.ngrok-free.app/uibot, the command would be:

curl -F "url=https://58a1-223-176-63-126.ngrok-free.app/uibot" https://api.telegram.org/bot123456789:ABCdefGHIjklMNO_pqrSTUvwxYZ/setWebhook
Enter fullscreen mode Exit fullscreen mode

or Simply

curl https://api.telegram.org/bot123456789:ABCdefGHIjklMNO_pqrSTUvwxYZ/setWebhook
Enter fullscreen mode Exit fullscreen mode

Do a get request on setWebhook it will automatically delete the webhook for security reasons(personally observed)

2. Getting Webhook Information

To get information about the current webhook, use the getWebhookInfo method. This method provides details about the webhook, such as the URL, pending updates, and the last error encountered.

Example Command

curl https://api.telegram.org/bot<your-bot-token>/getWebhookInfo
Enter fullscreen mode Exit fullscreen mode

Response

A successful response might look like this:

{
  "ok": true,
  "result": {
    "url": "https://yourdomain.com/uibot",
    "has_custom_certificate": false,
    "pending_update_count": 0,
    "last_error_date": 0,
    "last_error_message": "",
    "max_connections": 40,
    "allowed_updates": null
  }
}
Enter fullscreen mode Exit fullscreen mode

3. Deleting a Webhook

To remove the webhook and switch to getUpdates mode, use the deleteWebhook method.

Example Command

curl https://api.telegram.org/bot<your-bot-token>/deleteWebhook
Enter fullscreen mode Exit fullscreen mode

Response

A successful response should look like this:

{
  "ok": true,
  "result": true,
  "description": "Webhook was deleted"
}
Enter fullscreen mode Exit fullscreen mode

4. Setting a Webhook with Additional Parameters

Telegram allows you to set additional parameters when setting a webhook, such as the maximum number of connections or specific types of updates.

Example Command

curl -F "url=https://yourdomain.com/uibot" \
     -F "max_connections=100" \
     -F "allowed_updates=message,edited_channel_post,callback_query" \
     https://api.telegram.org/bot<your-bot-token>/setWebhook
Enter fullscreen mode Exit fullscreen mode

Parameters

  • url: Required. HTTPS URL to send updates to.
  • max_connections: Optional. Maximum allowed number of simultaneous HTTPS connections to the webhook (1-100). Defaults to 40.
  • allowed_updates: Optional. JSON-serialized list of update types you want your bot to receive.

Example with Parameters

curl -F "url=https://58a1-223-176-63-126.ngrok-free.app/uibot" \
     -F "max_connections=100" \
     -F "allowed_updates=message,edited_channel_post,callback_query" \
     https://api.telegram.org/bot123456789:ABCdefGHIjklMNO_pqrSTUvwxYZ/setWebhook
Enter fullscreen mode Exit fullscreen mode

5. Getting Updates in getUpdates Mode

If you do not set a webhook, you can still get updates using the getUpdates method.

Example Command

curl https://api.telegram.org/bot<your-bot-token>/getUpdates
Enter fullscreen mode Exit fullscreen mode

Response

A successful response should look like this:

{
  "ok": true,
  "result": [
    {
      "update_id": 123456789,
      "message": {
        "message_id": 1,
        "from": {
          "id": 123456789,
          "is_bot": false,
          "first_name": "John",
          "last_name": "Doe",
          "username": "johndoe",
          "language_code": "en"
        },
        "chat": {
          "id": 123456789,
          "first_name": "John",
          "last_name": "Doe",
          "username": "johndoe",
          "type": "private"
        },
        "date": 1610000000,
        "text": "Hello, world!"
      }
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Using curl, you can easily manage your Telegram bot's webhooks, whether you're setting, getting, or deleting them. This guide should help you get started with Telegram webhooks and ensure your bot can handle updates efficiently.

Summary of Commands

  • Set Webhook:
  curl -F "url=https://yourdomain.com/uibot" https://api.telegram.org/bot<your-bot-token>/setWebhook
Enter fullscreen mode Exit fullscreen mode
  • Get Webhook Info:
  curl https://api.telegram.org/bot<your-bot-token>/getWebhookInfo
Enter fullscreen mode Exit fullscreen mode
  • Delete Webhook:
  curl https://api.telegram.org/bot<your-bot-token>/deleteWebhook
Enter fullscreen mode Exit fullscreen mode
  • Get Updates (Without Webhook):
  curl https://api.telegram.org/bot<your-bot-token>/getUpdates
Enter fullscreen mode Exit fullscreen mode

Feel free to modify the example commands with your actual bot token and URL to suit your needs.

Top comments (1)

Collapse
 
sh20raj profile image
Sh Raj