From 3424680e9bb9f1990dcc17c75c5209477b164d3d Mon Sep 17 00:00:00 2001 From: Daniele Callari Date: Fri, 4 Oct 2019 09:33:10 +0200 Subject: [PATCH] Retry added --- Telegram.class.php | 72 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 19 deletions(-) diff --git a/Telegram.class.php b/Telegram.class.php index d4473f4..2d06df8 100644 --- a/Telegram.class.php +++ b/Telegram.class.php @@ -1,16 +1,29 @@ Token = $token; + + # + # check curl module + # + if( !function_exists ('curl_init') ) + { + header("Content-type:application/json"); + die(json_encode(array('error' => true, 'message' => 'php-curl module not installed/enabled'))); + } } # @@ -74,28 +97,39 @@ class TelegramBot { curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); # - # TODO - manage timeout and retry + # do http call with retry # + $TryCounter = 0; + $Result = false; - # - # do call - # - $result = curl_exec($ch); - + while( $TryCounter < self::RetryMax ) + { + # + # do call + # + $Result = curl_exec($ch); + + # + # manage retry + # + if($Result === false) $TryCounter++; + else $TryCounter = self::RetryMax; + } + # # parse response # - $j = json_decode($result, true); - + $j = @json_decode($Result, true); + # # output if debug # if($this->debug) print_r($j); - + # # return response from api.telegram # - return $j; + return ($Result)?$j:json_encode([]); } # @@ -158,9 +192,9 @@ class TelegramBot { 'photo' => $url )); }else{ - return $this->_httpSend('sendDocument', array( - 'chat_id' => $destination, - 'photo' => new CURLFile($url) + return $this->_httpSend('sendDocument', array( + 'chat_id' => $destination, + 'photo' => new CURLFile($url) )); } } @@ -295,7 +329,7 @@ class TelegramBot { } catch(Exception $e) { - if($this->debug) echo $e->getMessage()."\n"; + if($this->debug) echo $e->getMessage()."\n"; } finally { #