From 37b5553af02bc04a4fdf9eefec2e61e52e9e5a1d Mon Sep 17 00:00:00 2001 From: Radya Date: Tue, 29 Nov 2016 10:50:53 +0700 Subject: [PATCH] Add new event: channel, edited_edited and New debug ouput --- README.md | 4 +++- src/Bot.php | 23 +++++++++++++++++++++++ src/PHPTelebot.php | 9 ++++++--- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1459039..8970069 100644 --- a/README.md +++ b/README.md @@ -181,6 +181,8 @@ $bot->on('sticker|photo|document', function() { - **inline** - inline message - **callback** - callback message - **game** - game +- **channel** - channel +- **edited_channel** - edited channel post ## Command with custom regex *(advanced)* @@ -201,7 +203,7 @@ Handles events. ##### `regex(, )` Create custom regex for command. ##### `Bot::type()` -Return [message event](#message-events) type. +Return [message event](#supported-events) type. ##### `Bot::message()` Get [message properties](https://core.telegram.org/bots/api#message). diff --git a/src/Bot.php b/src/Bot.php index c4324d0..070a11f 100644 --- a/src/Bot.php +++ b/src/Bot.php @@ -23,6 +23,13 @@ class Bot * * @return array|bool */ + + /** + * Bot debug + * @var array + */ + public static $debug = ''; + public static function send($action = 'sendMessage', $data = []) { $upload = false; @@ -82,6 +89,12 @@ public static function send($action = 'sendMessage', $data = []) $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); + if (PHPTelebot::$debug && $action != 'getUpdates') { + self::$debug .= 'Method: '.$action."\n"; + self::$debug .= 'Data: '.str_replace("Array\n", '', print_r($data, true))."\n"; + self::$debug .= 'Response: '.$result."\n"; + } + if ($httpcode == 401) { throw new Exception('Incorect bot token'); @@ -170,6 +183,12 @@ public static function message() return $get['inline_query']; } elseif (isset($get['edited_message'])) { return $get['edited_message']; + } elseif (isset($get['channel_post'])) { + return $get['channel_post']; + } elseif (isset($get['edited_channel_post'])) { + return $get['edited_channel_post']; + } else { + return []; } } @@ -228,6 +247,10 @@ public static function type() return 'edited'; } elseif (isset($getUpdates['message']['game'])) { return 'game'; + } elseif (isset($getUpdates['channel_post'])) { + return 'channel'; + } elseif (isset($getUpdates['edited_channel_post'])) { + return 'edited_channel'; } else { return 'unknown'; } diff --git a/src/PHPTelebot.php b/src/PHPTelebot.php index b5cf1cb..43542f6 100644 --- a/src/PHPTelebot.php +++ b/src/PHPTelebot.php @@ -45,14 +45,14 @@ class PHPTelebot * * @var bool */ - protected static $debug = true; + public static $debug = true; /** * PHPTelebot version. * * @var string */ - protected static $version = '1.1'; + protected static $version = '1.3'; /** * PHPTelebot Constructor. @@ -186,9 +186,12 @@ private function longPoll() if (!empty($req['result'])) { foreach ($req['result'] as $update) { self::$getUpdates = $update; - $response = $this->process(); + $process = $this->process(); if (self::$debug) { + $response = Bot::$debug?: $process; + // reset debug + Bot::$debug = ''; $line = "\n--------------------\n"; $outputFormat = "$line %s $update[update_id] $line%s"; echo sprintf($outputFormat, 'Query ID :', json_encode($update));