Telegram Helper Loading, PHP & Cron
Which runtime are you using?
| Execution | Normal PHP | Telegram helpers |
|---|---|---|
| Botlify hosted webhook | No loader needed | Call tg_all_fun() first |
| Botlify scheduled child cron | No loader needed | Call tg_all_fun() first |
| Direct CLI: php script.php | Runs normally | No Botlify runtime automatically provided |
| Botlify platform dashboard/master cron | Platform bootstrap | Platform loads its own helpers |
A scheduler entry is not the same as a PHP runtime. A cPanel cron that launches Botlify's master cron uses platform scheduling; a cPanel cron that directly launches your standalone PHP script does not automatically provide tg_all_fun().
Normal PHP cron
<?php
declare(strict_types=1);
// Normal PHP cron: no Telegram helpers needed.
file_put_contents(__DIR__ . '/cron-heartbeat.log', gmdate('c') . PHP_EOL, FILE_APPEND);
Schedule this file through the Botlify Files/Cron interface. It also works as standalone PHP, subject to folder permissions. No Telegram request is made.
Hosted cron using Telegram helpers
<?php
declare(strict_types=1);
tg_all_fun(); // Botlify hosted cron runtime supplies this loader.
$adminId = getenv('ADMIN_CHAT_ID');
if ($adminId === false || $adminId === '') {
throw new RuntimeException('Set ADMIN_CHAT_ID for this bot first.');
}
$result = tg_send($adminId, 'Scheduled task completed.');
if (!($result['ok'] ?? false)) {
error_log('Scheduled Telegram notification failed.');
}
Call the loader before the first helper use on every request or cron execution. It registers the suite once per request; calling it again is safe. Your active hosted-bot credentials are resolved by the shared runtime. Missing or invalid credentials still cause API failures.
Hosted webhook
<?php
declare(strict_types=1);
tg_all_fun();
$update = json_decode(file_get_contents('php://input'), true) ?: [];
$chatId = $update['message']['chat']['id'] ?? null;
if ($chatId !== null) {
tg_send($chatId, 'Hello!');
}Migrate existing helper bots
Before deploying the opt-in runtime, add the following compatibility line to every helper-based bot and child cron entrypoint. It supports both the old eager-loading runtime and the new runtime. Ordinary PHP/cURL bots do not need it.
// Add before the first helper call, after declare(strict_types=1).
if (function_exists('tg_all_fun')) {
tg_all_fun();
}
Deploy includes/telegram.php, includes/bootstrap.php, webhook.php and cron.php together. Updating bootstrap first against an old telegram.php can call a missing loader. Back up the existing release and use a maintenance window. Do not add platform include paths to hosted bot code. After migration, new bots can simply call tg_all_fun();.
Common failures
- Undefined tg_send/tg_request: load the helpers before use.
- Undefined tg_all_fun: the new runtime is not deployed, or the script runs outside Botlify's hosted runtime.
- Strict-types declaration error: keep declare(strict_types=1) before the loader and all executable code.
- Cannot redeclare a helper: do not combine custom functions using the same names with the native suite.
- Telegram ok=false: inspect description and response parameters; loading cannot fix permissions, rate limits or invalid inputs.
What is and is not loaded
The public helper suite is registered on opt-in in hosted webhook and isolated child-cron requests. Internal platform transport and the loader remain available. The containing PHP file can still be compiled; this is not a zero-parsing-overhead or guaranteed speed claim. Platform pages load helpers for platform operations.