Maximum number of updates retrievable via Telegram bot API

Hey everyone! I’ve been messing around with the Telegram bot API and I’m hitting a snag. I’m trying to fetch updates for my bot, but it looks like I can only get 100 at a time. Is this a hard limit? I’m pretty sure there are more updates I’m not seeing.

Here’s what I’ve been doing:

import requests

bot_token = 'my_secret_token'
url = f'https://api.telegram.org/bot{bot_token}/getUpdates'

response = requests.get(url)
updates = response.json()['result']

print(f'Number of updates: {len(updates)}')

This always prints ‘100’ even though I know there should be more. Any ideas on how to grab the rest of the updates? Is there a trick to paginate or something? Thanks in advance for any help!

hey jack, i’ve run into this too. the 100 limit is default, but you can use the offset parameter to get more. try adding ?offset=101 to your url. you’ll need to keep track of the highest update_id and use that as your new offset for the next request. hope this helps!

I have encountered this limitation as well. While others have mentioned using the offset parameter, another crucial factor is update retention. Telegram only keeps updates available for about 24 hours, so if you attempt to retrieve a large backlog, older updates may no longer be accessible. It is advisable to process updates as they arrive or at short time intervals to avoid missing any information. Additionally, using webhooks can improve efficiency by pushing updates directly to your server, thereby reducing the reliance on constant API polling.

I’ve dealt with this issue before, and there’s definitely a way around it. The 100 update limit is indeed the default, but you can adjust it using the ‘limit’ parameter in your API request. You can set it up to 100 at most per request.

To get all updates, you’ll need to implement a loop. After each request, use the highest update_id you received and add 1 to it. Use this as your ‘offset’ for the next request. Keep looping until you get fewer than 100 updates, which indicates you’ve reached the end.

Here’s a rough idea of how to modify your code:

offset = 0
all_updates = []

while True:
    url = f'https://api.telegram.org/bot{bot_token}/getUpdates?offset={offset}&limit=100'
    response = requests.get(url)
    updates = response.json()['result']
    
    if not updates:
        break
    
    all_updates.extend(updates)
    offset = updates[-1]['update_id'] + 1

print(f'Total updates: {len(all_updates)}')

This should get you all available updates. Just be mindful of rate limits if you’re dealing with a large number of updates.