# Updates, Callbacks and Persistent State

> Event routing, callback acknowledgement, formatting, SQLite and files.

**Category:** Getting Started  
**URL:** https://docs.botlify.cloud/bot-update-state-guide  
**Platform:** Botlify Cloud (https://botlify.cloud)  

---

## Treat updates as different event types

A Telegram update is not always a text message. It can carry a callback query, channel post, edited message, business event, inline query or another supported event. Check the field before dereferencing it. Configure allowed updates for the event types your bot needs; loading helpers does not change webhook subscriptions.

```php

```

Callback data is an action identifier, not proof that a user has permission. Check the sender before administration actions. Some callback queries refer to inline messages rather than an ordinary `message`; do not assume a chat/message ID is always present.

## Acknowledge callbacks promptly

```php

```

For privileged actions, validate the sender and the requested resource on every update. Do not authorize a user merely because they know a callback string. Use the generated API wrapper if you need parameters not present in a convenience signature.

## Message text, HTML and entities

`tg_send` defaults to HTML formatting. Escape user-controlled text before embedding it in HTML, or request plain text with `['parse_mode' => null]`. The exact API wrappers do not add the shorthand's HTML defaults. Entity offsets/lengths use UTF-16 units; string byte positions are not interchangeable with entity offsets.

```php

```

## SQLite state across requests

Local variables disappear after the request. Store conversation state in your bot's database. Use `__DIR__` to anchor the path and prepared statements for user values. The following schema is a starting point, not a complete conversation workflow.

```php

```

Keep transactions short. Do not hold a database write transaction open while waiting for Telegram. WAL mode can improve concurrency when supported, but is not a replacement for bounded writes and appropriate filesystem support.

## Duplicate updates and side effects

Track Telegram `update_id` if duplicate processing matters. A unique key can prevent duplicate local inserts. It does not guarantee exactly-once remote actions: a process can fail after Telegram accepts a request but before the local database records completion. Payments, inventory and other important actions need durable states, reconciliation and operation-specific retry rules.

A webhook HTTP 200 response acknowledges delivery; it does not prove every application action succeeded. Botlify may acknowledge handled failures to avoid repeated delivery loops. Review application state and `error.log`, not only the HTTP status.

## Files and media

Store `file_id` with the bot identity and media type; it is not portable across bots. `file_unique_id` identifies a file but cannot download/resend it. `getFile` returns a Telegram path, not a path on the Botlify server. `tg_download_file` returns `local_path`, `file_name`, `direct_tg_url` and `url`; check the local path before using it. The URL fields can contain credentials, so keep them server-side.

See [file, custom emoji, effects and album examples](/tg-native-helpers). Refer to [Telegram's Bot API](https://core.telegram.org/bots/api) for method limits, permitted formats and event fields.
