public function testExternalStream() { $file = $this->logfiles['external']; $this->assertFileNotExists($file); $external_monolog = new Logger('bot_update_log'); $external_monolog->pushHandler(new StreamHandler($file, Logger::ERROR)); $external_monolog->pushHandler(new StreamHandler($file, Logger::DEBUG)); TelegramLog::initialize($external_monolog); TelegramLog::error('my error'); TelegramLog::debug('my debug'); $this->assertFileExists($file); $file_contents = file_get_contents($file); $this->assertContains('bot_update_log.ERROR: my error', $file_contents); $this->assertContains('bot_update_log.DEBUG: my debug', $file_contents); }
/** * Url Shortener function * * @param $url * @param $user_id * * @return string * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function shortenUrl($url, $user_id) { if (empty(self::$token)) { return $url; } if (empty($user_id)) { throw new TelegramException('User id is empty!'); } $cached = BotanDB::selectShortUrl($user_id, $url); if (!empty($cached[0]['short_url'])) { return $cached[0]['short_url']; } $request = str_replace(['#TOKEN', '#UID', '#URL'], [self::$token, $user_id, urlencode($url)], self::$shortener_url); $options = ['http' => ['ignore_errors' => true, 'timeout' => 3]]; $context = stream_context_create($options); $response = @file_get_contents($request, false, $context); if (!filter_var($response, FILTER_VALIDATE_URL) === false) { BotanDB::insertShortUrl($user_id, $url, $response); } else { TelegramLog::debug('Botan.io API replied with error: ' . $response); return $url; } return $response; }