How to manage the offset in Telegram Bot getUpdates to avoid message loss

I’m working on a Telegram bot to provide subscription updates to users. For this to work, users must first start a conversation with the bot. I’m using deep linking for this process.

Here’s how I plan it:

  1. Share a link like https://t.me/MyNotificationBot?start=user_token with the user.
  2. The user clicks the link to start chatting with the bot.
  3. After that, they return to my site and click a check button.
  4. The site sends a getUpdates request to find the chat_id linked to their user_token.
  5. Then the offset will be increased by 1.

My challenge is managing this offset. If User A starts chatting with the bot right before User B does, but User B completes the verification first, the offset will increase. This means if User A clicks the check button afterward, their message may be missed because the offset has already advanced.

I would prefer not to use webhooks or SSL certificates. How can I best handle this offset issue to ensure messages from all users are received?

Try a sliding window approach - track processed message IDs instead of just managing offsets. When you call getUpdates, grab a bigger batch of messages and filter out the ones you’ve already handled. Keep a set of processed update_ids in memory or a simple database. This kills the race condition since you’re not stuck with sequential offset updates. You could also run a separate polling loop in the background that continuously processes incoming messages and stores chat_id mappings right when /start commands come in. This separates user verification from message retrieval, making your system way more reliable against timing issues.

why not process all updates first, then update the offset? dont increment until you’ve handled every message in the batch. also, store the chat_id + token mapping right when they hit /start - dont wait for verification. that’ll kill the race condition.

The issue you are facing with the offset in Telegram’s getUpdates is quite common. Instead of advancing the offset immediately after processing a message, consider batch processing all updates from each getUpdates call. This way, you can manage messages from multiple users more effectively. Additionally, it may be beneficial to maintain a temporary storage (like a database or cache) for chat_id and user_token pairs once users initiate conversations. This method allows your verification process to access stored data rather than relying solely on real-time message handling, thus mitigating the race condition and ensuring all messages are captured correctly.