Advertisement
Hitmare

Telegram bot advanced Debugging Bot side

Aug 10th, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.20 KB | None | 0 0
  1. // insert it in the hook.php  between the  
  2. // require_once __DIR__ . '/vendor/autoload.php';
  3. // and the
  4. // $bot_api_key  =
  5.  
  6. // Error handling
  7.    
  8.   /**
  9.    * Error handler, passes flow over the exception logger with new ErrorException.
  10.    *
  11.    * @param $num
  12.    * @param $str
  13.    * @param $file
  14.    * @param $line
  15.    * @param null $context
  16.    */
  17.   function log_error( $num, $str, $file, $line, $context = null )  {
  18.     log_exception( new ErrorException( $str, 0, $num, $file, $line ) );
  19.   }
  20.  
  21.   /**
  22.    * Uncaught exception handler.
  23.    * @param \Exception $e
  24.    */
  25.   function log_exception( $e )  {
  26.  
  27.     // setup notifier
  28.     $API_KEY  = 'BOT-API-KEY'; // Replace 'XXXXXXXXXX' with your bot's API token
  29.     $DEV_ID   = 'YOUR ID'; // Replace 'XXXXXXXXXX' with your Telegram user ID (use /whoami command)
  30.    
  31.     // get incomming message
  32.     $incoming = file_get_contents('php://input');
  33.    
  34.     // if message exist convert it into array
  35.     $incoming = !empty($incoming) ? json_decode(file_get_contents('php://input'), true) : false ;
  36.    
  37.     // developer notification message text
  38.     $message  = get_class( $e ) . " - <b>{$e->getMessage()}</b>;".PHP_EOL."File: <b>{$e->getFile()}</b>; Line: <b>{$e->getLine()}</b>; Time: <b>".date("H:i:s / d.m.Y")."</b>;".PHP_EOL."<b>Incoming message:</b><pre>".(!empty($incoming)?var_export($incoming, true).'</pre>':'</pre>'.PHP_EOL.'<b>Trace:</b><pre>'.$e->getTraceAsString().'</pre>');
  39.    
  40.     // developer notification message settings
  41.     $fields_string = '';
  42.     $url = 'https://api.telegram.org/bot'.$API_KEY.'/sendMessage';
  43.    
  44.     $fields = [
  45.         'chat_id' => urlencode($DEV_ID),
  46.         'parse_mode' => urlencode('HTML'),
  47.         'text' => urlencode(''.$message)
  48.     ];
  49.    
  50.     //url-ify the data for the POST
  51.     foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
  52.     rtrim($fields_string, '&');
  53.    
  54.     //open connection
  55.     $ch = curl_init();
  56.    
  57.     //set the url, number of POST vars, POST data
  58.     curl_setopt($ch,CURLOPT_URL, $url);
  59.     curl_setopt($ch,CURLOPT_POST, count($fields));
  60.     curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
  61.     curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
  62.    
  63.     //execute post
  64.     $result = curl_exec($ch);
  65.        
  66.     //close connection
  67.     curl_close($ch);
  68.    
  69.     // Uncomment following line and change path to store errors log in custom file
  70.      file_put_contents( __DIR__ .'/custom_errors.log', ($result?'Notified: '.var_export($result, true).PHP_EOL:'Not notified: '.var_export($result, true).PHP_EOL).$message . PHP_EOL, FILE_APPEND );
  71.  
  72.     // Sending 200 response code
  73.     header('X-PHP-Response-Code: 200', true, 200);
  74.    
  75.     exit();
  76.   }
  77.  
  78.   /**
  79.    * Checks for a fatal error, work around for set_error_handler not working on fatal errors.
  80.    */
  81.   function check_for_fatal()
  82.   {
  83.     $error = error_get_last();
  84.     if ( $error["type"] == E_ERROR )
  85.       log_error( $error["type"], $error["message"], $error["file"], $error["line"] );
  86.   }
  87.  
  88.   register_shutdown_function( "check_for_fatal" );
  89.   set_error_handler( "log_error" );
  90.   set_exception_handler( "log_exception" );
  91.   ini_set( "display_errors", "off" );
  92.   error_reporting( E_ALL );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement