/** * Execute HTTP Request * * @param string $action Action to execute * @param array|null $data Data to attach to the execution * * @return mixed Result of the HTTP Request */ public static function execute($action, array $data = null) { $debug_handle = TelegramLog::getDebugLogTempStream(); //Fix so that the keyboard markup is a string, not an object if (isset($data['reply_markup']) && !is_string($data['reply_markup'])) { $data['reply_markup'] = (string) $data['reply_markup']; } $request_params = ['debug' => $debug_handle]; //Check for resources in data $contains_resource = false; foreach ($data as $item) { if (is_resource($item)) { $contains_resource = true; break; } } //Reformat data array in multipart way if ($contains_resource) { foreach ($data as $key => $item) { $request_params['multipart'][] = array('name' => $key, 'contents' => $item); } } else { $request_params['form_params'] = $data; } try { $response = self::$client->post('/bot' . self::$telegram->getApiKey() . '/' . $action, $request_params); } catch (RequestException $e) { throw new TelegramException($e->getMessage()); } finally { //Logging verbose debug output TelegramLog::endDebugLogTempStream("Verbose HTTP Request output:\n%s\n"); } $result = $response->getBody(); //Logging getUpdates Update if ($action === 'getUpdates') { TelegramLog::update($result); } return $result; }
public function testUpdateStream() { $file = $this->logfiles['update']; $this->assertFileNotExists($file); TelegramLog::initUpdateLog($file); TelegramLog::update('my update'); $this->assertFileExists($file); $this->assertContains('my update', file_get_contents($file)); }
/** * Execute HTTP Request * * @param string $action Action to execute * @param array $data Data to attach to the execution * * @return string Result of the HTTP Request * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function execute($action, array $data = []) { //Fix so that the keyboard markup is a string, not an object if (isset($data['reply_markup'])) { $data['reply_markup'] = (string) $data['reply_markup']; } $request_params = self::setUpRequestParams($data); $debug_handle = TelegramLog::getDebugLogTempStream(); $request_params['debug'] = $debug_handle; try { $response = self::$client->post('/bot' . self::$telegram->getApiKey() . '/' . $action, $request_params); $result = (string) $response->getBody(); //Logging getUpdates Update if ($action === 'getUpdates') { TelegramLog::update($result); } } catch (RequestException $e) { $result = (string) $e->getResponse()->getBody(); } finally { //Logging verbose debug output TelegramLog::endDebugLogTempStream("Verbose HTTP Request output:\n%s\n"); } return $result; }