<?php // we are on git saeed
class Bot {
public $BOT_TOKEN;
private $API_URL;
private $WEBHOOK_URL;
public $botUsername;
public $debug;
function __construct($botUsername,$botToken,$webhookUrl) {
$this->botUsername = $botUsername;
$this->BOT_TOKEN = $botToken;
$this->WEBHOOK_URL = $webhookUrl;
$this->API_URL = 'https://api.telegram.org/bot' . $this->BOT_TOKEN . '/';
// $botme=$this->getMe();
// $testMessage = var_export($botme, true);
// file_put_contents("testbot2.html", $testMessage, FILE_APPEND | LOCK_EX);
}
public function apiRequestWebhook($method, $parameters) {
if (! is_string ( $method )) {
error_log ( "Method name must be a string\n" );
return false;
}
if (! $parameters) {
$parameters = array ();
} else if (! is_array ( $parameters )) {
error_log ( "Parameters must be an array\n" );
return false;
}
$parameters ["method"] = $method;
if($_SERVER['SERVER_NAME']!="unittest"){
header ( "Content-Type: application/json" );
}
echo json_encode ( $parameters );
return true;
}
public function getMe(){
$this->apiRequest( 'getMe', array (
'get' => "true"
) );
}
public function setWebhook(){
return $this->apiRequest( 'setWebhook', array (
'url' => $this->WEBHOOK_URL
) );
}
public function deleteWebhook(){
$this->apiRequest( 'deleteWebhook', array (
'url' => $this->WEBHOOK_URL
) );
}
public function WebhookInfo(){
echo $this->apiRequest( 'WebhookInfo');
}
public function exec_curl_request($handle) {
$response = curl_exec ( $handle );
if ($response === false) {
$errno = curl_errno ( $handle );
$error = curl_error ( $handle );
throw new Exception($error);
error_log ( "Curl returned error $errno: $error\n" );
curl_close ( $handle );
return false;
}
$http_code = intval ( curl_getinfo ( $handle, CURLINFO_HTTP_CODE ) );
curl_close ( $handle );
if ($http_code >= 500) {
// do not wat to DDOS server if something goes wrong
sleep ( 10 );
return false;
} else if ($http_code != 200) {
$response = json_decode ( $response, true );
error_log ( "Request has failed with error {$response['error_code']}: {$response['description']}\n" );
if ($http_code == 401) {
throw new Exception ( 'Invalid access token provided' );
}
return false;
} else {
$response = json_decode ( $response, true );
if (isset ( $response ['description'] )) {
error_log ( "Request was successfull: {$response['description']}\n" );
}
$response = $response ['result'];
}
return $response;
}
public function apiRequest($method, $parameters) {
// print_r($parameters);
if (! is_string ( $method )) {
error_log ( "Method name must be a string\n" );
return false;
}
if (! $parameters) {
$parameters = array ();
} else if (! is_array ( $parameters )) {
error_log ( "Parameters must be an array\n" );
return false;
}
foreach ( $parameters as $key => &$val ) {
// encoding to JSON array parameters, for example reply_markup
if (! is_numeric ( $val ) && ! is_string ( $val )) {
$val = json_encode ( $val );
}
}
$url = $this->API_URL . $method . '?' . http_build_query ( $parameters );
$handle = curl_init ( $url );
curl_setopt ( $handle, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $handle, CURLOPT_CONNECTTIMEOUT, 150 );
curl_setopt ( $handle, CURLOPT_TIMEOUT, 220 );
return $this->executeCurl($handle);
}
public function executeCurl($handle){
return $this->exec_curl_request($handle);
}
public function apiRequestJson($method, $parameters) {
if (! is_string ( $method )) {
error_log ( "Method name must be a string\n" );
return false;
}
if (! $parameters) {
$parameters = array ();
} else if (! is_array ( $parameters )) {
error_log ( "Parameters must be an array\n" );
return false;
}
$parameters ["method"] = $method;
$handle = curl_init ( API_URL );
curl_setopt ( $handle, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $handle, CURLOPT_CONNECTTIMEOUT, 5 );
curl_setopt ( $handle, CURLOPT_TIMEOUT, 60 );
curl_setopt ( $handle, CURLOPT_POSTFIELDS, json_encode ( $parameters ) );
curl_setopt ( $handle, CURLOPT_HTTPHEADER, array (
"Content-Type: application/json"
) );
return $this->exec_curl_request ( $handle );
}
}
?>