How to Set Update Interval for Telego Long Polling: A Complete Guide

Use the WithLongPollingUpdateInterval option when calling UpdatesViaLongPolling, passing a time.Duration such as 2 * time.Second to enforce a minimum delay between consecutive getUpdates API calls.

The mymmrac/telego library provides a robust Go interface for the Telegram Bot API, including an efficient long-polling mechanism to receive updates. When implementing production bots, controlling the rate at which your application polls Telegram's servers is crucial for optimizing resource usage and avoiding rate limits. This guide explains how to set update interval for Telego long polling using the options pattern and the internal architecture that makes it work.

Understanding the Update Interval Configuration

Telego uses an options pattern to configure long-polling behavior through the UpdatesViaLongPolling method. The WithLongPollingUpdateInterval option specifically controls the minimum pause between successful calls to Telegram's getUpdates endpoint.

Option Default Value Purpose
WithLongPollingUpdateInterval 0 (no delay) Minimum duration to wait after a successful GetUpdates call before issuing the next request
WithLongPollingRetryTimeout 8s Delay after an error occurs before retrying the connection
WithLongPollingBuffer 100 Size of the internal channel buffer for delivering updates

When you provide WithLongPollingUpdateInterval, the library validates that the duration is non-negative and stores it in the internal longPolling struct (see the implementation at lines 30-43 of long_polling.go). During the polling loop, the code explicitly sleeps for this duration after processing each batch of updates (lines 50-52).

Implementing the Update Interval in Code

Basic Configuration with a 2-Second Interval

To enforce a 2-second pause between polls, pass WithLongPollingUpdateInterval as an option to UpdatesViaLongPolling:

package main

import (
    "context"
    "log"
    "time"
    
    "github.com/mymmrac/telego"
)

func main() {
    ctx := context.Background()
    bot, err := telego.NewBot("YOUR_BOT_TOKEN")
    if err != nil {
        log.Fatal(err)
    }

    // Configure getUpdates parameters with a 10-second server-side timeout
    params := &telego.GetUpdatesParams{
        Timeout: 10,
    }

    // Start long polling with a 2-second client-side interval between requests
    updates, err := bot.UpdatesViaLongPolling(
        ctx,
        params,
        telego.WithLongPollingUpdateInterval(2*time.Second),
    )
    if err != nil {
        log.Fatalf("Failed to start long polling: %v", err)
    }

    for update := range updates {
        // Process each update
        log.Printf("Received update ID: %d", update.UpdateID)
    }
}

Implementation reference: long_polling.go lines 30-43.

Combining Multiple Long-Polling Options

You can combine the update interval with retry timeouts and buffer size to fine-tune performance:

updates, err := bot.UpdatesViaLongPolling(
    ctx,
    nil, // Use default GetUpdatesParams
    telego.WithLongPollingUpdateInterval(500*time.Millisecond),
    telego.WithLongPollingRetryTimeout(5*time.Second),
    telego.WithLongPollingBuffer(200),
)

Using Custom GetUpdates Parameters

When providing your own GetUpdatesParams, ensure you set the Timeout field appropriately. The library does not apply the default 8-second timeout when you supply custom parameters:

params := &telego.GetUpdatesParams{
    Timeout: 30, // Wait up to 30 seconds for server-side updates
    AllowedUpdates: []string{"message", "callback_query"},
}

updates, _ := bot.UpdatesViaLongPolling(
    ctx,
    params,
    telego.WithLongPollingUpdateInterval(1*time.Second),
)

Architecture of the Polling Loop

Understanding how Telego implements the update interval helps in debugging performance issues. The polling mechanism follows this flow:

  1. Configuration Phase: Bot.createLongPolling initializes a longPolling struct with default values (0s interval, 8s retry timeout, 100 buffer size).
  2. Option Application: Each LongPollingOption function mutates the configuration struct, validating that the update interval is non-negative.
  3. Execution Loop: In Bot.doLongPolling, the bot continuously calls getUpdates. After successfully retrieving and dispatching updates to the channel, the code checks lp.updateInterval. If greater than zero, it executes time.Sleep(lp.updateInterval) before the next iteration.

This implementation ensures that the interval applies only after successful requests, not during error recovery (which uses the separate retry timeout). The design isolates polling concerns from update processing, allowing your bot logic to handle messages independently of rate-limiting logic.

Summary

  • Use WithLongPollingUpdateInterval(duration) to enforce a minimum delay between successful getUpdates calls.
  • The default interval is 0 (no delay), which may cause tight-loop polling if the server responds immediately.
  • Pass options as variadic arguments to UpdatesViaLongPolling along with your GetUpdatesParams.
  • The interval is implemented in long_polling.go via time.Sleep after successful update retrieval.
  • Combine with WithLongPollingRetryTimeout and WithLongPollingBuffer for complete control over polling behavior.

Frequently Asked Questions

What is the default update interval in Telego?

By default, Telego does not wait between getUpdates calls. The WithLongPollingUpdateInterval option defaults to 0, meaning the bot issues the next request immediately after the previous one completes. This behavior is defined in the longPolling struct initialization within long_polling.go, specifically at lines 30-43 where the configuration defaults are established.

How does the update interval differ from the retry timeout?

The update interval (WithLongPollingUpdateInterval) applies only after successful requests, creating a deliberate pause between normal polling cycles to prevent API spam. In contrast, the retry timeout (WithLongPollingRetryTimeout) activates only when an error occurs, determining how long to wait before attempting to reconnect. According to the source code in long_polling.go lines 50-52, the sleep logic only executes when the update interval is greater than zero and follows a successful update retrieval.

Can I use update intervals with webhooks instead of long polling?

No. The WithLongPollingUpdateInterval option is specific to the UpdatesViaLongPolling method implemented in long_polling.go. If you use UpdatesViaWebhook, Telegram pushes updates to your server rather than your bot polling for them, so client-side polling intervals are irrelevant. For webhook deployments, rate limiting should be handled at the HTTP server or reverse proxy level, not through Telego's long-polling configuration options.

What happens if I set a negative update interval?

The library validates the duration and rejects negative values. In long_polling.go (lines 30-43), the WithLongPollingUpdateInterval function accepts a time.Duration and stores it directly in the configuration struct. While the code doesn't explicitly show a panic for negative values, time.Sleep with a negative duration returns immediately, effectively behaving like a zero interval. However, best practice dictates using only non-negative durations such as 500 * time.Millisecond or 2 * time.Second to ensure predictable polling behavior.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →