/** * @return string */ public function getPath() { if ($this->path == null) { $this->path = Telegram::filePath(Telegram::api('getFile', ['file_id' => $this->file_id])['result']['file_path']); } return $this->path; }
/** * @param string $method * @param array $data * @return mixed */ public static function api($method, $data = []) { if (Telegram::$ch === null) { Telegram::$ch = curl_init(); } curl_setopt(Telegram::$ch, CURLOPT_URL, 'https://api.telegram.org/bot' . Telegram::$token . '/' . $method); curl_setopt(Telegram::$ch, CURLOPT_RETURNTRANSFER, true); curl_setopt(Telegram::$ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt(Telegram::$ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt(Telegram::$ch, CURLOPT_POST, true); curl_setopt(Telegram::$ch, CURLOPT_POSTFIELDS, $data); $content = curl_exec(Telegram::$ch); $json = json_decode($content, true); if (!isset($json['ok']) || $json['ok'] === false) { //todo throw exception or do something else with the error return null; } return $json; }
<?php use NielsBot\Core\NielsBot; use NielsBot\Telegram\Telegram; use NielsBot\Telegram\TelegramUpdate; spl_autoload_register(function ($cls) { $path = __DIR__ . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $cls) . '.php'; if (file_exists($path)) { require_once $path; } }); if (!isset($_GET['token'])) { die; } Telegram::setToken($_GET['token']); $update = json_decode(file_get_contents('php://input'), true); if (!isset($update['message'])) { die('Missing message in update.'); } $bot = new NielsBot(); new TelegramUpdate($update['message']);
<?php use NielsBot\Core\NielsBot; use NielsBot\Telegram\Telegram; use NielsBot\Telegram\TelegramUpdate; spl_autoload_register(function ($cls) { $path = __DIR__ . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $cls) . '.php'; if (file_exists($path)) { require_once $path; } }); Telegram::setToken(file_get_contents(__DIR__ . '/BOT_TOKEN')); $updates = Telegram::api('getUpdates'); if (!isset($updates['ok']) || !$updates['ok']) { die(json_encode($updates)); } $bot = new NielsBot(); $latestUpdate = null; foreach ($updates['result'] as $update) { $latestUpdate = $update['update_id']; new TelegramUpdate($update['message']); } if ($latestUpdate != null) { Telegram::api('getUpdates', ['offset' => $latestUpdate + 1]); }
/** * @param CURLFile $file * @param string|null $caption = null */ public function sendPhoto($file, $caption = null) { Telegram::api('sendPhoto', ['chat_id' => $this->id, 'photo' => $file, 'caption' => $caption]); }