Retry added

This commit is contained in:
Daniele Callari 2019-10-04 09:33:10 +02:00
parent 89582e8644
commit 3424680e9b

View File

@ -1,16 +1,29 @@
<?php
class TelegramBot {
/*
* PHP Telegram API implementation
* Daniele Callari
* Daxtech.net
*
*/
class TelegramBot
{
#
# version
#
const Version = '1.1';
const Version = '1.2';
#
# limit of http retry
#
const RetryMax = 5;
#
# Bot token
#
private $Token = null;
#
# bool param
# bool param - TODO
#
private $Async = false;
@ -32,11 +45,21 @@ class TelegramBot {
#
# init class
#
function __construct($token){
#
function __construct($token)
{
#
# set token as private
#
$this->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 {
#