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,18 +97,29 @@ class TelegramBot {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
#
# TODO - manage timeout and retry
# do http call with retry
#
$TryCounter = 0;
$Result = false;
while( $TryCounter < self::RetryMax )
{
#
# do call
#
$result = curl_exec($ch);
$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
@ -95,7 +129,7 @@ class TelegramBot {
#
# return response from api.telegram
#
return $j;
return ($Result)?$j:json_encode([]);
}
#