#!/usr/bin/env php file_exists($config['last_update_id_file']) ? (int)file_get_contents($config['last_update_id_file']) : 0; $putLastUpdateId = fn($lastUpdateId) => file_put_contents($config['last_update_id_file'], "$lastUpdateId"); // request Telegram Bot API $requestApi = fn(string $method, ?array $payload = null) => json_decode(file_get_contents( "$config[endpoint]$config[token]/$method", false, stream_context_create(['http' => [ 'method' => 'POST', 'header' => 'Content-Type: application/json', 'content' => json_encode($payload), ]]), ), true); // some method we are using $getMe = fn() => $requestApi('getMe'); $getUpdates = fn(int $offset = 0) => $requestApi('getUpdates', [ 'offset' => $offset, 'limit' => (int)$config['limit'], 'timeout' => (int)$config['timeout'], ]); $sendMessage = fn($params) => $requestApi('sendMessage', $params); $response = $getMe(); if (!$response['ok']) { error_log('Failed to start the bot.'); error_log("Error message: $response[description]"); error_log("Error code: $response[error_code]"); exit(1); } $me = $response['result']; error_log('Login as: ' . json_encode($me, JSON_UNESCAPED_UNICODE)); error_log('Start receiving updates…'); while (($response = $getUpdates($getLastUpdateId() + 1))['ok']) { $updates = $response['result']; $lastUpdateId = null; foreach ($updates as $update) { $lastUpdateId = $update['update_id']; error_log('Got an update: ' . json_encode($update, JSON_UNESCAPED_UNICODE)); if (isset($update['message'])) { $sendMessage([ 'chat_id' => $update['message']['chat']['id'], 'text' => '喵', 'reply_to_message_id' => $update['message']['message_id'], ]); } $putLastUpdateId($lastUpdateId); } } if (!$response['ok']) { error_log('Failed to get updates.'); error_log("Error message: $response[description]"); error_log("Error code: $response[error_code]"); exit(1); }